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=\'amit@gmail.com\' ';

 

[attachment deleted by admin]

Link to comment
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";

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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

 

 

vinu@gmail.com

SELECT * FROM contacts WHERE umail =vinu@gmail.com

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

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.