Jump to content

WHat is missing?


roback

Recommended Posts

I am trying to pull an image from an image gallery DB using the following function:

<?php
function 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
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

Okay, so I adjusted the query and the errors help refine it.

<?php
function 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.
Link to comment
Share on other sites

$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]
Link to comment
Share on other sites

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.