1

I've got a database that contains info on some images. The columns are id, name, description, location and visible.

I'm trying to have PHP read in the location column as an array WHERE visible=1, but it's not working. Can anyone see where I'm going wrong?

I know I should be using mysqli, before anyone points this out to me. I will do, once I've got this array syntax sorted out.

My code is below:

<?php

$con = mysql_connect("localhost", "root", "mypassword");
if (!$con)
{
  die('Could not connect: ' . mysql_error());
}

$db_selected = mysql_select_db("shibby",$con);
$sql = "SELECT location from gallery WHERE visible=1";
$photos= mysql_query($sql,$con);
print_r(mysql_fetch_array($result));
mysql_close($con);

$i = 0;
foreach ($photos as $photo) {
    $i = 0;
    if ($i < 3) {
        echo '<td><center><img src="'.$photo.'.jpg"></td>';
        $i++;
    } elseif ($i == 3) {
        echo '</tr><tr>';
        $i = 0;
    }
}
6
  • What does "not working" mean? What did you want to happen, and what actually happened? Commented May 15, 2014 at 13:52
  • 1
    $result is not defined. Change $photos= mysql_query($sql,$con); to $result= mysql_query($sql,$con); and set the fetch method to $photos Commented May 15, 2014 at 13:53
  • Sorry, I should have been more specific. I get one image displayed. thats it. When I echo $photos, i dont see a array, I see just the first item. Commented May 15, 2014 at 13:53
  • As a side note, the mysql_* functions have been deprecated. You should transition to mysqli or PDO. Commented May 15, 2014 at 13:55
  • Have a look at mysql_fetch_array in the manual - it just fetches a row, not the whole resultset. Look at the examples on that page to see how to retrieve all results. Commented May 15, 2014 at 13:55

4 Answers 4

1

Undefined variable 'result' (line 12)

Change

$photos= mysql_query($sql,$con);
print_r(mysql_fetch_array($result));

with

$photos= mysql_query($sql,$con);
print_r(mysql_fetch_array($photos));
Sign up to request clarification or add additional context in comments.

1 Comment

Incorrect: mysql_fetch_array only returns one row, not multiple rows
1

Use:

$photos= mysql_fetch_array(mysql_query($sql,$con));

And remove the line:

print_r(mysql_fetch_array($result));

And yes, use PDO or MySQLi :)

1 Comment

Incorrect: does not retrieve multiple rows as needed.
1

Please do NOT USE MYSQL methods on new projects!! Use mysqli or PDO (I prefer the latter as the first one is a PITA to get right)

If you really insist on correcting the existing code, fix the location retrieval as follows:

$result= mysql_query($sql,$con);
while ($row = mysql_fetch_row($result)) { 
   $photos[] = $row['location'];
}

mysql_fetch_array and mysql_fetch_row only get one row of the results, not the complete set, hence the need for the loop in the above

Comments

0

remove print_r from this line and replace this

mysql_fetch_array($photos); // you had used wrong variable

Comments

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.