View Single Post
  #2 (permalink)  
Old June 3rd, 2006, 03:09 PM
richard.york's Avatar
richard.york richard.york is offline
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Validate the arguments that you are passing to mysql_list_fields and call mysql_error / mysql_errno after calling mysql_list_fields.

For example,
Code:
if ($connection && is_resource($connection))
{
    $fields = mysql_list_fields('A', 'B', $connection);

    if (!$fields)
    {
        // $fields evaluates to a false value
        echo mysql_error();
    }
    else if (is_resource($fields)
    {
        // $fields has a value that doesn't evaluate to false
        // and it is a resource, do something with it here.
    }
    else
    {
        // You should never get here, but I'm not one to say never
        echo "Unable to retrieve database fields.";
    }
}
else
{
    // something's botched with the connection.
    echo "No connection to the database!";
}
Alot of what you do in programming is track down bugs, so to build a solid application, you'll need to include that part of it in your planning. That means validating the data that you pass as arguments to other functions, verify that the $connection is a resource handle and that it doesn't have a false value. Then, put in as much error reporting as is possible. Check returned values. Call built-in error reporting functions like mysql_error(), after every function where it's possible an error could be generated. Put in error reporting for every possible scenario. I see that you've at least started doing that from your script here, but, you need more!

The alternative is, if you're in a rush and just want a quick and dirty solution to the problem and aren't interested in the possible academic reasons why you're seeing errors, then just supress the error, in PHP that's done with the '@' operator.
Code:
$fields = @mysql_list_fields('A', 'B', $connection);
Of course, I recommend the former route.

In about six years of PHP programming, I've never seen this particular error, so I can't comment on what is likely to be wrong. Looking at the manual for this function, however, it is marked as deprecated. You should use a straight query instead. Either way, follow my advice and you'll spend less time wondering what went wrong and more time fixing what went wrong and working on other projects.

Quote:
quote:From: http://www.php.net/mysql_list_fields

This function is deprecated. It is preferable to use mysql_query() to issue a SQL SHOW COLUMNS FROM table [LIKE 'name'] statement instead.
The concept would be the same using the updated method since mysql_query also returns a resource handle. Just use the suggested SQL above and you'll be good to go.

For your reading pleasure:
http://www.php.net/is_resource
http://www.php.net/mysql_list_fields
http://www.php.net/mysql_error
http://www.php.net/mysql_errno
http://www.php.net/errorfunc
http://www.php.net/manual/en/languag...rorcontrol.php

HTH!

Regards,
Rich

--
Author,
Beginning CSS: Cascading Style Sheets For Web Design
CSS Instant Results

http://www.catb.org/~esr/faqs/smart-questions.html
Reply With Quote