Jump to content

Archived

This topic is now archived and is closed to further replies.

BadGoat

Trouble echoing multiple variables from the same table

Recommended Posts

Hello!

I am trying to echo multiple values from the same table, which are identified by an ID field. The data makes it into the db, and it works fine when I echo the ID rather than the gear, but when I try to echo the gear, it displays blank. Any ideas?

$sqlquery = "INSERT INTO bike VALUES('', '". $_POST['first_gear_id'] ."', '". $_POST['second_gear_id'] ."', '". $_POST['third_gear_id'] ."', '". $_POST['fourth_gear_id'] ."', '". $_POST['fifth_gear_id'] ."')";
$queryresult = mysql_query($sqlquery) or die(" Could not execute mysql query 1!");

$sqlquery = "SELECT * from bike";

$get_gear_info = mysql_query("SELECT * FROM gear_ratios WHERE gear_id = '".$_POST['first_gear_id']."'");
$row=mysql_fetch_array($get_gear_info);
$first_gear_id = $row['first_gear_id'];

echo'
<tr>
<td>First Gear</td>
<td>' .$first_gear. '</td>
</tr>

$get_gear_info = mysql_query("SELECT * FROM gear_ratios WHERE gear_id = '".$_POST['second_gear_id']."'");
$row=mysql_fetch_array($get_gear_info);
$second_gear_id = $row['second_gear_id'];

echo'
<tr>
<td>First Gear</td>
<td>' .$second_gear. '</td>
</tr>

Share this post


Link to post
Share on other sites
If your pulling multiple values from a table you could through the query in a loop like:

[code]
// Select all rows in the database
$get_gear_info = mysql_query("SELECT * FROM gear_ratios");

// Loop through the rows and output them
while($row = mysql_fetch_array($get_gear_info)) {
   echo "ID {$row['gear_id']} <br />";
}
[/code]

Then it would output:
ID 1
ID 2
ID 3
.
.
.

Through your whole table. You would also retrieve the other fields via $row['whateverfieldnameis']

Share this post


Link to post
Share on other sites
Also it might help to note that the variable '$first_gear' and '$second_gear' are echoed, yet never defined.

Share this post


Link to post
Share on other sites

×

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.