-1

I am having a problem. I know I am using deprecated MySQL, but I am working on a website for school and security is not necessary(as long as it works). I am trying to get all the Id's from a table called Reviews. This should be stored in a array. For some reason it only shows the first one. How to get all of them stored in 1 array? This is my code which is not working:

$sql1 = "SELECT Klanten_id FROM Reviews";
$res1 = mysql_query($sql1);
$r = mysql_fetch_assoc($res1)['Klanten_id'];
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
  • 1
    RTM: [mysql_fetch_assoc](http://php.net/manual/en/function.mysql-fetch-assoc.php): _Fetch a result row as an associative array_. You need a loop (There is an exemple in the manual too). – FirstOne Jun 08 '17 at 18:23
  • 2
    But since we are at it, don't use outdated APIs, learn PDO or MySQLi instead. – FirstOne Jun 08 '17 at 18:23
  • How to use loop in a database then? That stores all id's in a array? – Hugo de Heer Jun 08 '17 at 18:27
  • 1
    There are literally thousands of examples out there. Do we really need to provide another? – Strawberry Jun 08 '17 at 18:30
  • Btw guys, I am still learning and I find it very demotivating when people dislike the post. I am struggeling with this for a while, but it finally works now thanks to Alive to Die. Thanks for understanding and people trying to help me! – Hugo de Heer Jun 08 '17 at 18:53

1 Answers1

2

1.Don't use outdated mysql_* library.Turn to mysqli_* or PDO.

2.mysql_fetch_assoc: Fetch a result row as an associative array. So you need to apply loop to get all data

Like below:-

$ids = array(); //created an array
while ($row = mysql_fetch_assoc($res1)) {
    $ids[] = $row['Klanten_id']; // assign each id to the array
}
print_r($ids);// print array

3.To start working on mysqli/PDO check basic example here:- mysqli/PDO example

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
  • I understand your answer, but how to split the id's. If I get id=1 and id=12. It echoes 121. I want it to be stored in a array like this: ("1","12") – Hugo de Heer Jun 08 '17 at 18:36