Page 1 of 1
can't retrieve "email" column from mysql
Posted: Sun Jul 18, 2010 8:52 pm
by iansane
Hi again,
I hate to keep asking noobie questions but I've searched for this one and can't find an answer.
in the following code the email is not getting retrieved but phone and privLevel work just fine.
Code: Select all
$sql = "SELECT * FROM $tbl_name where uName='$uName'";
$result = mysql_query($sql);
for($i = 0; $i < mysql_num_rows($result); $i++){
$email = mysql_result($result, $i , 'email');
$phone = mysql_result($result, $i , 'phone');
$privilege = mysql_result($result, $i , 'privLevel');
}
Does it have anything to do with the "@" symbol in the email? And if so, how do I get around it?
I've double and triple checked that the column name "email" is correct. If I echo out $email it is empty but echo $phone and echo $privilege work.
Thanks
Re: can't retrieve "email" column from mysql
Posted: Sun Jul 18, 2010 10:15 pm
by JakeJ
Once you have a result, you need to fetch a row or an array. It looks like you're only expecting one row of data so do the following.
Code: Select all
$sql = "SELECT * FROM $tbl_name where uName='$uName'";
$result = mysql_query($sql);
$row = mysql_fetch_row($result);
$email = $row['email'];
$phone = $row['phone'];
$privlevel = $row['privLevel'];
//echo it out
echo $email.'<br />';
echo $phone.'<br />';
echo $privlevel.'<br />;
Re: can't retrieve "email" column from mysql
Posted: Mon Jul 19, 2010 3:34 pm
by iansane
Thanks Jake,
That works, and yeah it's just one row. It's for log in and setting the email into $_SESSION for use later in the application.
I still think it has something to do with the "@" symbol because I can use the for loop method for all the other fields and they work just fine but I'll stick with your solution so as not to have problems later.
mysql_fetch_row() if one row expected and mysql_fetch_assoc_array() if more than one row expected.
Thanks a lot!

Re: can't retrieve "email" column from mysql
Posted: Thu Jul 22, 2010 10:12 pm
by iansane
Well it's not working. I thought it was a few days ago but now it's not working for some reason.
Here's the exact code. When I echo out the $_SESSION variables on another page nothing is there.
Code: Select all
//creat sql statement
$sql="SELECT * FROM $tbl_name WHERE uName='$uName' and pWord='$encryptedPass'";
$result=mysql_query($sql);
//count matching row. If match then $count = 1
$count=mysql_num_rows($result);
if($count==1){
$row = mysql_fetch_row($result);
$email = $row['email'];
$privLevel = $row['privLevel'];
session_start();
$_SESSION['validName'] = 'valid';
$_SESSION['validPass'] = 'valid';
$_SESSION['userName'] = $uName;
$_SESSION['email'] = $email;
$_SESSION['privLevel'] = $privLevel;
require_once '../db/dbclose.php';
header("location: ../../app/home.php");
}
else{
echo "Wrong Username or Password";
}
validName, validPass, and userName work but they aren't coming from the database.
email and privLevel are not getting retrieved from $row['email'] and $row['privLevel']
Can anyone see something I'm overlooking in my code?
Thanks
I added $_SESSION['row'] = $row; and then print_r($_SESSION); in the home page and this is what I get
Array ( [validName] => valid [validPass] => valid [userName] => isimmons [email] => [privLevel] => [row] => Array ( [0] =>
noone@nowhere.com [1] => 0 ) )
So it is putting it into $row but not getting the data from $row[] array into individual variables
Thanks
Re: can't retrieve "email" column from mysql
Posted: Thu Jul 22, 2010 11:20 pm
by JakeJ
Your query is wrong. Notice the curly brackets around the table name.
Code: Select all
$sql="SELECT * FROM {$tbl_name} WHERE uName='$uName' and pWord='$encryptedPass'";
I'm not sure why they made it that way, you'd think you could put quotes around it but nope.
Re: can't retrieve "email" column from mysql
Posted: Thu Jul 22, 2010 11:35 pm
by iansane
Are you sure about that JakeJ?
I've never put {} around the table name and my other queries work fine. Also I see examples all over the internet that don't use the {}'s
Is that something new?
Also, without the braces it is getting the data and putting it into $row just fine. I just can't get it from $row into $email and $privLevel.
Put the {} around $tbl_name and still get the same problem. no difference.
Thanks
Re: can't retrieve "email" column from mysql
Posted: Thu Jul 22, 2010 11:42 pm
by JakeJ
mm....interesting.. when i first started writing queries, when I wanted to use a variable as a table name, I had to do that. Nothing else worked for me at the time. I might have to revisit that.
What browser are you doing this in? IE doesn't always like to pass data if there's no privacy policy loaded. Try firefox and see if that works and let me know.
Re: can't retrieve "email" column from mysql
Posted: Thu Jul 22, 2010 11:52 pm
by iansane
I'm using Firefox on a linux (Ubuntu box).
I thought $_SESSION was a server side session. I'm confused by that. Or confused by the difference between cookie (as in setcookie()) and $_SESSION. I thought setcookie was for client side cookies and $_SESSION for the server side. As you can tell I'm fairly new to php.
The strange thing is that it worked the other day after I followed your advice in your first response.
Also it's strange that when I pass the entire $row variable through session it comes out on the other page with all the data so it is definately getting the data. But somewhere in the statement $email = $row['email']; it is not getting from $row into the $email variable. I thought it might have to do with single or double quotes but I tried both and even tried $email = $row[0]; that didn't work either.
I have to get some sleep and be at work in a few hours but thank you for your assistance so far. I'll revisit this tomorrow.
Thanks
Re: can't retrieve "email" column from mysql
Posted: Fri Jul 23, 2010 12:01 am
by JakeJ
Try echoing out your variables on the page first. echo $row['email']. See if anything is there.
Re: can't retrieve "email" column from mysql
Posted: Fri Jul 23, 2010 12:08 am
by iansane
the result of this
Code: Select all
$row = mysql_fetch_row($result);
$email = $row['email'];
$privLevel = $row['privLevel'];
echo $row;
echo "<br />";
echo $row['email'];
echo "<br />";
echo $email;
is
Array
noone@nowhere.com
so it echoes $row['email'] but not $email
Re: can't retrieve "email" column from mysql
Posted: Fri Jul 23, 2010 12:29 am
by iansane
And now it doesn't work!
echo $row['email']; worked once and now won't work again. I changed nothing but just refreshed the page. It's like the other day things worked and now they don't. I don't know what it could be. It's crazy!

