Jump to content


Photo

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


  • Please log in to reply
3 replies to this topic

#1 c4onastick

c4onastick
  • Members
  • PipPipPip
  • Advanced Member
  • 216 posts

Posted 05 October 2006 - 05:07 AM

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:

                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;

When I run this I get this output:
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

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.)
Regex Tester::Unicode Regex::PHP Function List::MySQL 5.1
"Sorry sweetheart... but this all day sucker is down to the soggy white stick." -- Topper Harley

#2 markbett

markbett
  • Members
  • PipPipPip
  • Advanced Member
  • 133 posts

Posted 05 October 2006 - 05:31 AM

what is in the variable morerow?  is it a number...  where is it being set?

#3 c4onastick

c4onastick
  • Members
  • PipPipPip
  • Advanced Member
  • 216 posts

Posted 05 October 2006 - 05:48 AM

$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:
$morerow = array(null);
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.
Regex Tester::Unicode Regex::PHP Function List::MySQL 5.1
"Sorry sweetheart... but this all day sucker is down to the soggy white stick." -- Topper Harley

#4 c4onastick

c4onastick
  • Members
  • PipPipPip
  • Advanced Member
  • 216 posts

Posted 05 October 2006 - 05:48 PM

Ok. I did some tinkering. And got it working. Anyone have any ideas on randomly selecting a quote of the day that is static throughout the day?
Regex Tester::Unicode Regex::PHP Function List::MySQL 5.1
"Sorry sweetheart... but this all day sucker is down to the soggy white stick." -- Topper Harley




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users