Jump to content

loop through SQL results to print an HTML table


Go to solution Solved by jasonxxx102,

Recommended Posts

Ok, so I have a main page that grabs data from a MySQL db and posts it into a dropdown menu

 

see here:

$sql = "SELECT * FROM 4000Series";
$result = $con->query($sql);

print "Select your GPU: <select name='gpuId'>";
while ($row = $result->fetch_assoc())
{
    print "<option value='" .$row["id"] . "'>" . $row["gpu"] . "</option>";
}
print "</select> </br>\n";

This works fine, from here I'm passing the POST data from the option values to a new page (basically the option values correspond directly to the "id" value in my SQL db).

 

The values are passed onto the new page fine, but when I try to setup an SQL select statement to grab the row with the corresponding "id" value and print it out with a while loop I'm shooting a blank.

 

Here's the code for the second page

$id = $_POST['gpuId'];

**Connect to sql db here**

$sql = "SELECT * FROM 4000Series WHERE $id ='id'";
$result = $con->query($sql);

echo "<table>"; 

while ($row = $result->fetch_assoc()){   
echo "<tr><td>" . $row['id'] . "</td><td>" . $row['gpu'] . "</td></tr>";  
}

echo "</table>";

When I view the source it prints out the table tags but nothing in the while loop, any ideas?

you have a $ in front of your id column name in your query, you are also missing the $ in front of the $id variable name in your query OR you could remove the single-quotes from around the id column name in your query.

 

if you include a debugging step where you echo out your sql query statement, you would have likely seen this problem.

Edited by mac_gyver
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.