Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > PHP Databases
Password Reminder
Register
| FAQ | Members List | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
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
Reply With Quote
  #2 (permalink)  
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
Reply With Quote
  #3 (permalink)  
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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 09:48 PM.


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