0

New to PHP and MySQL, have heard amazing things about this website from Leo Laporte and others. I am using a extremely basic PHP script to search a MySQL database of peoples names. I was wondering how I can have the search supply an error if there no one by that name in the database.

</p><?php

mysql_connect ("mysql.mydomain.us", "pnq_1","passwordremoved")  or die (mysql_error());
mysql_select_db ("pnq_soldiers");

$term = $_POST['term'];

$sql = mysql_query("select * from test_database where LN like '%$term%'");

while ($row = mysql_fetch_array($sql)){
    echo 'Last Name: '.$row['LN'];
    echo '<br/> First Name: '.$row['FN'];
    echo '<br/> Middle Initial: '.$row['MI'];
    echo '<br/> Rank: '.$row['RNK'];
    echo '<br/> Company: '.$row['CO'];
    echo '<br/> Platoon: '.$row['PL'];
    echo '<br/> Roster Number: '.$row['RN'];
    echo '<br/><br/>';
    }


?>
3
  • 3
    For reference, mysql_query has been obsolete for years now. Check out PDO and/or mysqli. Commented Jun 11, 2012 at 23:00
  • Consider using PDO. Commented Jun 11, 2012 at 23:00
  • Oh, and you're leaving yourself vulnerable to SQL injection. If you insist on using mysql functions and/or building SQL by hand, learn to love mysql_real_escape_string -- you'll be using it a lot. Ideally, though, you'll upgrade and learn to love prepared statements even more. :) Commented Jun 11, 2012 at 23:04

4 Answers 4

2

Check to see how many rows were returned. If no rows were returned, you can display an error.

$sql = mysql_query("select * from test_database where LN like '%$term%'");
if (mysql_num_rows($sql)) {
    // continue as normal
} else {
    echo "Error!";
}
Sign up to request clarification or add additional context in comments.

Comments

0

Keeping it simple... use mysql_num_rows()

</p><?php

mysql_connect ("mysql.mydomain.us", "pnq_1","passwordremoved")  or die (mysql_error());
mysql_select_db ("pnq_soldiers");

$term = $_POST['term'];

$sql = mysql_query("select * from test_database where LN like '%$term%'");

if(mysql_num_rows($sql) < 1) {

    echo 'Couldn\'t find anything for '.$term.'.';
}
else {

while ($row = mysql_fetch_array($sql)){
    echo 'Last Name: '.$row['LN'];
    echo '<br/> First Name: '.$row['FN'];
    echo '<br/> Middle Initial: '.$row['MI'];
    echo '<br/> Rank: '.$row['RNK'];
    echo '<br/> Company: '.$row['CO'];
    echo '<br/> Platoon: '.$row['PL'];
    echo '<br/> Roster Number: '.$row['RN'];
    echo '<br/><br/>';
    }
}


?>

1 Comment

Worked flawlessly. Thanks much!
0

Use mysql_num_rows to test the number of rows in the result. 0 for no results.

Comments

0
if (mysql_num_rows($sql)==0){
    //error
}else{
    //process data, while loop
}

and of course don't use mysql_ functions :-)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.