开发者

Sort information from database into a table

I am trying to sort all of this information into a small table which I looked up on a tutorial, however I am having problems :/

I had this originally:

$sql = "SELECT * FROM venues WHERE id IN (" . implode(",",$aVenues) . ")";
$comma_separated = implode(",", $aVenues);
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();

And I changed it to this:

$sql = "SELECT * FROM venues WHERE id IN (" . implode(",",$aVenues) . ")";
$comma_separated = implode(",", $aVenues);
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;
  }

  echo "<table border='1'>";
echo "<tr> <th>Name</th> <th>Age</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $rowset)) {
    // Print out the contents of each row into a table
    echo "<tr><td>"; 
    echo $row['VENUE_NAME'];
    echo "</td><td>"; 
    echo $row['ADDRESS'];
    echo "</td></tr>"; 
} 

echo "</table>";



}
else echo mysql_error();

...and this is the error I am getting now :/

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/nightl7/public_html/demos/autocompletejquery/index.php on line 66

I also tried changing

while($row = mysql_fetch_array( $rows开发者_如何学Cet))

to

while($row = mysql_fetch_array( $result))

but all that did was make the error disappear, but the rows weren't being displayed. Thanks everyone :))))!


You are looking to loop over a result array ($rowset) and create a table. There are a few ways to modify this, but the easiest is:

Change

while($row = mysql_fetch_array( $rowset)) {

to

foreach ($rowset as $row) {

And keep the rest as it is.

So the second while loop becomes:

foreach ($rowset as $row) {
    // Print out the contents of each row into a table
    echo "<tr><td>"; 
    echo $row['VENUE_NAME'];
    echo "</td><td>"; 
    echo $row['ADDRESS'];
    echo "</td></tr>"; 
}


while($row = mysql_fetch_array( $rowset)) {
    // Print out the contents of each row into a table
    echo "<tr><td>"; 
    echo $row['VENUE_NAME'];
    echo "</td><td>"; 
    echo $row['ADDRESS'];
    echo "</td></tr>"; 
}

$rowset isn't a result resource. Even if it were, you've already parsed all of the results. You want:

foreach ($rowset as $row) {
    // Print out the contents of each row into a table
    echo "<tr><td>"; 
    echo $row['VENUE_NAME'];
    echo "</td><td>"; 
    echo $row['ADDRESS'];
    echo "</td></tr>"; 
}


Change your line that says

while($row = mysql_fetch_array( $rowset)) {

to

foreach($rowset as $row) {

Your first while-loop already picks up all results. It stores them in an array called $rowset.

In the PHP-manual you can find how to iterate over arrays: http://php.net/manual/en/control-structures.foreach.php

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