westman Posted March 20, 2018 Share Posted March 20, 2018 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? Link to comment Share on other sites More sharing options...
Ingolme Posted March 20, 2018 Share Posted March 20, 2018 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); Link to comment Share on other sites More sharing options...
westman Posted March 20, 2018 Author Share Posted March 20, 2018 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); Link to comment Share on other sites More sharing options...
Ingolme Posted March 20, 2018 Share Posted March 20, 2018 No, that won't work. You have four placeholders but only three values. Link to comment Share on other sites More sharing options...
westman Posted March 20, 2018 Author Share Posted March 20, 2018 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); ??? Link to comment Share on other sites More sharing options...
Ingolme Posted March 20, 2018 Share Posted March 20, 2018 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. Link to comment Share on other sites More sharing options...
westman Posted March 20, 2018 Author Share Posted March 20, 2018 It works very well. thank you so so so so much Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now