On Mon, 9 Jun 2003, Thomas Seifert wrote:

> On Mon, 9 Jun 2003 07:35:16 -0300 [EMAIL PROTECTED] (Marcelo Luiz De Laia) wrote:
> 
> > I use phpnuke and it have a sql_layer.php. You are correct, for mysql is
> > mysql_query. But this dont is the problem, I change it and the problem
> > continue!
> > 
> > Any tip??
> 
> I think you are mixing the layer and native functions.
> You should use ONE of these, either the layer from phpnuke OR 
> the native functions.
> I don't know how the layer handles the results but from the layout you had it looks 
> just wrong.
> 
> The "standard"-way using native mysql_*-functions for a select (with some basic 
> error checking) is as follows:

It's one way, not standard imho :)  My additions below are of
course just one way out of many.  In a production environment
you won't be printing out mysql errors, or sql statements, but
instead will give the user some pretty error.  The code below
is for a development environment and doesn't include a "debug"
mode.

The reason I use @ is because we are implementing our own
error handling so having PHP print out the errors too isn't
needed.  So choose one or the other, PHP's errors or your
own, or, turn down error reporting for the entire script
but I'll just use @.

The reason for the error is your are assuming $result is
a valid MySQL result resource when it's not, it's most
likely boolean false because for some reason (like the
connection failed, couldn't select database, invalid
query, etc) it's not valid.  phpnuke is known for having 
crappy code so that's unfortunate for you but anyway the 
words below will explain how to avoid this error, or why 
it may exist.

> [... connection and so on ...]

Making sure the connection and database selection works is
pretty important too:

$conn = @mysql_connect("host", "user", "pass");
if (!$conn) {
    echo "Could not connect to MySQL";
    exit;
}
if ([EMAIL PROTECTED]('dbname')) {
    echo "Could not select database: " . mysql_error();
    exit;
}

> $result = mysql_query("select * from ...");
> 
> if(!mysql_error()) {
>    echo mysql_error();
> }

Instead of calling mysql_error() twice, it's more common
and efficient to check the return value of mysql_query() 
(in this case we put it in $result) as mysql_query returns
boolean false on failure:

$sql = "SELECT foo, bar FROM sometable";
if (!$result = @mysql_query($sql)) {
    echo "Could not run query ($sql): " . mysql_error();
    exit;
}

And next, before fetching rows, make sure there are actually
rows to fetch.  Instead of my silly echo statement, consider
including the search form again, or have them press the
back button, or whatever:

if (mysql_num_rows($result) < 1) { 
    echo "No results match your query, please try again";
    exit;
}

Now it's finally time to fetch some rows as we now know that
$result is a valid mysql resource and rows exist.  This is 
the basic idea of checking if everything worked instead of 
just assuming everything will be perfect 100% of the time 
because it won't.

Regards,
Philip
 


> while($row=mysql_fetch_row($result)) {
>   [... output ...]
> }
> 
> You are either not transferring the result-identifier correctly or your query 
> returns an error.




-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to