Jump to content

How to tell if there are no results returned give an error


ivalea

Recommended Posts

Hello,

I'm trying to pull customers first name and last name from mysql.  What I want to do is say if there is no result returned then either echo an error message or redirect to another page.  But what I have now , lets the user into the page whether they are found in the table or not. All I'm trying to do is get the customers first and last name but only if their email address is found in the students table.  Here is what I have:

[code]
$result = mysql_query("SELECT customers.customers_firstname, customers.customers_lastname from customers, students where students.students_email = customers.customers_email_address") or die (mysql_error());
if(!result){
echo 'None';
}else{
while ($row = mysql_num_rows($result))
{
$first .= $row[ 'customers_firstname' ];
$last .= $row[ 'customers_lastname' ];
}
}
[/code]

This returns a server error when running the script...  Any thoughts on how I can achive this correctly?  Thanks! :)
You need to test whether the number of rows returned by the function mysql_num_rows() is greater than 0.

Here's my take on your code:
[code]<?php
$q = "SELECT customers.customers_firstname, customers.customers_lastname from customers, students where students.students_email = customers.customers_email_address";
$result = mysql_query($q) or die ("Problem with the query: $q<br>" . mysql_error());
if(mysql_num_rows($result)) echo 'None';
else{
    while ($row = mysql_fetch_asssoc($result)) {
        $first .= $row[ 'customers_firstname' ];
        $last .= $row[ 'customers_lastname' ];
    }
}?>[/code]

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.