Jump to content

Archived

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

cyprus

Bypass Select statement when empty.

Recommended Posts

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);

Share this post


Link to post
Share on other sites
You could use an if statement

[code]<?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);
}

?>[/code]

Share this post


Link to post
Share on other sites
Just curious, what does the @ mean in front of the mysql_select_db() call?

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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....

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Only very few circumstances warrant the use of the error suppresor.

Share this post


Link to post
Share on other sites

×

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.