Martezz
Martezz

Reputation: 35

Collect all ID's MySQL

I've done this:

$result = mysql_query("SELECT image, id FROM store WHERE username = '$username_show'");
        $num_rows = mysql_num_rows($result);

        $ids = mysql_fetch_assoc($result);
        $ids = $ids['id'];

for ($i = 0; $i < $num_rows; $i++) {    
        echo "<img src='get.php?id=$ids[$i]' height='300'><p/>";        
   }

I want to show all of my photos that has that username. But the $ids array only gets one index, and that's the last ID. What am I doing wrong?

Upvotes: 0

Views: 87

Answers (1)

Up_One
Up_One

Reputation: 5271

Like @Matthew said thet are deprecated use :

// mysqli
$mysqli = new mysqli("example.com", "user", "password", "database");
$result = $mysqli->query("SELECT image, id FROM store WHERE username = '$username_show'");
$row = $result->fetch_assoc();
echo htmlentities($row['row']);

// PDO
$pdo = new PDO('mysql:host=example.com;dbname=database', 'user', 'password');
$statement = $pdo->query("SELECT image, id FROM store WHERE username = '$username_show'");
$row = $statement->fetch(PDO::FETCH_ASSOC);
echo htmlentities($row['row']);

To answer your comment :
- use the array function

$result_array = array();
while($row = mysql_fetch_assoc($result))
{
    $result_array[] = $row;
}

$result = $mysqli->query("SELECT  id FROM store WHERE username = '$username_show'");
$result_array = array();
while($row = mysqli_fetch_assoc($result))
{
    $result_array[] = $row;
}

Upvotes: 1

Related Questions