Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP Databases
| Search | Today's Posts | Mark Forums Read
PHP Databases Using PHP in conjunction with databases. PHP questions not specific to databases should be directed to one of the other PHP forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 3rd, 2006, 11:28 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: South Bend, Indiana, USA.
Posts: 149
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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:
Code:
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:
Code:
<?
 require_once("bookmark_fns.php");
 do_html_header("");



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

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

</select>

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

</form>


<?
 do_html_footer();
?>
Mitch
__________________
Mitch
 
Old June 3rd, 2006, 03:09 PM
richard.york's Avatar
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
 
Old June 4th, 2006, 09:25 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: South Bend, Indiana, USA.
Posts: 149
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks very much1

Little by little I will get there. :)

Mitch




Similar Threads
Thread Thread Starter Forum Replies Last Post
strange behavior: works once then error msgs start bonkbc Beginning VB 6 8 March 13th, 2008 02:49 PM
Nothing works out of the box - ??? mdunn6 BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 18 June 8th, 2006 02:02 PM
works bryan.lugo Excel VBA 0 April 19th, 2006 02:47 PM
DB works.....then it doesn't shyster1977 Classic ASP Databases 2 March 9th, 2005 02:57 PM
will the session works? rekha_jsr Classic ASP Basics 3 December 10th, 2004 02:02 AM





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