Jump to content
Sign in to follow this  
westman

using OR in MySQLi

Recommended Posts

how do I use "OR" in MySQLi?

Here is what I have

$i = 0;
$Db1 = "1";
$Db2 = "2";
$stmt = $conn->prepare("SELECT id, user, title, date, show_post FROM post WHERE show_post = ? ORDER BY id DESC");
$stmt->bind_param("s", $Db1);
$stmt->execute();
$stmt->store_result();
//if($stmt->num_rows === 0) exit('No rows');
$stmt->bind_result($idRow, $userRow, $titleRow, $dateRow, $showRow);
while ($stmt->fetch()) {
$post_id[] = $idRow;
$user[] = $userRow;
$title[] = $titleRow;
$date[] = $dateRow;
$post_show[] = $showRow;
$i++;
// run a while loop
}
$stmt->close();
$conn->close();

I would like to use OR to look for
$Db1 = "1";
$Db2 = "2";
in my DB.
How do I use OR?

 

Share this post


Link to post
Share on other sites

This is how the OR operator is used in SQL:

$stmt = $conn->prepare("SELECT id, user, title, date, show_post FROM post WHERE show_post = ? OR show_post = ? ORDER BY id DESC");
$stmt->bind_param("ss", $Db1, $Db2);

 

Share this post


Link to post
Share on other sites

as I would like to use AND and OR together, would this work?

$Db1 = "1";
$Db2 = "2";
$Dbseen = "yes";

$stmt = $conn->prepare("SELECT id, user, title, date, show_post FROM post WHERE show_post = ? AND seen = ? OR show_post = ? AND seen = ? ORDER BY id DESC");
$stmt->bind_param("sss", $Db1, $Dbseen, $Db2);

 

Share this post


Link to post
Share on other sites

No, that won't work. You have four placeholders but only three values.

Share this post


Link to post
Share on other sites

then, would this work?

$Db1 = "1";
$Db2 = "2";
$Dbseen = "yes";

$stmt = $conn->prepare("SELECT id, user, title, date, show_post FROM post WHERE show_post = ? AND seen = ? OR show_post = ? AND seen = ? ORDER BY id DESC");
$stmt->bind_param("ssss", $Db1, $Dbseen, $Db2, $Dbseen);

???

Share this post


Link to post
Share on other sites

It will function without throwing any errors, but I can't tell you if it meets your requirements because I don't know what your requirements are.

I would recommend that you run the code for yourself, testing it with different values, and see whether it gives you the results you want it to.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×