Help With Random Image\URL PHP (Using MySQL)
Posted: Sun Feb 08, 2009 6:23 pm
Let me come straight out and say this I'm very new to PHP programming, I'm still pulling scripts off the internet and "attempting" to modify them to do what I want. I recently found this forum and decided to stop lurking and ask a question, hopefully someone can lend me a hand.
Below is a code I want to use, it's going to query a MySQL DB and pull a random image and link to that image to be displayed. Below is the code...
Below is what I'm injecting via SQL into the database...
Now, when I test the above code and call it via include into a php web page it displays nothing, no errors that I can see. I'm not to sure how old this code is and if it's an issue with it being too old. I'd like someone to help me figure this out.
Also, is there any way I can make this process faster and less intensive on the web server, this script will most likely be called by web visitors at the same time, with about 100 to 200 visitors at one time. Any help is greatly apperciated.
Below is a code I want to use, it's going to query a MySQL DB and pull a random image and link to that image to be displayed. Below is the code...
Code: Select all
// PHP script
<?
// Connect to the database
mysql_connect ('localhost', 'username', 'password') ;
mysql_select_db ('database_name_here');
// Edit this number to however many links you want displaying
$num_displayed = 3 ;
// Select random rows from the database
$result = mysql_query ("SELECT * FROM links ORDER BY RAND() LIMIT $num_displayed");
// For all the rows that you selected
while ($row = mysql_fetch_array($result))
{
// Display Results to Web Page
echo "<a href=\"" . $row["link"] . "\">
<img src=\"" . $row["image"] . "\" border=0 alt=\"" . $row["text"] . "\">
</a>" ;
}
?>
Code: Select all
CREATE TABLE `links` (
`id` int(11) NOT NULL auto_increment,
`link` text NOT NULL,
`image` longtext NOT NULL,
`text` text NOT NULL,
PRIMARY KEY (`id`)
) TYPE=MyISAM;
insert into links values ('NULL',"http://www.site1.com", "path/image.gif", "website description");
insert into links values ('NULL',"http://www.site2.com", "path/image.gif", "website description");
insert into links values ('NULL',"http://www.site3.com", "path/image.gif", "website description");
Also, is there any way I can make this process faster and less intensive on the web server, this script will most likely be called by web visitors at the same time, with about 100 to 200 visitors at one time. Any help is greatly apperciated.