简体   繁体   中英

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?

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);

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM