Jump to content


Photo

Bypass Select statement when empty.


  • Please log in to reply
9 replies to this topic

#1 cyprus

cyprus
  • Members
  • PipPipPip
  • Advanced Member
  • 68 posts

Posted 14 September 2006 - 11:19 PM

How can I bypass the code below if my $fields and $where have not been executed yet? The first time the page appears its empty. Thanks

$username="root";
$password="";
$database="";
mysql_connect(localhost,$username);
$query = "SELECT $fields FROM ORDERS $where";
@mysql_select_db($database) or die( "Unable to select database");     
$result = mysql_query($query) or die('Problem with query: ' . $query . '<br>'. mysql_error());
$numrows=mysql_numrows($result);

#2 sford999

sford999
  • Members
  • PipPipPip
  • Advanced Member
  • 119 posts

Posted 15 September 2006 - 12:36 AM

You could use an if statement

<?php

$username="root";
$password="";
$database="";
mysql_connect(localhost,$username);

if($fields == "" || $where == "")
{
// Show the page when nothing is defined for $fields and $where
}
else
{
$query = "SELECT $fields FROM ORDERS $where";
@mysql_select_db($database) or die( "Unable to select database");     
$result = mysql_query($query) or die('Problem with query: ' . $query . '
'. mysql_error());
$numrows=mysql_numrows($result);
}

?>


#3 cyprus

cyprus
  • Members
  • PipPipPip
  • Advanced Member
  • 68 posts

Posted 15 September 2006 - 07:13 AM

Many many thanks, works a treat.

#4 JustinK101

JustinK101
  • Members
  • PipPipPip
  • Advanced Member
  • 503 posts
  • LocationSan Diego, California, US

Posted 15 September 2006 - 07:22 AM

Just curious, what does the @ mean in front of the mysql_select_db() call?

#5 markbett

markbett
  • Members
  • PipPipPip
  • Advanced Member
  • 133 posts

Posted 15 September 2006 - 07:25 AM

surpresses sql errors

#6 JustinK101

JustinK101
  • Members
  • PipPipPip
  • Advanced Member
  • 503 posts
  • LocationSan Diego, California, US

Posted 15 September 2006 - 07:32 AM

HUmm, so whats the point if he has the || die(mysql_error()) call. Inst it going to hit the or clause so whats the point of surpressing sql errors?

#7 markbett

markbett
  • Members
  • PipPipPip
  • Advanced Member
  • 133 posts

Posted 15 September 2006 - 08:33 AM

The mysql_select_db( ) function uses the @ to suppress the default error message the ordie that is listed is not well thought out as it returns what you surpressed....

#8 cyprus

cyprus
  • Members
  • PipPipPip
  • Advanced Member
  • 68 posts

Posted 15 September 2006 - 08:57 AM

Thanks for the comments on @, I just picked up my colleages line of code. Should I remove @, as I don't want to suppress seing any errors. Thanks

#9 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 15 September 2006 - 09:00 AM

Only very few circumstances warrant the use of the error suppresor.

#10 cyprus

cyprus
  • Members
  • PipPipPip
  • Advanced Member
  • 68 posts

Posted 15 September 2006 - 09:41 AM

Thanks, I will remove it. Regards




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users