Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

PHP Quote database

Featured Replies

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.

echo doesnt require brackets. Instead : echo "<blockquote>".$quote;

<?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.

  • 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.

  • 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

try changing this line here

 

 

$sql = "SELECT * from quote";

 

to

 

$sql = "SELECT * from q_quote";

 

 

  • 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'

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

  • 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...

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"));

use this as your query

 

$sql = "SELECT * from quote ORDER BY RAND() ";

  • 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 >.<

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

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.