PHP nesting quotes - php

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]'");

Related

How to set a variable in where clause of Select query in php

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.

Issue with mysql query when changing value to a variable

$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}'

Several while statements for mysql_fetch_array causing unexpected errors

I am having a problem with the while function for a mysql_fetch_array. I have experimented on what to use after the statement and what I have now works better than it did before. I thought i could just run a load of loops inside each other but clearly not. I currently have curly brackets on the first two statements and none on the others, you can see this clearly in the code.
However, what i have now means that having more than one variable after each statement causes the second one to stop working when echoed etc. I am trying to avoid using arrays as variables would be a lot easier to lay out afterwards. Not sure what's going on here. I normally use curly brackets after every statement but that just made the whole thing redundant. What should I do to keep all the variables working? I am not great with PHP yet and thanks for all the help so far!
I am just having a mess around for future purposes so I know i should be using mysqli. I have only recently learnt mysqli so I was just using mysql because i feel more comfortable with it for the time being.
Here is the code anyway:
//fetch favourited artist(s)
$fetchartistFavourite = mysql_query("SELECT * FROM artistfavourites WHERE username = '$username' AND password = '$pass';")or die(mysql_error());
while ($artistFavourite = mysql_fetch_array($fetchartistFavourite)){
$favouritedArtist = $artistFavourite['artistname'];
$favouritedArtistUrl = $artistFavourite['artisturl'];
//fetch favourite track(s)
$fetchtrackFavourite = mysql_query ("SELECT * FROM trackfavourites WHERE username = '$username' AND password = '$pass'")or die(mysql_error());
while ($trackFavourite = mysql_fetch_array($fetchtrackFavourite)){
$favouritedTrack = $trackFavourite['artistname'];
$favouritedTrackUrl = $trackFavourite['artisturl'];
//Get news from favourited artist(s)
//Get updates to bio
$fetchupdatedBio = mysql_query ("SELECT * FROM members WHERE artistname = '$favouritedArtist'")or die(mysql_error());
while ($updatedBio = mysql_fetch_array($fetchupdatedBio))
$updatedBio = $updatedBio['bio'];
//Get updates to profile pic
$fetchupdatedProfile = mysql_query ("SELECT * FROM members WHERE artistname = '$favouritedArtist'")or die(mysql_error());
while ($updatedProfile = mysql_fetch_array($fetchupdatedProfile))
$updatedProfile = $updatedProfile ['image1'];
//Get any new pictures
$fetchPic = mysql_query ("SELECT * FROM pictures WHERE artistname = '$favouritedArtist'")or die(mysql_error());
while ($pic = mysql_fetch_array($fetchPic))
$pic = $pic['picurl'];
//Get any new tracks
$fetchTracks = mysql_query ("SELECT * FROM tracks WHERE artistname = '$favouritedArtist'")or die(mysql_error());
while ($tracks = mysql_fetch_array($fetchTracks))
$trackurl = $tracks['trackurl'];
$trackname = $tracks['trackname'];
//Get any new gigs
$fetchGigs = mysql_query ("SELECT * FROM gigs WHERE artistname = '$favouritedArtist'")or die(mysql_error());
while ($gigs = mysql_fetch_array($fetchGigs))
//arrange gig data into format to be echoed
$gig = $favouritedArtist.' is playing for the gig ' .$gigs['gigname'].' at ' .$gigs['venue'].' on the '.$gigs['day'].'th of '.$gigs['month'].', '.$gigs['year'];
//Get any new sessions
$fetchSessions = mysql_query ("SELECT * FROM sessions WHERE artistname = '$favouritedArtist'")or die(mysql_error());
while ($sessions = mysql_fetch_array($fetchSessions))
$sessionName = $sessions ['title'];
//Get new tracks from favourited tracks(s)if the artist has not been favourited
$fetchnewTrack = mysql_query ("SELECT * FROM tracks WHERE artistname = '$favouritedTrack' AND artistname !='$favouritedArtist'")or die(mysql_error());
while ($newTrack = mysql_fetch_array($fetchnewTrack))
$trackname2 = $newTrack['trackname'];
//asign all variables into an
echo $trackname;
}
}
First of all, you should definitely try not to SELECT *, but just the content you need.
Like :
SELECT picurl FROM pictures WHERE artistname = '$favouritedArtist'
instead of
SELECT * FROM pictures WHERE artistname = '$favouritedArtist'
In your :
while ($tracks = mysql_fetch_array($fetchTracks))
$trackurl = $tracks['trackurl'];
$trackname = $tracks['trackname'];
There is an error, because you don't need brackets only when there is a single instruction after the while statement.
Idem with your
while ($sessions = mysql_fetch_array($fetchSessions))
with no brackets, you can't do so if there is more than one instruction related to the while.
While are only needed when you know there will be multiple answers in you MySQL request. Since the might be only one user with this username, you don't need a while.
All of this are basics of php and mysql development, a simple google search would have given you the answer.
I think you might need to read some more tutorials on basics of php and mysql.

Putting 2 WHERE statements together

I'm having problems putting two where statements together
My current code:
include 'config.php';
$result22 = mysql_query("SELECT * FROM messages WHERE to_user='".$_SESSION['username']."' AND to_read_yet='yes' ");
$num_rows22 = mysql_num_rows($result22);
echo "$num_rows22 ";
For some reason this isn't working. I am not getting any results i have checked the db and there are results which should come out
You should read about sql syntax. After where you put any number conditions with bool operators. Using 2 times where is incorrect
$result = mysql_query("SELECT * FROM messages WHERE to_user='".$_SESSION['username']."' AND to_read_yet='"no"' ");
Leave the second WHERE expression out, it's just WHERE condition_1 AND condition_2 AND condition_3 AND ....
$result = mysql_query("SELECT * FROM messages WHERE to_user='".$_SESSION['username']."' AND to_read_yet='"no"' ");
SELECT *
FROM messages
WHERE to_user = '".$_SESSION['username']."'
AND to_read_yet= '"no"'
try with:
$result = mysql_query("SELECT * FROM messages WHERE to_user='".$_SESSION['username']."' AND to_read_yet='no' ");
$result = mysql_query( "SELECT * FROM messages WHERE to_user='" . $_SESSION['username'] . "' AND to_read_yet='no'" );
Drop the second WHERE

Empty query result in php

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)

Categories