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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

try this

$id = $_GET['id'];

$qP2 = "SELECT * FROM model_in_city WHERE city_display = 1 AND model_id = '$id'  ";
$rsP2 = mysql_query($qP2);
$row2 = mysql_fetch_assoc($rsP2);
$city_1 = $row2['example_field']

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.