Jump to content

[SOLVED] After calling fetchInto once, it doesn't work for the second query! help!


c4onastick

Recommended Posts

I've been working on this code for my site, the intention of which is to select a given number of quotes from a database at random and display them. I can select the whole database fine, but I'd kind of like to have the "quote of the day" (or 3 or so). Any thoughts on accomplishing this? I've written this much so far:

[code]
                require_once("DB.php");
                $dsn = 'mysql://user:pass@localhost/quotes';
                $db =& DB::Connect( $dsn );
                if (PEAR::isError($db)) {die($db->getMessage());}

                $res = $db->query( "SELECT count(id) FROM quotes", array());

                $row = array(null);
                if( $res != null )
                        $res->fetchInto($row);

                echo "$row[0]"."<br />"; //Added for troubleshooting
                $total = $row[0];

                $nodisplay = 8;

                $res->free();

                for($i=0; $i<$nodisplay; $i++)
                {
                        $ids[]=rand(0, $total);
                        echo "$ids[$i]"."<br />"; //Added for troubleshooting
                }

                $resdis = $db->query( "SELECT quote, author FROM quotes WHERE id=?", $ids);

                $eye = "<table class='quotetable' width='100%'>\n";

                if( $resdis != null )
                {
                        while($resdis->fetchInto($morerow)) // Line 40
                        {
                        $eye.="<tr>\n<td><i>&quot;";
                        $eye.=stripslashes($morerow[0]);
                        $eye.="&quot;</i></td>\n</tr>";
                        $eye.="<tr>\n<td align='right'>--";
                        $eye.=.stripslashes($morerow[1]);
                        $eye.="&nbsp;&nbsp;<hr></td>\n</tr>\n";
                        }
                }

                $eye.="</table>\n";
                echo $eye;
[/code]

When I run this I get this output:
[code]43
28
22
15
2
9
2
35
30

Fatal error: Call to undefined method DB_Error::fetchInto() in /public_html/randtest.php on line 40[/code]

What am I missing? FetchInto worked fine the first time, can I not call it twice in the same script?

Thanks in advance for the help!

(By the by, if any one has any better ideas on how to display a "quote of the day" (or 3) chosen at random from the database, please share.)
Link to comment
Share on other sites

$morerow is (read: should be) an array containing the quote and author from the database. I didn't define it explicitly, but I tried it with:
[code]$morerow = array(null);[/code]
Right before line 40. Same error though. Does it have anything to do with the result object? I did a little snooping around pear.php.net, seems there's some functions to clear result and query (or prepare rather) objects.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.