0

I have purchased godaddy linux by mistake and now I have no clue how to do my simple photo gallery which I used to do with classic ASP!

I have created a MySQL table with fields "image_path" and "no_of_images" etc... What I want to do is connect to the database table, get image_path into img tag and loop till the numeric value stored in "no_of_images" is met.

I have tried to do this with this code but it is not working.

<?php
    $servername = "localhost";
    $username="";
    $password="";
    $dbname="";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    $myid = $_GET['id']; 
    $sql = "SELECT * FROM gallery where id='$myid'";
    $result = $conn->query($sql);

    $row = mysql_fetch_row($result);
    $image_path = $row[3]; 
    $no_of_images = $row[4]; 
    $x = 1;

    while($x <= $no_of_images ) {
        echo "<img src="$image_path"/"$x".jpg><br>";
        $x++;
    } 

    $conn->close();
?> 
4
  • 4
    I'm pretty sure GoDaddy lets you switch to a different server platform. Commented Mar 3, 2015 at 14:40
  • 4
    You're mixing MySQL APIs with mysql_fetch_row. Those different APIs do not intermix with each other. Having checked for errors, would have signaled an error. php.net/manual/en/mysqli.error.php Commented Mar 3, 2015 at 14:43
  • 1
    I'm pretty sure you also have a SQL injection vulnerability there. You'll want to read up on that a bit and start using prepared statements. php.net/manual/en/security.database.sql-injection.php Commented Mar 3, 2015 at 14:48
  • Yes, i am familiar with monetize querystring with classic asp! not sure how to do it with php! Thanks for the link. Commented Mar 3, 2015 at 15:17

2 Answers 2

4

You are using a mysqli result in a mysql function. You should use $result->fetch_row()

Sign up to request clarification or add additional context in comments.

Comments

0

As a previous answer states - your switching to a MySQL function. Use this code:

<?php

$servername = "localhost";
$username   = "";
$password   = "";
$dbname     = "";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$myid   = $_GET['id']; 
$sql    = "SELECT * FROM gallery where id='$myid'";
$result = $conn->query($sql);

$x      = 1;

while ($row = $result -> fetch_row()){

     $image_path = $row[3]; 
     $no_of_images = $row[4]; 

     echo "<img src=" .$image_path. "/" .$x. ".jpg><br>";
     $x++;

}

$conn->close(); 

3 Comments

Thanks for the edit, it connects but only out putting one image! how do i loop the $image_path till the value in $no_of_images which is 17 is met? can i use another while loop above the echo? Sorry for not understanding as i am totally new to PHP!
Is your SQL query returning more than one row of data?
No, its only one row, but want to loop a column(image_path) < = value in another column(no_of_images)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.