Help With Random Image\URL PHP (Using MySQL)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
phpless
Forum Newbie
Posts: 1
Joined: Sun Feb 08, 2009 6:18 pm

Help With Random Image\URL PHP (Using MySQL)

Post by phpless »

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

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>" ;
}
?>
 
Below is what I'm injecting via SQL into the database...

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");
 
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.
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: Help With Random Image\URL PHP (Using MySQL)

Post by novice4eva »

The first phase of diagnosis should start with enabling the error reporting with these two lines:

Code: Select all

 
    ini_set('display_errors', 1);
    ini_set('error_reporting', E_ALL);
 
This should pop out the issues that maybe in hiding. I don't know the default value of result type for mysql fetches but i think doing this is better for your purpose:

Code: Select all

 
mysql_fetch_array($result, MYSQL_ASSOC)) 
 
Post Reply