p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   PHP Databases (http://p2p.wrox.com/forumdisplay.php?f=97)
-   -   Error but it works (http://p2p.wrox.com/showthread.php?t=43429)

Mitch June 3rd, 2006 11:28 AM

Error but it works

I have a page that is generating the following errror and I can't figure out why, especially when I use the same code on another page (but different database) and it does not generate the error, but when I change the database reference i get it:

Warning: mysql_list_fields(): Unable to save MySQL query result in /usr/www/users/xyz/abc/123/list.php on line 9
But it does work, the only thing it does is populate a dropdown list and that works.

Here is the code:


$connection = mysql_pconnect("x", "y", "z");

$fields = mysql_list_fields("A", "B", $connection);


<form method="post" action="show_list.php">
<p align="center">
<select name="searchteam">

$query = mysql_query('select username from onmylist_user order by username') or die
while($row = mysql_fetch_array($query)) {
           echo("<option value=\"${row['username']}\">${row['username']}\n");


<input type=submit value="Show A Persons People On My List">




richard.york June 3rd, 2006 03:09 PM

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

For example,

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.
        // You should never get here, but I'm not one to say never
        echo "Unable to retrieve database fields.";
    // 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.

$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: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:



Beginning CSS: Cascading Style Sheets For Web Design
CSS Instant Results


Mitch June 4th, 2006 09:25 AM

Thanks very much1

Little by little I will get there. :)


All times are GMT -4. The time now is 11:17 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.