c4onastick Posted October 5, 2006 Share Posted October 5, 2006 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>""; $eye.=stripslashes($morerow[0]); $eye.=""</i></td>\n</tr>"; $eye.="<tr>\n<td align='right'>--"; $eye.=.stripslashes($morerow[1]); $eye.=" <hr></td>\n</tr>\n"; } } $eye.="</table>\n"; echo $eye;[/code]When I run this I get this output:[code]432822152923530Fatal 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.) Quote Link to comment Share on other sites More sharing options...
markbett Posted October 5, 2006 Share Posted October 5, 2006 what is in the variable morerow? is it a number... where is it being set? Quote Link to comment Share on other sites More sharing options...
c4onastick Posted October 5, 2006 Author Share Posted October 5, 2006 $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. Quote Link to comment Share on other sites More sharing options...
c4onastick Posted October 5, 2006 Author Share Posted October 5, 2006 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? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.