Putting 2 WHERE statements together - php

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

Related

how can i use two mysql query with user defined variable in php

select #min_price:=min(prd_sale_price),#max_price:=max(prd_sale_price) from ct_product;
select * from ct_product where prd_sale_price=#min_price or prd_sale_price=#max_price;
This query works in mysql console
but
$query = "
select #min_price:=min(prd_sale_price),#max_price:=max(prd_sale_price) from ct_product;
select * from ct_product where prd_sale_price=#min_price or prd_sale_price=#max_price;
";
$result = mysql_query($query);
this code raise error in php
so, I tried this
$query="
select * from ct_product, (select #min_price:=min(prd_sale_price),#max_price:=max (prd_sale_price) from ct_product) as b
where prd_sale_price=#min_price or prd_sale_price=#max_price
";
$result = mysql_query($query);
that works
...
$query = "
select #min_price:=min(prd_sale_price),#max_price:=max(prd_sale_price) from ct_product;
select * from ct_product where prd_sale_price=#min_price or prd_sale_price=#max_price;
";
$result = mysql_query($query);
What's the way that this code would work well without modification as my second way?
Use two calls to mysql_query:
$query1 = "select #min_price:=min(prd_sale_price),#max_price:=max(prd_sale_price) from ct_product";
$query2 = "select * from ct_product where prd_sale_price=#min_price or prd_sale_price=#max_price";
mysql_query($query1);
mysql_query($query2);
Variables are associated with a database connection, so they'll persist between the calls.
in PHP mysql_query() can handle only one query at a time
You can't make this method handle 2 query at the same time
what I can suggest is using mysql_query() for every query

Use Query Result Row in Another Query

I'm trying to use the row from a query in another query.
This query correctly displays the username of the user currently signed in:
$param = $fgmembersite->UserEmail();
$query = mysqli_query($con, "SELECT username FROM Users1 WHERE email = '$param'
");
while ($row = mysqli_fetch_array($query)){
echo $row['username'] ;
}
I'm trying to find a way to use $row['username'] in another query something like...
$sql = mysqli_query($con, "SELECT * FROM messages WHERE to_user = '" . $row['username'] . "' ");
This doesn't give me a coding error, but it doesn't work. The username row obviously can't be taken from a separate query the way I'm attempting.
I have tried every combination I can think of but nothing has worked for me. Any help greatly appreciated.
Try a subquery
SELECT * FROM messages WHERE to_user in (SELECT username FROM Users1 WHERE email = '$param')
You can join the queries into one:
SELECT `messages`.*
FROM `messages`
JOIN `Users1`
ON `Users1`.`username`=`messages`.`to_user`
WHERE
`Users1`.`email`='$param'
Well you should place your second query inside while:
while ($row = mysqli_fetch_array($query)){
echo $row['username'] ;
$sql = mysqli_query($con, "SELECT * FROM messages WHERE to_user =
'" . $row['username'] . "' ");
}
Now loop ends when mysqli_fetch_arrray returns NULL and that NULL you are trying to insert into second query.
You can combine the two queries into one.
SELECT * FROM messages WHERE to_user = (SELECT username FROM Users1 WHERE email = '$param')

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

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)

PHP nesting quotes

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

Categories