0

I have a table which assigns all users an ID number. I am trying to fetch all those ID's and add them into an array for use with a foreach. The method I am using below is not working for me. The array is not being populated with the data from MYSQLi. Can anyone spot any issues?

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

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

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

    $result = mysql_query("SELECT userid FROM vms_users");

    $data = array();
    while(($row = mysql_fetch_array($result))) {
        $data[] = $row['userid'];
    }

    print_r(array_values($data));

    $conn->close();
?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141

1 Answers1

2

I spotted an error for you :) Thanks to @icecub for pointing a typo.

you're connecting using mysqli but you're querying and extracting object using mysql

so change following part

$result = mysql_query("SELECT userid FROM vms_users");
//       ^ here
$data = array();
while(($row = mysql_fetch_array($result))) {
    //       ^ here
    $data[] = $row['userid'];
}

to

$result = $conn->query("SELECT userid FROM vms_users");

$data = array();
while($row = $result->fetch_assoc()) {
    $data[] = $row['userid'];
}

and this should work fine.

icecub
  • 8,615
  • 6
  • 41
  • 70
Mubin
  • 4,325
  • 5
  • 33
  • 55
  • Hey thanks for your comment. I noticed that issue as well just before I saw your answer. I did it a bit different but was still able to get the same result. – Whiskey Simulation Oct 11 '15 at 01:02
  • pleasure buddy :). also, if you've `userid` as `Primary Key` in your `database` then after using `SELECT DISTINCT userid FROM vms_users`, you don't need to use `array_values`. There will be no blank/empty value. IFF you've `userid` a `PK`. – Mubin Oct 11 '15 at 01:06
  • and if, it's `PK`, you can use this too `SELECT userid FROM vms_users`. this will also yield same results. – Mubin Oct 11 '15 at 01:06
  • 2
    If you're going for OOP (which I can only applaud), at least stick to it. So instead use: `while($row = $result->fetch_assoc()) { ... }` – icecub Oct 11 '15 at 01:08
  • @Mubin Awesome thanks for the information. I will give you Answer credit as we pretty much came up with the same result. Thanks again. – Whiskey Simulation Oct 11 '15 at 01:08
  • @icecub, thanks man, updated my answer. +1 to you. – Mubin Oct 11 '15 at 01:12
  • @icecub Great tip! Thanks! I have adjust my code. I just started getting into learning mysqli and I am having a hard time weeding through all the depreciated functions.. – Whiskey Simulation Oct 11 '15 at 01:12
  • Much better. Now I'd still remove the extra `(...)` around the parameters in the while loop (they're kinda pointless), but that's just me :) – icecub Oct 11 '15 at 01:13
  • @icecub so, suggest an edit, and I'll accept it, edit the code you way the like :) – Mubin Oct 11 '15 at 01:15
  • awesome +1 for `$result->fetch_assoc()` ; thanks @icecub – Joomler Oct 11 '15 at 01:19