Re: can't retrieve "email" column from mysql
Posted: Fri Jul 23, 2010 9:36 am
by JakeJ
I have no idea what to tell you then. Just keep playing until it works, then back up that code.
Re: can't retrieve "email" column from mysql
Posted: Sat Jul 24, 2010 9:45 am
by iansane
Definitely something strange going on. Last night a professor at my school suggested that maybe using the same variable name was confusing the php engine so I changed it to
Code: Select all
//instead of
$email = $row['email'];
//changed to this
$lv_email = $row['email'];
The strange part is that it worked. The reason this is strange is because I forgot to change the var in
so I wasn't even passing $lv_email through $_SESSION but it still showed up on the other page where it is retrieved from $_SESSION['email'];
It worked once so I thought the problem was solved and then tried it again this morning and that's when I realized I hadn't even remembered to pass it into $_SESSION. How can this be? How could it have worked at all when I tested it the first time? It's crazy.
Either way, using the index number seems to be the one thing that continuously works so I'm sticking with that but I really want to know what is going on inside php to cause this strange behavior.
If you think of anything else please let me know. Thanks again for your help.
Re: can't retrieve "email" column from mysql
Posted: Fri Aug 06, 2010 7:24 am
by iansane
Wow, This slipped by both of us and was so simple.
mysql_fetch_row() returns a regualar array which can only use index numbers.
while mysql_fetch_assoc() returns an associative array where the name of the variable at each index can be used.
So if I want to use the var name rather than the index number mysql_fetch_assoc() can be used regardless of whether it is one or more rows being returned.
Re: can't retrieve "email" column from mysql
Posted: Fri Aug 06, 2010 11:14 am
by JakeJ
iansane wrote:Wow, This slipped by both of us and was so simple.
mysql_fetch_row() returns a regualar array which can only use index numbers.
while mysql_fetch_assoc() returns an associative array where the name of the variable at each index can be used.
So if I want to use the var name rather than the index number mysql_fetch_assoc() can be used regardless of whether it is one or more rows being returned.
That's why I use mysql_fetch_array().
As for the quirky behavior you mentioned, I'm far too sleepy still to sort through it.