Moose
Moose

Reputation: 45

Printing a PHP Array from MySQL table

I am trying to print all the items in a row from a particular table from a mysql database.

Here is the out put I am getting (which is not what I want):

**mysqli_result Object ( [current_field] => 0 [field_count] => 3 [lengths] => [num_rows] => 28 [type] => 0 ) Array ( )**

Here is how I am doing it:

<?php
    $connect = mysqli_connect("localhost", "root", "root");
    mysqli_select_db($connect, "TrackTool");

    $fetch1 = mysqli_query($connect, "SELECT * FROM SalesForceActions");

    // set array
    $array = array();

    // look through query
    while($row = mysql_query($fetch1)){

        // add each row returned into an array
        $array[] = $row;

    }

    print_r($fetch1);
    print_r($array);

    mysqli_close($connect);

?>

What am I doing wrong?

Upvotes: 2

Views: 15145

Answers (2)

Jason McCreary
Jason McCreary

Reputation: 72991

You need to fetch the result using mysqli_fetch_assoc() (or equivalent):

$array = array();
while($row = mysqli_fetch_assoc($fetch1)) {
  $array[] = $row;
}
print_r($array);

If you indeed want all the rows, take a look mysqli_fetch_all():

$array = mysqli_fetch_all($fetch1, MYSQLI_ASSOC);
print_r($array);

Upvotes: 3

immulatin
immulatin

Reputation: 2118

You need to fetch the result after running the query. So you execute the query then loop which fetching from those results

Like so:

$fetch1 = mysqli_query($connect, "SELECT * FROM SalesForceActions");

$array = array();
while($row = mysqli_fetch_assoc($fetch1)){
    $array[] = $row;
}

You were also mixing mysqli_* functions with mysql_* functions. Be sure to NOT mix those. In fact, just use mysqli_* functions from now on as the mysql_* functions are being deprecated.

Upvotes: 5

Related Questions