Zaffar Saffee
Zaffar Saffee

Reputation: 6305

Display image with URL from MySQL

I am using this code to display image from URL, but it is not working,

<?php
     echo 'me'.'<br/>';


      $sql= 'SELECT url_imgsrch FROM p_url_imgsrch';
      $result   =   mysql_query($sql);
        echo $result;
       $row = mysql_fetch_row($result);


      for ($i=0; $i<6; $i++){

        //calling rows
        echo '<td>';

//calling rows value for debugging purpose so that i can learn the process and check the output

         echo $row[$i].'<br/>';

echo '<img name="myimage" src="<?php echo $row[$i]; ?>" width="60" height="60" alt="word" />';

            echo '</td>';
            }

     ?>

The result is, I get alt text only, and image is not displayed. also, I get error

Notice: Undefined offset: 1 in D:\wamp\www\demo\login\flashcard.php on line 31

In my file What I am trying is, to get 5 img url from database and display them in columns of a table..and what I guess is I am getting same img URL again and again...for 5 times..

Please give me guideline and tell me what I could be missing...

Upvotes: 0

Views: 8103

Answers (3)

Erik
Erik

Reputation: 222

try this:

    <?php
    echo 'me'.'<br/>';

    $sql= 'SELECT url_imgsrch FROM p_url_imgsrch';
    $result   =   mysql_query($sql);
    echo $result;
    $row = mysql_fetch_row($result);


    for ($i=0; $i<6; $i++)
    {
        //calling rows
        echo '<td>';
        //calling rows value for debugging purpose so that i can learn the process and check the output
        echo $row[$i].'<br/>';
        echo '<img name="myimage" src=' . $row[$i] . ' width="60" height="60" alt="word" />';
        echo '</td>';
    }

?>

Upvotes: 0

zrvan
zrvan

Reputation: 7753

With mysql_fetch_row() your fetching the results one row at a time, since you only specified url_imgsrch in the SELECT statement, it will be an array with one single element, the value for url_imgrch. This is the reason for your error message. The for() loop tries to in turn read the first, second, etc up to fifth value of the array, which never gets updated with new information and always has only one element.

mysql_fetch_row() will return false when there is no more data to be read, you need to call it to fetch each new row of data, so what you want is to rewrite your code to something along the lines of this:

$sql = 'SELECT url_imgsrch FROM p_url_imgsrch';

$result = mysql_query($sql);
while (false !== ($row = mysql_fetch_row($result))
{
  echo '<td>';
  echo '<img name="myimage" src=' . $row[0] . ' width="60" height="60" alt="word" />';
  echo '</td>';
}

The first part is just as you've written, setting up the SQL query and fetching a resource pointer ($result) to with mysql_query().
In the next part the while() loop will run for as long as $row is not false, updating $row with new data for each run of the loop. Since mysql_fetch_row() fetches an numerically indexed array, you want to retrieve $row[0], it's the first (and only) column you requested in the SQL query. There are many ways of writing the while() loop, but this way quite common, and easy to read for other developers.

Upvotes: 0

Johnny Craig
Johnny Craig

Reputation: 5000

i have never seen someone loop through a query that way, i do it like this:

print "<table>";
$sql= 'SELECT url_imgsrch FROM p_url_imgsrch LIMIT 5';
$result   =   mysql_query($sql);
 while($row = mysql_fetch_array($result)){
    print '<tr>
            <td>
               <img name="myimage" src="'.$row[column_name_here].'" width="60" height="60" alt="word" />
            </td>
          </tr>';
    }
print "</table>";

change column_name_here to the name of the column which store the image file name

edit: changed mysql_fetch_row to mysql_fetch_array <- that is why u got the same image all 5 times.

Upvotes: 3

Related Questions