Jump to content

Recommended Posts

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);

 

 

Link to comment
https://forums.phpfreaks.com/topic/148541-solved-if-not-found-in-db-do-this/
Share on other sites

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).

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

$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

<?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

 

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();
?>

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");}

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");}

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");}

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.