-4

I am new to php so hopefully someone could point out where i am going wrong I have written a php code to fetch certain records from an MySQL database

I am running the query as

$result = mysql_query("SELECT * FROM Messages where Id='idLo'") 
or  die(mysql_error());

i get no results but when i hard code like

$result = mysql_query("SELECT * FROM Messages where Id='4'") 
or  die(mysql_error());

It returns all the data What am i misisng i am collecting idLo as a get parameter

$idLo = $_GET['id'];
2
  • 1
    Did you spend a few seconds searching? this has been answered so many times. Commented Nov 24, 2013 at 13:14
  • Yes i tried maybe i searched it wrongjust started coding on php today Coudnt realise this silly mistake All the answers below work fine Thanks for the help guys Commented Nov 24, 2013 at 13:25

7 Answers 7

1

Your code needs to change to

$result = mysql_query("SELECT * FROM Messages where Id='$idLo'") or  die(mysql_error());

From

$result = mysql_query("SELECT * FROM Messages where Id='idLo'") or  die(mysql_error());

There is basic thing that in php every variable has dollar( $ ) sign that we need to use every time while using it. 

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

Comments

1

Try this: $result = mysql_query("SELECT * FROM Messages where Id='$idLo'");

9 Comments

You are recommending using a depreacted function
@nrathaus, OP is welcome to use whatever function he wishes. This corrects his error.
And? He is clearly working with mysql_ functions. If I wrote mysqli_, it will not work, as he mysql_connected. Now remove that downvote.
Helping him use code that will soon stop working isn't a helpful answer even if it solves his problem "now"
I didn't give you any downvote, and even if I did, I still think your answer isn't good, yes he used mysql_query, so tell him DONT use it
|
1

you forgot $ in front of idLo in your query.

Comments

1
$result = mysql_query("SELECT * FROM Messages where Id='".$idLo."'");

should do it

Comments

1

It appears that you haven't included the "$" to signify that idlo is a variable:

where Id='idLo'

should be

where Id='$idLo'

Also, you might want to have a look into using PDO or Mysqli for accessing your mysql database through PHP.

Comments

0

you should user mysqli pr PDO by now. A bit better:

$result = $connection->query("SELECT * FROM Messages where Id='$id'");

also, check if Id is really with capital letters and add a dollar sign to the variable name.

Comments

0
$idLo = $_GET['id'];
$result = mysql_query("SELECT * FROM Messages where Id='$idLo'") or die(mysql_error());

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.