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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...