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='.subbu66g@gmail.com.' "
// 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?

  • 1
    Is the email value you are searching for really supposed to have periods in front and behind it? `.subbu66g@gmail.com.` is what you say the query is printing out, however do you mean `subbu66g@gmail.com`? Just curious. – Michael Lea Crawford Jan 29 '15 at 18:16
  • 2
    A closing bracket (**`}`**) is missing before `else`. – A.L Jan 29 '15 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 – subramanyemm Jan 29 '15 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. – Michael Lea Crawford Jan 29 '15 at 18:24
  • Oops, a *brace or curly bracket* is missing, not a *bracket*. @subramanyemm I'm sorry but I don't understand your comment. If it's a typing mistake then update your question and fix it. – A.L Jan 29 '15 at 18:25
  • 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](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/). If you're new to PHP, a guide like [PHP The Right Way](http://www.phptherightway.com/) can help explain best practices. – tadman Jan 29 '15 at 18:47
  • Your code is unsafe and open to SQL injection. Please read this page: http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – Andy Lester Jan 29 '15 at 19:16
  • thank you @Michael Lea Crawford.. i placed 'dots(.)' before and after $name. problem solved – subramanyemm Jan 29 '15 at 19:50
  • Do you mean you *removed* the dots? – A.L Jan 30 '15 at 12:38

1 Answers1

3

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

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

frunkad
  • 2,433
  • 1
  • 23
  • 35