Jump to content

Fetching data from mysql table using $row


eMonk

Recommended Posts

Right now I'm trying:

 

$id = $_GET['id'];

 

$city_1 = mysql_query("SELECT * FROM model_in_city WHERE city_display = 1 AND model_id = '$id'  ");

$city_2 = mysql_query("SELECT * FROM model_in_city WHERE city_display = 2 AND model_id = '$id'  ");

 

but it's not displaying the correct value in the following text field:

 

<tr>

  <td>City 1</td>

  <td><input name="city_1" type="text" size="2" value="<?=$city_1?>" maxlength="2"></td>

</tr>

 

However, PHPMYADMIN is showing the correct value. Any ideas?

eMonk:

  When you do a mysql_query() the mysql server calculates the result.  That result then sits on the server.  In order to access it you have to fetch the individual rows.  This is clearly shown in the php manual, so I'm not going to post it here.  There are many different ways to fetch but the one I recommend for most people is mysql_fetch_assoc

See anything wrong with this code? $city_1 and $city_2 still aren't showing up in text fields but the data shows correctly in phpmyadmin.

 

$id = $_GET['id'];

$qP2 = "SELECT * FROM model_in_city WHERE city_display = 1 AND model_id = '$id'  ";
$rsP2 = mysql_query($qP2);
$row2 = mysql_fetch_array($rsP2);
extract($row2);
$city_1 = trim($city_display);

$qP3 = "SELECT * FROM model_in_city WHERE city_display = 2 AND model_id = '$id'  ";
$rsP3 = mysql_query($qP3);
$row3 = mysql_fetch_array($rsP3);
extract($row3);
$city_2 = trim($city_display);

 

what you have there will echo whatever is in you city_display column..if your query is grabbing multiple rows then you'll want to use a while() loop as well..if nothing is getting echoed then try debugging your query e.g

$rsP3 = mysql_query($qP3) or die(mysql_error());

 

edit: gizmola posted right before me...read the link that he posted...should help

Ok, I have it working now...

 

$city_2 = $row3['city_display'];

 

should have been

 

$city_2 = $row3['city_id'];

 

however now when i echo $city_2 at the bottom of the page, the value isn't displaying correctly like before. I'm not overwriting the $city_2 variable anywhere. Why is this variable loosing it's value at the bottom of the page?

Archived

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

×
×
  • 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.