0

I am successfully selecting the data from database table, when ever I am trying to fetch that data in to an array with the help of mysql_fetch_array() it's not storing anything into the array.

@session.start();
$name=$_SESSION['umailid'];
$chkname1 = "select * from ".USREG." where email='.$name.'";

echo $chkname1;
// it is printing like this:
// " select * from users_temp where email='[email protected].' "
// which means query was successful, above email is there in database table

$res1 = mysql_query($chkname1, $con) or die(mysql_error());

$chkresult1 = mysql_fetch_array($res1);
echo $chresult1['name']; //its not printing anything
if ($chkresult1) //it is storing null and entering into else block
{
    echo "query successful";
}
else {
    echo "query was not successful";
}

And the result is "query was not successful". I think everything is fine with my select query. Then why this mysql_fetch_array() is not fetching the data?

9
  • 1
    Is the email value you are searching for really supposed to have periods in front and behind it? [email protected]. is what you say the query is printing out, however do you mean [email protected]? Just curious. Commented Jan 29, 2015 at 18:16
  • 2
    A closing bracket (}) is missing before else. Commented Jan 29, 2015 at 18:17
  • really? I think not because if I miss } before else means i'll get another error saying something like "if without braces" its just my typing mistake @A L Commented Jan 29, 2015 at 18:22
  • @subramanyemm If you look at your code above, when you start if($chkresult1){ you have an opening brace but not a closing brace before else. This can cause unexpected results if this is really how you are executing your code. @A.L was just trying to point that out. Commented Jan 29, 2015 at 18:24
  • 1
    WARNING: mysql_query is an obsolete interface and should not be used in new applications as it's being removed in future versions of PHP. A modern replacement like PDO is not hard to learn. If you're new to PHP, a guide like PHP The Right Way can help explain best practices. Commented Jan 29, 2015 at 18:47

1 Answer 1

3

In $chkname change to email = $name (remove dot) or email = '$name'

And you are using mysql ,rather use mysqli or pdo sql

Sign up to request clarification or add additional context in comments.

5 Comments

Add a single quote ( ' ) near $name
I did same operation with mysql() only for another database table its worked fine without any error, I think mysqli() is not a problem
But mysql is deprecated. @Darshan Jain just gave an advice for you.
yeah its just deprecated not completely deleted we can use it right? @Darshan
Suppose you are driving your boss's car , he says dont start the music bcoz it is not pleasant . Would you start it ? Its the same situation php says dont use . So dont use !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.