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! :)
Link to comment
Share on other sites

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