fetch_row to display images

Dear Friends:

I need display pictures in 2 or 3 column with .PHP and MySQL and I would like to know if you can help me in order to modify the script below to make it:

Script
============

$conn = mysql_connect("localhost","user", "password");
mysql_select_db("db_name",$conn);

$ssql = "select * table_images ";


$rs = mysql_query($ssql);

while($myrow = mysql_fetch_array($rs))
{
print
"<table width='180' border='0' cellspacing='0' cellpadding='0'>
  <tr>
    <td><div align='center'>$myrow[name]</td>
  </tr>
    <tr>
    <td><img src='images/$myrow[photo]></td>
  </tr>
</table>";
}
mysql_free_result($rs);
mysql_close($conn);

==========

Best Regard,


Willy Gonzalez
willygonzalezAsked:
Who is Participating?
 
ldbkuttyConnect With a Mentor Commented:
Try like this (its untested but the logic should work):

<?php
$conn = mysql_connect("localhost", "user", "password") or die("Could not connect to DB: " . mysql_error());
mysql_select_db("db_name", $conn) or die("Could not select DB: " . mysql_error());

$ssql = "select * from table_images";
$rs = mysql_query($ssql) or die("Sql error: " . mysql_error());

while($myrow = mysql_fetch_array($rs))
{
    $myPhoto[] = $myrow['photo'];
    $myName[]  = $myrow['name'];
}
echo "<table width='180' border='0' cellspacing='0' cellpadding='0'>";
$i = 0;
while($i < count($myPhoto))
{
      echo "<tr>";
      for($j = 0; $j < 3; $j++)
      {
            $newVal = $i + $j;
            echo "<td align='center'>";
            echo (isset($newVal)) ? $myPhoto[$newVal] : '&nbsp;';
            echo "</td>";
      }
      echo "</tr>";
      echo "<tr>";
      for($j = 0; $j < 3; $j++)
      {
            $newVal = $i + $j;
            echo "<td align='center'>";
            echo (isset($myName[$newVal])) ? "<img src='images/{$myName[$newVal]}' />" : '&nbsp;';
            echo "</td>";
      }
      echo "</tr>";
      $i = $i + 3;
}
echo '</table>';
 
mysql_free_result($rs);
mysql_close($conn);
?>

PS: We cannot read your mind dude, you should have given the example in the beginning itself. :)
0
 
Timothy GoldenWeb DevCommented:
$conn = mysql_connect("localhost","user", "password");
mysql_select_db("db_name",$conn);
$ssql = "select * table_images ";
$rs = mysql_query($ssql);
echo"<table width='180' border='0' cellspacing='0' cellpadding='0'>";
while($myrow = mysql_fetch_object($rs)){
echo"
  <tr>
    <td><div align='center'>$myrow->name</td>
  </tr>
    <tr>
    <td><img src='images/$myrow->photo'></td>
  </tr>
";
}
echo "</table>";
mysql_free_result($rs);
mysql_close($conn);
0
 
Timothy GoldenWeb DevCommented:
use the last one

it will loop through the table rows createing a row for rach record
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
willygonzalezAuthor Commented:
Sorry but don't work. Display the follow messages:

Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in /home/roymarvi/public_html/row.php on line 26
 
Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource in /home/roymarvi/public_html/row.php on line 37
0
 
Timothy GoldenWeb DevCommented:
did you change the database connection information?
 $conn = mysql_connect("localhost","user", "password");

it would seem that the DB is not connecting.
0
 
Timothy GoldenWeb DevCommented:
thanks ldbkutty
0
 
ldbkuttyCommented:
"from" word is missing in the sql query, and an associative array should be enclosed within curly braces.

<?php
$conn = mysql_connect("localhost", "user", "password") or die("Could not connect to DB: " . mysql_error());
mysql_select_db("db_name", $conn) or die("Could not select DB: " . mysql_error());

$ssql = "select * from table_images";
$rs = mysql_query($ssql) or die("Sql error: " . mysql_error());

while($myrow = mysql_fetch_array($rs))
{
print
"<table width='180' border='0' cellspacing='0' cellpadding='0'>
  <tr>
    <td><div align='center'>{$myrow['name']}</td>
  </tr>
    <tr>
    <td><img src='images/{$myrow['photo']}'></td>
  </tr>
</table>";
}
mysql_free_result($rs);
mysql_close($conn);
?>
0
 
Timothy GoldenWeb DevCommented:
ldbkutty  oh yea.. missed that one too....oppps
0
 
willygonzalezAuthor Commented:
This script don't show the data in columns, list in rows
0
 
ldbkuttyCommented:
<?php
$conn = mysql_connect("localhost", "user", "password") or die("Could not connect to DB: " . mysql_error());
mysql_select_db("db_name", $conn) or die("Could not select DB: " . mysql_error());

$ssql = "select * from table_images";
$rs = mysql_query($ssql) or die("Sql error: " . mysql_error());

while($myrow = mysql_fetch_array($rs))
{
print
"<table width='180' border='0' cellspacing='0' cellpadding='0'>
  <tr>
    <td><div align='center'>{$myrow['name']}</td>
    <td><img src='images/{$myrow['photo']}'></td>
  </tr>
</table>";
}
mysql_free_result($rs);
mysql_close($conn);
?>
0
 
willygonzalezAuthor Commented:
Sorry but the script don't work property, I have the images in a directory and the reference [photo_name] in a data base, I need display the images in 3 columns how follow:

images1    | images2     | images3
name1      | name2       | name3

images4    | images5     | images6
name4      | name5       | name6
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.