简体   繁体   English

从MySQL表打印PHP数组

[英]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. 我正在尝试从mysql数据库的特定表中连续打印所有项目。

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): 您需要使用mysqli_fetch_assoc() (或等效方法) 获取结果:

$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() : 如果确实需要所有行,请查看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. 您还将mysqli_ *函数与mysql_ *函数混合在一起。 Be sure to NOT mix those. 确保不要将它们混合在一起。 In fact, just use mysqli_* functions from now on as the mysql_* functions are being deprecated. 实际上,由于不建议使用mysql_ *函数,因此从现在开始仅使用mysqli_ *函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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