$results = mysql_query("select * from doctorlist where assignednumber = '1231231234' ");
I need to change the number 1231231234 to a variable. If I change it to the code below it does not work. I have displayed the variable on the page so I know it is set.
$results = mysql_query("select * from doctorlist where assignednumber = '$phoneNumber' ");
Could someone please help. I know it is a small issue, but have been unable to fix it.
Perhaps split it like this
$sql_query = "select * from doctorlist where assignednumber='$phoneNumber'";
$results = mysql_query($sql_query);
or
$sql_query = "select * from doctorlist where assignednumber='".$phoneNumber."' ";
$results = mysql_query($sql_query);
First check your variable type with var_dump($phoneNumber) than do the following:
$results = mysql_query("select * from doctorlist where assignednumber = '".$phoneNumber."' ");
to improve readability and last if you expect an Integer cast your variable like:
(int)$phoneNumber
or if string do
mysql_real_escape_string($phoneNumber)
Try using the variable inside the query like this:
'{$phoneNumber}'
Related
while executing the below query i'm not getting sucess value
$name=qwe;
$result = mysql_query("SELECT *FROM USER WHERE name = $name");
Instead of above query if i put the below, I can able to get the appropriate ans:
$result = mysql_query("SELECT *FROM USER WHERE name = 'qwe'");
Can anyone give a solution for my first query??
Close the variable in quote and concatenate the string into query
$name= "qwe";
$result = mysql_query("SELECT *FROM USER WHERE name = '".$name."'");
one more note
please use mysqli for security purposes. Thanks
Use the following:
$result = mysql_query("SELECT *FROM USER WHERE name = '".$name."'");
P.S. I have not tested the above code but it should work.
select * from table_name where col_name = '".$variable."'
Directly use :
$name = 'qwe';
$result = mysql_query("SELECT * FROM USER WHERE name = '$name'");
And on more advise, you should never use mysql_* functions now as they have been deprecated in newer versions of PHP and will produce the notices.
The following works with no problem at all, when the photoId is directly on the statement and not a variable.
$img_query = mysqli_query($con, 'SELECT * FROM imgs WHERE photoid = "103"') or die(mysqli_error($con));
but the following just won't work with no error, what might be causing this not to select.
$imageid = '103';
$img_query = mysqli_query($con, 'SELECT * FROM imgs WHERE photoid = "$imageid"') or die(mysqli_error($con));
$img_row = mysqli_fetch_array($img_query);
echo $img_row['img'];
This is inside a while loop.
while($row = mysqli_fetch_array($somequery)){
$imageid = $row['photoid'];
$img_query = mysqli_query($con, 'SELECT * FROM imgs WHERE photoid = "$imageid"') or die(mysqli_error($con));
$img_row = mysqli_fetch_array($img_query);
echo $img_row['img'];
}
Thanks.
in php a ' and a " are very different and the query syntax is double quote around the query and single quote around variables.. although I would recommend you look at using parameters on your query instead of just putting a variable directly into the query
Per my recommendation you should change your query to this:
$imageid = '103';
$query = $con->prepare("SELECT * FROM imgs WHERE photoid = ?");
$query->bind_param('sssd', $imageid);
$query->execute();
this is just the nuts and bolts of it... if you want more information about the connection.. error handling and everything else read the DOCS
there is a big difference between ' and " in php
Differences
change your query to be
$img_query = mysqli_query($con, "SELECT * FROM imgs WHERE photoid = '$imageid'") or die(mysqli_error($con));
and it should work.
I've got the following query in an existing script - but it's not always returning a value even though it should based off what's in the database. There are plenty of things in the database it SHOULD be grabbing - they are there.
Don't see anything wrong with it - but I barely do this anymore :) See anything?
$query = "SELECT id FROM xtags WHERE tag_id = '$tagid' ORDER BY RAND() Limit 2";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$query = "SELECT * FROM xtable WHERE id = '$row[id]'";
$result = mysql_query($query) or die(mysql_error());
$row2 = mysql_fetch_assoc($result);
echo $row2[title];
}
$result is being used inside the loop and outside, try making a new variable inside and not reusing the outside one.
You're reusing the $result variable inside the loop which overwrites the value for use in the while condition. Use a different name for $query and $result inside the loop.
I don't know if is ok, but you are using $result twice, one before the "while" and another inside the "while".
I would personally split the string and the variable $row.
Why not use var_dump() to see $row and the other variables ???
I don't understand what you are trying to do actually, but try this:
$query = "SELECT id FROM xtags WHERE tag_id = '".$tagid."' ORDER BY RAND() Limit 2";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$query2 = "SELECT * FROM xtable WHERE id = '".intval($row[id])."'";
$result2 = mysql_query($query2) or die(mysql_error());
$row2 = mysql_fetch_assoc($result2);
echo $row2[title];
}
Problem solved - did a join statement.
I'm having troubles with a PHP code. The problems come when I execute a SQL query with a PHP variable inside it. The result doesn't shows anything, and the field in the database is not empty. Tried out with a static id (not variable) and the query works fine.
Do you know where I'm doing it wrong?
The query code:
$consultaVideoSeleccionado1 = mysql_query("SELECT * FROM videos WHERE idvideo = `'.$videoSeleccionado.'`;", $conexion);
Try this:
$consultaVideoSeleccionado1 = mysql_query("SELECT * FROM videos WHERE idvideo = `'.$videoSeleccionado.'`;", $conexion) or die(mysql_error());
That will give you an error message.
The problem is that you use both ` and ' as escape characters as the same time.
$consultaVideoSeleccionado1 = mysql_query("SELECT * FROM videos WHERE idvideo = '$videoSeleccionado';", $conexion);
should work.
Often things will be more clear when you echo the query so you can see the final result.
you are using double quotes on your query so there is no need for the dot . operator
$consultaVideoSeleccionado1 = mysql_query("SELECT * FROM videos WHERE idvideo = '$videoSeleccionado'", $conexion);
You are connecting string wrong! You are using ' single quote, but you should use double/none.
Try this query:
mysql_query("SELECT * FROM `videos` WHERE `idvideo` = '$videoSeleccionado';", $conexion)
Or
mysql_query("SELECT * FROM `videos` WHERE `idvideo` = '{$videoSeleccionado}';", $conexion)
Or
$q = "SELECT * FROM `videos` WHERE `idvideo` = '%s';";
mysql_query(sprintf($q, $videoSeleccionado), $conexion)
Edit:
If it's still not working problem can be in query, try checking if it is using mysql_error()(1) or try dumping query(2).
Example(1):
mysql_query("SELECT * FROM `videos` WHERE `idvideo` = '".$videoSeleccionado."';", $conexion) or die(mysql_error());
Example(2):
$q = "SELECT * FROM videos WHERE idvideo = '$videoSeleccionado';";
var_dump($q);
mysql_query($q, $conexion)
The following fails:
$result = mysql_query("SELECT * FROM Tasks WHERE UserID = '$_SESSION['userID']'");
I tried the following:
$userID = $_SESSION['userID'];
$result = mysql_query("SELECT * FROM Tasks WHERE UserID = '$userID'");
and it works. Is there a way to do this without making a separate variable?
Thanks!
Or like this:
$result = mysql_query("SELECT * FROM Tasks WHERE UserID = '{$_SESSION['userID']}'");
$result = mysql_query("SELECT * FROM Tasks WHERE UserID = '".$_SESSION['userID']."'");
or
$result = mysql_query("SELECT * FROM Tasks WHERE UserID = '{$_SESSION['userID']}'");
worth noting it would recommend the first one because it gets easier to read/find when you use a php editor, which in return makes it easier to debugg
Your first chokes the query, because you're actually commanding WHERE userID is '$_SESSION['. Not to mentions that rest which is userID']}' will be interpreted as a syntax error by MySQL.
Yes, like this
$result = mysql_query("SELECT * FROM Tasks WHERE UserID = '$_SESSION[userID]'");