roback Posted November 7, 2006 Share Posted November 7, 2006 I am trying to pull an image from an image gallery DB using the following function:<?phpfunction get_rand_img(){ // Make the Query $query = "SELECT 'image_url' FROM 'images' WHERE 'active' = 'Y' ORDER BY RAND() 'limit' = 3"; // Run the query $result = @mysql_query ($query); // If it ran okay, display records// if ($num > 0)// { echo '<table width="100%" border="1" cellspacing="5" cellpadding="5"> <tr> <td height="100px">'; $img = $result; if (isset($img)) { echo '<img src="gall/images/'; echo $img; echo '" width="190px">'; echo $img; // Testing echo $result; // Testing } echo '</td> </tr> </table>'; // }}?>I think I am missing an mysql_fetch command or something, but I cannot figure it out.You can view the site at [url=http://www.dilzoncharters.com]www.dilzoncharters.com[/url]Any help would be appreciated.Rob Quote Link to comment Share on other sites More sharing options...
shoz Posted November 7, 2006 Share Posted November 7, 2006 When debugging it's usually easier to find query errors when using the following format.[code]$query = 'query here';$result = mysql_query($query) or die('query: '.$query."<br />\n".mysql_error());[/code]When there are errors with the query the output will show both the query after all variables have been expanded and the error returned by MYSQL.You should remove the quotes (') from around the column names and the table names. LIMIT should also have no quotes surrounding it and the syntax should be LIMIT 3 instead of LIMIT = 3.In your post the $num variable hasn't been assigned a value in your code. You should use [url=http://www.php.net/mysql_num_rows]mysql_num_rows()[/url] to determine how many if any rows were returned.You can then go through the result using something similar to the following[code]while ($row = mysql_fetch_assco($result)){ echo $row['image_url']."<br />\n";}[/code] Quote Link to comment Share on other sites More sharing options...
fenway Posted November 7, 2006 Share Posted November 7, 2006 It's definitely the single quotes -- you're getting back "image_url" as a string literal, instead of the column value stored therein. Quote Link to comment Share on other sites More sharing options...
roback Posted November 7, 2006 Author Share Posted November 7, 2006 Okay, so I adjusted the query and the errors help refine it. <?phpfunction get_rand_img(){ // Make the Query $query = "SELECT image_url FROM images ORDER BY RAND() limit 3"; $result = mysql_query($query) or die('query: '.$query."<br />\n".mysql_error()); // Run the query // $result = @mysql_query ($query); // If it ran okay, display records// if ($num > 0)// { echo '<table width="100%" border="1" cellspacing="5" cellpadding="5"> <tr> <td height="100px">'; $img = $result; if (isset($img)) { echo '<img src="gall/images/'; echo $img; echo '" width="190px">'; echo $img.'image var'; echo $result.'result var'; } echo '</td> </tr> </table>'; // }}?>Problem now is I still get no picture but it outputs the following:<td height="100px"><img src="gall/images/Resource id #6" width="190px">Resource id #6image varResource id #6result var</td>Now I am not sure what the Resource id #6... the only thing I can think is that it's the ImageID from the DB, but why would it pull that when I did not query that?Any help would be greatly appreciated. Quote Link to comment Share on other sites More sharing options...
fenway Posted November 7, 2006 Share Posted November 7, 2006 $result contains the entire recordset, internally represented as a resource with id #6 in this case. You'll need to iterate through the result set, then pull the desired piece back from each record.Something like:[code]echo '<table width="100%" border="1" cellspacing="5" cellpadding="5">';while( $img = mysql_fetch_assoc( $result ) ) { echo '<tr> <td height="100px">'; echo '<img src="gall/images/'; echo $img['image_url']; echo '" width="190px">'; echo '</td></tr>';} echo '</table>';[/code] Quote Link to comment Share on other sites More sharing options...
roback Posted November 8, 2006 Author Share Posted November 8, 2006 That did the trick!! Thanks for all the help... one last question...As I set this up in a function, is there a way to define in the function the number of images to display.function get_rand_img(#)So that the # defines how many to retreive from the DB:$query = "SELECT image_url FROM images ORDER BY RAND() limit #"; <--- The # to limit it to?Then in each page where the function is called, I can put a number in that will make the images match the length of the page.I know most of the PHP function part, but how do change the limit amount from a number to a variable? Is it as easy as putting $n in there?Thanks again for the help.Rob Quote Link to comment Share on other sites More sharing options...
pnj Posted November 8, 2006 Share Posted November 8, 2006 It is indeed as easy as you say:[code]"... ORDER BY RAND() LIMIT $n";[/code]The query string is just a php string, there's nothing fancy about it.-pnj Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.