Jump to content

retrieving data from mysql


Amit20

Recommended Posts

Hi friends,

I m working on a code to retrieve data from mysql table.

I m using where clause i.e

 

$query= 'select * from contacts where umail=\'$_COOKIE["usern"]\' ';

 

 

You can see that i m trying to select records on the basis of cookie value.

The problem is i m unable to retrieve data as i guess  mysql is unable to understand the $_COOKIE value.

Or may be bcoz  of many single quote.

 

But then i tried in the following manner

 

$sql=stripslashes($query);

 

$result=mysql_query($sql) or die(mysql_error());

 

But neither i m getting an error nor my ode is working.

 

My code works when i replace $_COOKIE["usern"] variable with actual value i.e

 

$query= 'select * from contacts where umail=\'[email protected]\' ';

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/240685-retrieving-data-from-mysql/
Share on other sites

Try

$query = 'SELECT * FROM contacts WHERE umail = ' . $_COOKIE["usern"];

or

$query = "SELECT * FROM contacts WHERE umail = $_COOKIE['usern']";

 

and this is prolly what you should do (securing your query)

$usern = mysql_real_escape_string($_COOKIE["usern"]);
$query = "SELECT * FROM contacts WHERE umail = $usern";

Thanks,

 

But i tried using the quereies u suggested me.

 

Out of that the first 2 queries but they didnt work.

 

I tried third query and it resulted in mysql error i.e

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@gmail.com' at line 1.

 

 

I tried echoing the query!

 

Query is just fine but i still get the same error.

 

 

$usr = mysql_real_escape_string($_COOKIE["usern"]); 

echo $usr."<br/>";

$query = "SELECT * FROM contacts WHERE umail =". $usr;

echo $query."<br/>";

 

$result=mysql_query($query) or die(mysql_error());

 

//Output is as follow

 

 

[email protected]

SELECT * FROM contacts WHERE umail [email protected]

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@gmail.com' at line 1

 

 

No there is no space.

 

But thanks for your help.

 

I m able to solve it.

 

I just put the $usr variable in single quote.

 

Here it is:

 

$usr = mysql_real_escape_string($_COOKIE["usern"]);

 

echo $usr."<br/>";

 

$query = "SELECT * FROM contacts WHERE umail = '$usr'";

 

echo $query."<br/>";

 

Now i m getting the desired output.

 

Thanks alot to everyone.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.