<?php
$hostname = 'localhost';
$user = 'username';
$pass = 'password';
$database = 'database_name';
$db_connection = new PDO( "mysql:host=" . $hostname . ";dbname=" . $database, $user, $pass );
$results = $db_connection->query( 'SELECT testimonial, author FROM recommendations WHERE 1 ORDER by rand() LIMIT 1' );
foreach ( $results as $row ) {
echo '<p id="quote">' . $row['testimonial'] . '</p>';
echo '<p id="author">–' . $row['author'] . '</p>';
}
// Close the connection
$db_connection = null;
Basic Database Connection, Random Query, Display Result
Chris Coyier
on
Sweet!
Not the basic only. I liked it.
Thanks.
You are most welcome!
Hi, this script can only be implemented for a single table. Do you have any other script that works for more than one table.
Thank you.
You could write this into a query function, and then use multiple SQL queries.
I find this to be a more secure and robust implementation. It’s definitely more dynamic. Not knocking the author’s code, it’s two years old, and I am sure this was just a starting point.
Check out this class:
https://github.com/ajillion/PHP-MySQLi-Database-Class/blob/master/MysqliDb.php
You can store Login Details Like Username & Password in different file and then include it into above page instead of directly specified.Something like following
——–
and secure.php contains following data.
—
It is very secure method than above one.
Just an FYI for newcomers. I suggest using the new php syntax. This is deprecated. You should be using prepared statements and mysqli connection, otherwise you’re vulnerable to sql injection.
http://php.net/manual/en/book.mysqli.php
USE PDO! mysql_connect is an anti-pattern and not as secure as it should be.
PDO style. After reading, trial and error!! First post of a newbee!
This should be updated. MySQLi or PDO.
Indeed, when will you update this?
PLEASE FRIENDS AND FAM I NEED YOUR HELP I WANT TO BUILD A DYNAMIC WEBSITE USING PHP I NEED ALL THE HELP I CAN GET AS I AM KIND OF NEW TO PHP