Using mysql_fetch_row to retrieve data from database
Moderator: General Moderators
Using mysql_fetch_row to retrieve data from database
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.
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.
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
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.
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.
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
The period is used for concenation, that is joining two strings and/or variables together. So you can do:
PHP doesn't parse code within single quotes so
would produce
whereas
would produce
PHP does however parse code in double quotes so you would get 'hello world' from this too:
Your SQL statement is in double quotes so:
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:
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
Code: Select all
$concentated_vars = $var1.$var2;
$concentated_strings = 'something'.$var1;Code: Select all
$var = 'world';
echo 'Hello $var';Code: Select all
Hello $varCode: Select all
$var = 'world';
echo 'Hello '.$var;Code: Select all
Hello worldCode: Select all
echo "Hello $world";Code: Select all
$query = "select * from rxfirst where email = '$email'";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>');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
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
The concenation is not necessary within double quoted strings.daven wrote:What you should do is this:
$query = "select * from rxfirst where email = '".$email."'";
Not within double quoted strings, unless you're using array elements in which case you could do:daven wrote:If you have imbedded strings ($query = "select * from rxfirst where email = '$email'"; ) there are several problems which can arise.
Code: Select all
$query = "select * from rxfirst where email = '$email[info]'";
// or
$query = "select * from rxfirst where email = '{$email['info']}'";Mac