Jump to content

Trouble with mysql_num_rows


SF23103
Go to solution Solved by requinix,

Recommended Posts

Hello,

 

I created a page that gets some information from a MySQL database and displays it on a page.  The information is selected based on the ID of the row in the database.  So, if ...script.php?id=55 it will display the information from submission ID #55.  All of that works great, but I am having difficulty sending it to an error page if someone manually enters an ID number that does not exist.  For instance script.php?id=7777

 

Right now I am using 

if (mysql_num_rows($result)==0) { die (include "/path/to/public_html/errors/no_classes_selected.php"); } 

Unfortunately it ALWAYS includes that error page, as it seems $result is always equal to 0 even if the ID number is a real entry.  For instance, if I take that line out, and an ID that exists is entered, it displays the data fine.  If I leave that code in and I use the same ID, it displays the error page.

 

Am I doing this right?

<?php 
// Get required login info
include "/path/to/login_info.php";

$db = new mysqli('localhost', $username, $password, $database); // Connect to DB using required login info
if($db->connect_errno > 0){
    die('Unable to connect to database [' . $db->connect_error . ']');
}

unset($username);// put these variables back to null
unset($password);// put these variables back to null
unset($database);// put these variables back to null

date_default_timezone_set('America/Los_Angeles'); // set default time zone PST

// ************GET ID FROM URL***********************
$id = (int)$_GET['id']; // this gets the id from the url
if($id != '' && $id > 0) {  // this checks to make sure that the ID is an integer
// **************************************************

$sql = <<<SQL
SELECT ft_form_8.*,ft_form_7.*
FROM ft_form_7
LEFT JOIN ft_form_8 ON ft_form_8.Instructor=ft_form_7.Instructor
WHERE ft_form_7.submission_id=$id
SQL;

if(!$result = $db->query($sql)){ // if there is an error in running the query, show error message.
    die('There was an error running the query [' . $db->error . ']');
}


if (mysql_num_rows($result)==0) { die (include "/path/to/public_html/apps/errors/no_classes_selected.php"); }

while($row = $result->fetch_assoc()){
	// Get start date information
	$start_date = $row['class_start_date']; // Get event_start_date for conversion and call it $start_date
	$start_date_formatted = date("l M d, Y", strtotime($start_date)); // Convert start_date 
	$end_date = $row['class_end_date']; // Get event_end_date for conversion and call it $start_date
	$end_date_formatted = date("M d, Y", strtotime($end_date)); // Convert start_date
	
	// Do above for start and end date two for multiple day classes (ex. jan 1-2 and feb 2-3)
	$start_date_2 = $row['class_start_date_2'];
	$start_date_2_formatted = date("l M d, Y", strtotime($start_date_2)); 
	$end_date_2 = $row['class_end_date_2'];
	$end_date_2_formatted = date("M d, Y", strtotime($end_date_2));
	
	// Get time information.
	$start_time = $row['class_start_time']; // Get event_start_time for conversion and call it $start_time
	$start_time_formatted = date("h:i A", strtotime($start_time)); // Convert start_time 
	$end_time = $row['class_end_time']; // Get event_end_time for conversion and call it $end_time
	$end_time_formatted = date("h:i A", strtotime($end_time)); // Convert end_time 
	
		// echo information...
	    echo "<h2>" , $row['class_name'],"</h2>" ; // echo event name

	echo "<p><strong>",$start_date_formatted; // echo the start date
	if (empty($end_date) or $end_date = $start_date) { echo '<br/>'; } else { echo " - ","", $end_date_formatted , "<br />"; } // echo end date
	
	if (empty($start_date_2)) {echo '';} else { echo $start_date_2_formatted; } // echo the start date
	if (empty($end_date_2) or $end_date_2 = $start_date_2) { echo ''; } else { echo " - ","", $end_date_2_formatted , "<br />"; } // echo end date
	
		if (empty($start_time)) { echo ''; } else { echo $start_time; } // echo start time
		if (empty($end_time)) { echo ')'; } else { echo " - ", $end_time; } // echo end time
		
	// if there is no start time, echo nothing. (otherwise it seems to echo 4pm). If it does contain a time, echo the time.
	echo "</strong>";
	
	$chef_full_name = $row['Instructor'];
	$chef_id = $row['submission_id'];
	$full_bio = $row['full_bio'];
	echo "<div class=\"showbio\">" , "<div class=\"underline\">" , $chef_full_name , "</div>";
	echo "<div class=\"bio\" style=\"display: none;\">";
	echo $full_bio , "</div></div><br />";
	echo $row['class_description'], "<br />";
	echo "<strong>" , $row['type'], " - Cost: $",$row['cost'] , " - #" , $row['course_number'] , " - <a href=\"registration.php\" target=\"_blank\"> (Register Online)</a> </strong><br />" , "</p>"; // echo class type and cost
}

$db->close();
$result->free();

}

else {
   die (include "/path/to/public_html/apps/errors/no_id.php"); //display error if ID is not an integer
}


?>
Link to comment
Share on other sites

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.