July 28, 201016 yr Hey, I've tried writing a piece of code to pull random quotes from a database. Could anyone shed any light if this is the way to go? I know my database works, called 'quote' but I'm having a few problems with the code. There are two tables in the database one called 'q_id' [which gives the quote a value] and 'q_quote' which holds the actual quote. But I've managed to put this together from reading things on the internet about this kind of thing, but so far, no luck. <?php $db = mysql_connect("localhost", "root", "*") or die ("Unable to connect to database."); mysql_select_db("quote") or die ("Unable to select database."); $sql = "SELECT * from quote"; $result=mysql_query($query) or die(mysql_error()); $quote = htmlentities(mysql_result($result,$i,"quote")); echo("<blockquote>" . $quote); mysql_close($connection); ?> </body> </html> [i know calling the user 'root' isn't secure, this is purely to experiment] thanks for any help.
July 28, 201016 yr <?php $db = mysql_connect("localhost", "root", "*") or die ("Unable to connect to database."); mysql_select_db("quote") or die ("Unable to select database."); $sql = "SELECT * from quote"; $result=mysql_query($sql) or die(mysql_error()); $quote = htmlentities(mysql_result($result,$i,"quote")); echo "<blockquote>".$quote; mysql_close($db); ?> </body> </html> Try that.
July 28, 201016 yr Author echo doesnt require brackets. Instead : echo '"<blockquote>".$quote'; Thanks alot for that, fixed the error I was getting, now getting 'Query was empty', anything obvious causing this? @smithmr8 Thanks for the reply, but that gives me a similar error to the one before: Warning: mysql_result() [function.mysql-result]: quote not found in MySQL result index 3 in C:\xampplite\htdocs\digitalartsconcepts\gen.php on line 16 obviously some problem finding things in my database? //EDIT hang on just looked at my status and seen 'MySQL database DEACTIVATED' I'll try and fix this then check back, thanks for alot for sorting that code out.
July 28, 201016 yr Author Hey, just got my database up and running again, but i'm getting the error 'Warning: mysql_result() [function.mysql-result]: quote not found in MySQL result index 3 in C:\xampplite\htdocs\digitalartsconcepts\gen.php on line 16' The code I'm using: <?php $db = mysql_connect("localhost", "root", "*") or die ("Unable to connect to database."); mysql_select_db("quote") or die ("Unable to select database."); $sql = "SELECT * from quote"; $result=mysql_query($sql) or die(mysql_error()); $quote = htmlentities(mysql_result($result,$i,"quote")); echo "<blockquote>".$quote; mysql_close($db); ?> It doesn't seem to like this: $quote = htmlentities(mysql_result($result,$i,"quote")); Any ideas? any help appreciated
July 28, 201016 yr try changing this line here $sql = "SELECT * from quote"; to $sql = "SELECT * from q_quote";
July 28, 201016 yr Author try changing this line here $sql = "SELECT * from quote"; to $sql = "SELECT * from q_quote"; Thanks, but that gives me the error 'Table 'quote.q_quote' doesn't exist'
July 28, 201016 yr Thanks, but that gives me the error 'Table 'quote.q_quote' doesn't exist' Are you sure the table holding the quotes is q_quote and there are no caps in q_quote in the database
July 28, 201016 yr Author Sorry, just reading back through this It made sense that '$quote = htmlentities(mysql_result($result,$i,"quote"));' should infact be '$quote = htmlentities(mysql_result($result,$i,"q_quote")); thanks pat24 Just one question, should this randomly display the quotes in the table? Just tried refreshing it a few times and didn't change...
July 28, 201016 yr Sorry misunderstood the db setup use this below $sql = "SELECT * from quote"; $result=mysql_query($sql) or die(mysql_error()); $quote = htmlentities(mysql_result($result,$i,"q_quote"));
July 28, 201016 yr Author Just one last question I now have my code as: <?php if($_GET['act'] == "generate_quotes") { $db = mysql_connect("localhost", "root", "bonnie") or die ("Unable to connect to database."); mysql_select_db("quote") or die ("Unable to select database."); $offset_result = mysql_query( " SELECT FLOOR(RAND() * COUNT(*)) AS `offset` FROM `quote` "); $offset_row = mysql_fetch_object( $offset_result ); $offset = $offset_row->offset; $result = mysql_query( " SELECT * FROM `quote` LIMIT $offset, 1 " ); $fetch = mysql_fetch_array($result); echo "<blockquote>".$fetch['q_quote']; mysql_close($db); } else { echo "<img src=\"1.png\">"; } ?> <a href='gen.php?act=generate_quotes'>Generate</a> because apparently doing it this way is much faster? But could someone shed some light on why the link isn't working? What it does it shows and image until 'generate' is clicked...which is right, but then the link should cycle through the quotes in the database, but it doesn't. But refreshing the page still does... Any ideas? Sorry to keep bumping this >.<
August 3, 201016 yr You only need 1 query here... <?php if($_GET['act'] == "generate_quotes") { $db = mysql_connect("localhost", "root", "bonnie") or die ("Unable to connect to database."); mysql_select_db("quote") or die ("Unable to select database."); $result = mysql_query( "SELECT `q`.`q_quote`, ROUND(RAND() * `x`.`q_id`) AS `random_quote` FROM `quote` AS `q`, (SELECT MAX(`q`.`id`) AS `q_id` FROM `quote` AS `q`) AS `x` ORDER BY `random_quote` LIMIT 1" ); $fetch = mysql_fetch_row($result); echo "<blockquote>".$fetch[0]."</blockquote>"; mysql_close($db); } else { echo "<img src=\"1.png\">"; } ?> <a href='gen.php?act=generate_quotes'>Generate</a>
Create an account or sign in to comment