0

I want to display many photos which belong to same alias, for example if i have many phographs of Apple and and want to show them all at once having one photo as main and others as a thumbnails of all apples.

$url=$_GET['photoUrl'];
$sql = "SELECT * FROM photos WHERE photoUrl='$url'";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
    $photoId=$row['photoId'];$photoName=$row['photoName'];$photoDesc=$row['photoDesc'];$photoUrl=$row['photoUrl'];$photoCategory=$row['photoCategory'];}

Above code is the main file called photo.php (which works perfectly for fetching all photos) and below code is morephoto.php which i include in photo.php.

why i am writing this below code because if i have signle apple photo as main photo and just beneath or above a photo bar like carausal will appear having many apple photos

so thing is may be i am just failing to co-relate photo.php with morephoto.php

<?php
include('admin/config.php');
$aliasPhoto=$_GET['alias'];
$sql = "SELECT * FROM photos WHERE alias='$aliasPhoto' LIMIT 0, 8";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
    $photoId=$row['photoId'];
    $photoName=$row['photoName'];
    $photoDesc=$row['photoDesc'];
    $photoUrl=$row['photoUrl'];
    $photoCategory=$row['photoCategory'];       
?>
                <div class="col-md-3">      
                    <a href="<?php echo"$photoUrl.html";?>">
                        <img class="img-responsive thumbnail" src="images/<?php echo"$photoUrl.jpg";?>" <?php echo "alt=\"$photoName\" title=\"$photoName\"";?>>
                    </a>                
                </div>
RRPANDEY
  • 235
  • 4
  • 15
  • Are you getting an error then draw us towards it – Narendrasingh Sisodia Mar 21 '15 at 13:32
  • i am not getting error but no result as needed, when using " $sql = "SELECT * FROM photos WHERE alias='Apple' LIMIT 0, 8"; it works perfectly but i want it dynamically no restriction to apple only – RRPANDEY Mar 21 '15 at 13:33
  • Are you getting value within `$_GET['alias']` – Narendrasingh Sisodia Mar 21 '15 at 13:42
  • @ Narendra i am not an expert in php so no idea how to check i am getting it or not – RRPANDEY Mar 21 '15 at 13:49
  • 1
    **Danger**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Mar 21 '15 at 13:53
  • You need to do some basic debugging. Is the generated SQL statement what you expect? Does the query call actually return a request? Do you go around the while loop at all or are there no results? Do any of the variables get assigned correctly within the loop? Is the generated HTML what you expect? – Quentin Mar 21 '15 at 13:58
  • Your code already is made for "dynamic" selection of your photos. How do you call that code? If you want another alias, just change `?alias=Apple` in your URL to the wanted alias. –  Mar 21 '15 at 13:59

2 Answers2

1

Please try following code:

<?php
 include('admin/config.php');
 $aliasPhoto = $_GET['alias'];
 $sql = sprintf("SELECT * FROM photos WHERE alias='%s' LIMIT 0, 8",mysql_real_escape_string($aliasPhoto));
 // Perform Query
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
    $photoId = $row['photoId'];
    $photoName = $row['photoName'];
    $photoDesc = $row['photoDesc'];
    $photoUrl = $row['photoUrl'];
    $photoCategory = $row['photoCategory'];
    echo "<div class='col-md-3'>
            <a href='$photoUrl.html'>
              <img class='img-responsive thumbnail' src='images/$photoUrl.jpg' alt='$photoName' title='$photoName'/>
            </a>
        </div>";
}


?>

the key point is you need make sure your sql execute correct , please use print the sql statement and copy it execute in mysql , see what happened.

Cherry
  • 388
  • 1
  • 3
  • 13
0

You are missing the $photoName in your HTML image source. that is,

<img class="img-responsive thumbnail" src="images/<?php echo $photoUrl.$photoName.".jpg";?>" <?php echo "alt=\"$photoName\" title=\"$photoName\"";?>>
varunsinghal
  • 329
  • 4
  • 12
  • it works fine for single photo but not for multiple photo having same category(i am using as alias). – RRPANDEY Mar 21 '15 at 13:47
  • Not quite. If `$photoURL` already contains the complete file name, your code will fail –  Mar 21 '15 at 13:55
  • @IkoTikashi it was not mentioned `$photoUrl` to have a filename. I considered it to be common error. – varunsinghal Mar 21 '15 at 14:02