简体   繁体   English

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

[英]Use variable as ID and get table from mysql (edited)

I'm using a multiple select option form to get a table of venues. 我正在使用多项选择选项表格来获取场地表。 Each venue has an ID and this is what I used: 每个场所都有一个ID,这就是我使用的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;



  }
}

?>

It results in this: 结果是: 在此处输入图片说明

However I thought that the code would use those two numbers below and draw out a table with those id's I used :/ ? 但是我认为代码会在下面使用这两个数字,并用我使用的id绘制一张表:/? Am I missing something? 我想念什么吗?

$array is used in implode(",", $array); $array用于implode(",", $array); but is not defined anywhere else that we can see. 但未在我们可以看到的其他地方定义。 It is perhaps intended to be: 它可能旨在:

implode(",", $aVenues);

UPDATE 更新

Per comments, it does not draw a table because you never actually query your database. 对于注释,它不会绘制表,因为您从未真正查询过数据库。

You build your SQL statement, but you need to execute it and fetch the result set. 您构建了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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM