简体   繁体   中英

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:

<?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 :/ ? Am I missing something?

$array is used in 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.

// 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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