dadamssg Posted March 9, 2009 Share Posted March 9, 2009 im getting a number via $_GET and putting it in a query and displaying results in a table. I want to be able to send a "Post not found" message or send the user to another page if they type in a number not found in the db. right now if i type in a # thats not found it just displays an empty table. heres the part of the script $postid = $_GET["id"]; $cxn = mysqli_connect($host,$user,$passwd,$dbname) or die ("Couldn't connect"); $quer = "SELECT * FROM test WHERE eventid = $postid"; $rsult = mysqli_query($cxn,$quer) or die ("Couldn't execute"); $row = mysqli_fetch_assoc($rsult); Quote Link to comment https://forums.phpfreaks.com/topic/148541-solved-if-not-found-in-db-do-this/ Share on other sites More sharing options...
corbin Posted March 9, 2009 Share Posted March 9, 2009 Hrmmm does MySQLi have a num_rows() function? *googles* Edit: http://php.net/manual/en/mysqli-stmt.num-rows.php Hrmmmm >.< Perhaps theres an easier way. I'm not very familiar with MySQLi (I've used it through DB classes and OOP style, but never directly procedurally). Quote Link to comment https://forums.phpfreaks.com/topic/148541-solved-if-not-found-in-db-do-this/#findComment-780024 Share on other sites More sharing options...
dadamssg Posted March 9, 2009 Author Share Posted March 9, 2009 right right...forgot about that. heres what i got but i have an error Fatal error: Can't use function return value in write context in /homepublic_html/test/work.php on line 21 $rsult = mysqli_query($cxn,$quer) or die ("Couldn't execute"); if(num_rows($rsult)=0){header("Location: http://www.mysite.com");} $row = mysqli_fetch_assoc($rsult); and line 21 is the if statement Quote Link to comment https://forums.phpfreaks.com/topic/148541-solved-if-not-found-in-db-do-this/#findComment-780027 Share on other sites More sharing options...
Maq Posted March 9, 2009 Share Posted March 9, 2009 if(num_rows($rsult)==0) NOT if(num_rows($rsult)=0) Quote Link to comment https://forums.phpfreaks.com/topic/148541-solved-if-not-found-in-db-do-this/#findComment-780029 Share on other sites More sharing options...
dadamssg Posted March 9, 2009 Author Share Posted March 9, 2009 $quer = "SELECT * FROM test WHERE eventid = $postid"; $rsult = mysqli_query($cxn,$quer) or die ("Couldn't execute"); if(num_rows($rsult)==0){header("Location: http://www.mysite.com");} $row = mysqli_fetch_assoc($rsult); gets Fatal error: Call to undefined function num_rows() on line 21 Quote Link to comment https://forums.phpfreaks.com/topic/148541-solved-if-not-found-in-db-do-this/#findComment-780033 Share on other sites More sharing options...
Maq Posted March 9, 2009 Share Posted March 9, 2009 You're using the wrong function you need to use: mysqli_stmt_num_rows Quote Link to comment https://forums.phpfreaks.com/topic/148541-solved-if-not-found-in-db-do-this/#findComment-780036 Share on other sites More sharing options...
dadamssg Posted March 9, 2009 Author Share Posted March 9, 2009 <?php session_start(); $postid = $_GET["id"]; if (! ctype_digit($postid)) { header("Location: http://www.mysite.com"); } include("caneck.inc"); $postid = $_GET["id"]; $cxn = mysqli_connect($host,$user,$passwd,$dbname) or die ("Couldn't connect"); $quer = "SELECT * FROM test WHERE eventid = $postid"; $rsult = mysqli_query($cxn,$quer) or die ("Couldn't execute"); if(mysqli_stmt_num_rows($rsult)==0){header("Location: http://www.mysite.com");} $row = mysqli_fetch_assoc($rsult); ?> this is what i have at the very top and im getting Warning: mysqli_stmt_num_rows() expects parameter 1 to be mysqli_stmt, object given on line 21 Warning: Cannot modify header information - headers already sent by (output started at :21) on line 21 boo Quote Link to comment https://forums.phpfreaks.com/topic/148541-solved-if-not-found-in-db-do-this/#findComment-780082 Share on other sites More sharing options...
Philip Posted March 9, 2009 Share Posted March 9, 2009 Personally, I use mysqli object orientated style.... <?php session_start(); $postid = $_GET["id"]; if(!ctype_digit($postid)) { header("Location: http://www.mysite.com"); } include("caneck.inc"); $cxn = mysqli_connect($host,$user,$passwd,$dbname) or die ("Couldn't connect"); $quer = "SELECT * FROM test WHERE eventid = $postid LIMIT 1"; // Run query $rsult = $cxn->query($quer) or die ("Couldn't execute: ".$cxn->error); // cant recall if the following line is needed in this case. weird times that it is needed $rsult->store_result(); // check row count if($rsult->num_rows==0){header("Location: http://www.mysite.com");} // fetch the row $row = $rsult->fetch_assoc(); $rsult->free_result(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/148541-solved-if-not-found-in-db-do-this/#findComment-780094 Share on other sites More sharing options...
dadamssg Posted March 9, 2009 Author Share Posted March 9, 2009 hmm im not familiar with that method...i tried this and it never sends me to the homepage like i want if the postid isn't in the db. if the number doesn't exist in the db it just outputs a table with no variables in it. <?php session_start(); $postid = $_GET["id"]; if (! ctype_digit($postid)) { header("Location: http://www.mysite.com"); } include("caneck.inc"); $postid = $_GET["id"]; $cxn = mysqli_connect($host,$user,$passwd,$dbname) or die ("Couldn't connect"); $quer = "SELECT * FROM test WHERE eventid = $postid"; $rsult = mysqli_query($cxn,$quer) or die ("Couldn't execute"); $found = mysqli_num_rows($rsult); //check if postid exist, if not send to homepage if ($found < 0) {header("Location: http://www.mysite.com");} Quote Link to comment https://forums.phpfreaks.com/topic/148541-solved-if-not-found-in-db-do-this/#findComment-780100 Share on other sites More sharing options...
dadamssg Posted March 9, 2009 Author Share Posted March 9, 2009 figured it out...just added an else statement with the first if part blank...dunno why that worked but it does $found = mysqli_num_rows($rsult); //check if postid exist, if not send to homepage if ($found > 0) {} else {header("Location: http://www.mysite.com");} Quote Link to comment https://forums.phpfreaks.com/topic/148541-solved-if-not-found-in-db-do-this/#findComment-780104 Share on other sites More sharing options...
Maq Posted March 9, 2009 Share Posted March 9, 2009 Yeah it's because in the code you showed before you had an error here: ( extra '}' ) if(mysqli_stmt_num_rows($rsult)==0){header("Location: http://www.mysite.com");} To shorten your code, you can use: if ($found == 0) header("Location: http://www.mysite.com"); Instead of: if ($found > 0) {} else {header("Location: http://www.mysite.com");} Quote Link to comment https://forums.phpfreaks.com/topic/148541-solved-if-not-found-in-db-do-this/#findComment-780105 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.