Using mysql_fetch_row to retrieve data from database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
marnieg
Forum Commoner
Posts: 65
Joined: Wed Mar 12, 2003 4:35 pm

Using mysql_fetch_row to retrieve data from database

Post by marnieg »

Here is the code I have for selecting all fields from a table based on an email address entered. When I call the function and try to echo the results I get NO data. I have tried other functions such as fetch_assoc, but get the same results.

I know that I am getting a valid result because it does echo the literals, but no variables.

Please advise

mysql_select_db("orders");
$query = "select * from rxfirst where email = '.$email.')";
$result = mysql_query($query);
if ($result)

$row = mysql_fetch_row($result);

echo "Name of Patient: <u>" .$row[3]. "</u> Phone number: <u>".$row[9]."</u><br>";
echo "Mailing Address: <u>" .$row[4]. "</u> Apt/Space:_________ <br>";

I have verified that name is the 4th column in my table, since starting with 0 and phone is 9, and address is 4.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

try

Code: Select all

$row = mysql_fetch_row($result) or die('no data');
print_r($row);
pootergeist
Forum Contributor
Posts: 273
Joined: Thu Feb 27, 2003 7:22 am
Location: UK

Post by pootergeist »

just remove the closing parentesis from your select statement

where email = '.$email.')"; == bad

where email = '.$email.'"; == better
marnieg
Forum Commoner
Posts: 65
Joined: Wed Mar 12, 2003 4:35 pm

Post by marnieg »

Thanks for pointing out the ) in my select statement and the other post for using the print_r($row) so I could see the array.

The actual solution was to remove the periods (.) around the variable $email in the select statement.

In all the examples I have seen online and in my php reference manuals, it shows the period(.) syntax. I'm not sure why but I am just glad I got it working.

Thanks for your direction.
User avatar
daven
Forum Contributor
Posts: 332
Joined: Tue Dec 17, 2002 1:29 pm
Location: Gaithersburg, MD
Contact:

Post by daven »

What you should do is this:

$query = "select * from rxfirst where email = '".$email."'";

The dot notation is for string concatenation. If you have imbedded strings ($query = "select * from rxfirst where email = '$email'";) there are several problems which can arise.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

The period is used for concenation, that is joining two strings and/or variables together. So you can do:

Code: Select all

$concentated_vars = $var1.$var2;
$concentated_strings = 'something'.$var1;
PHP doesn't parse code within single quotes so

Code: Select all

$var = 'world';
echo 'Hello $var';
would produce

Code: Select all

Hello $var
whereas

Code: Select all

$var = 'world';
echo 'Hello '.$var;
would produce

Code: Select all

Hello world
PHP does however parse code in double quotes so you would get 'hello world' from this too:

Code: Select all

echo "Hello $world";
Your SQL statement is in double quotes so:

Code: Select all

$query = "select * from rxfirst where email = '$email'";
is fine.

If you are having problems with an SQL statement echo out the statement to check that variables are being replaced correctly, and use mysql_error() to trap errors:

Code: Select all

echo 'The SQL statement is: '.$query;
$result = mysql_query($query) or die(mysql_error().'<p>'.$query.'</p>');
For more information:
http://www.php.net/manual/en/language.types.string.php
http://www.php.net/manual/en/function.mysql-error.php
http://www.php.net/manual/en/function.die.php

Mac
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

daven wrote:What you should do is this:

$query = "select * from rxfirst where email = '".$email."'";
The concenation is not necessary within double quoted strings.
daven wrote:If you have imbedded strings ($query = "select * from rxfirst where email = '$email'"; ) there are several problems which can arise.
Not within double quoted strings, unless you're using array elements in which case you could do:

Code: Select all

$query = "select * from rxfirst where email = '$email[info]'";
// or
$query = "select * from rxfirst where email = '{$email['info']}'";
It's down to personal choice.

Mac
Post Reply