使用变量作为ID并从mysql获取表(已编辑)

时间:2011-09-19 18:55:32

标签: php mysql

我正在使用多选选项表格来获取场地表。每个场地都有一个ID,这就是我使用的:

<?php
require("db_access.php");

if(isset($_POST['select3'])) 
{
  $aVenues = $_POST['select3'];

  if(!isset($aVenues))
  {
    echo("<p>You didn't select any venues!</p>\n");
  }
  else
  {
    $nVenues = count($aVenues);

    echo("<p>You selected $nVenues venues: ");
    for($i=0; $i < $nVenues; $i++)
    {
      echo($aVenues[$i] . " ");
    }
    echo("</p>");

$sql = "SELECT * FROM venues WHERE id IN (" . implode(",",$aVenues) . ")";
$comma_separated = implode(",", $aVenues);
echo $comma_separated;



  }
}

?>

结果如下: enter image description here

但是我认为代码会使用下面的这两个数字并绘制一个表格,其中包含我使用的那些ID:/?我错过了什么吗?

1 个答案:

答案 0 :(得分:1)

$arrayimplode(",", $array);中使用,但未在我们可以看到的任何其他位置定义。它可能是:

implode(",", $aVenues);

<强>更新

根据评论,它不会绘制表格,因为您实际上从不查询数据库。

构建SQL语句,但需要执行它并获取结果集。

// Make sure you actually have a database connection
$conn = mysql_connect('localhost', $username, $password);
mysql_select_db($database);

$sql = "SELECT * FROM venues WHERE id IN (" . implode(",",$aVenues) . ")";
$comma_separated = implode(",", $array);
echo $comma_separated;

// Execute query and fetch result rowset
$result = mysql_query($sql);
if ($result) {
  $rowset = array();
  while ($row = mysql_fetch_array($result)) {
    $rowset[] = $row;
  }
  var_dump($rowset);
}
else echo mysql_error();