Jump to content

[SOLVED] Using Try...Catch to eliminate an error


elite_prodigy

Recommended Posts

I'm using the following code to test if a "site" exists in the database.

 

<?php

//...

$sql = "SELECT * FROM `sites` WHERE `site`=$site;";
$result = mysql_query($sql,$conn);

$rows = mysql_num_rows($result);

if($rows > 0){


if($_SESSION['count'] < 1){

  $page = "home";
  $_SESSION['count'] = $_SESSION['count']++;

}

}
else{

$page = "sign";

}

//...

?>

 

The above code does a couple things. It queries the data I need from the database and uses mysql_num_rows() to see if at least one row exists (I'll add a clause later for when more than one row exists) because if there is a row then the site is there.

 

I'm getting the desired result, but a warning is generated when no site exists because mysql_num_rows() returns an error when there is nothing to query. I need to catch the warning so that it is not displayed to the screen.

 

I thinking a try/catch statement but cannot for the life of me think of how to write it.

 

Can anyone help?

Link to comment
Share on other sites

<?php

$sql = "SELECT * FROM `sites` WHERE `site`=$site;";
if ($result = mysql_query($sql,$conn)) {
  if (mysql_num_rows($result)) {
    if ($_SESSION['count'] < 1){
      $page = "home";
      $_SESSION['count'] = $_SESSION['count']++;
    }
  }
} else {
  $page = "sign";
}

?>

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.