我以数组形式从数据库传递数据,然后我想在php中显示这个数组

时间:2015-06-13 14:05:45

标签: php arrays database

如何在此功能中使用$objects
我以数组形式从数据库传递数据,然后我想在PHP页面中显示这个数组,但它打印这个单词“array”。

我想以这种格式打印 - > [['Trident','Internet Explorer 4.0','Win 95 +','4','X'],['Trident','Internet Explorer 5.0','Win 95 +','5','C '],['Trident','Internet Explorer 5.5','Win 95 +','5.5','A']] 代码:

    function showing_daily_basket(){

    $connect_mysql= @mysql_connect($server,$username,$passwor) or die ("Connection Failed!");
    $mysql_db=mysql_select_db("GP15",$connect_mysql) or die ("Could not Connect to Database");
    $query = "SELECT * FROM basket_daily_work";
    $result=mysql_query($query) or die("Query Failed : ".mysql_error());
            $objects= array();
    while($rows=mysql_fetch_array($result))
    {
                    $objects[]= $rows;
    }
    exit($objects);
 }
  

如果我粘贴这个var_dump($ objects);'在while之后循环这个结果   块引用

the result

编辑后,它什么都不返回

$connect_mysql= @mysql_connect($server,$username,$passwor) or die ("Connection Failed!");
$mysql_db=mysql_select_db("GP15",$connect_mysql) or die ("Could not Connect to Database");
$query = "SELECT * FROM basket_daily_work";
$result=mysql_query($query) or die("Query Failed : ".mysql_error());
            $objects= array();
    while($rows=mysql_fetch_array($result))
    {
                    $objects[]= $rows;
    }
	var_dump($objects);
$data_set = "[";
$count_rows = count($objects);
$count = 1;
foreach($objects as $object){
    $data_set .= "['". $object['basketID'] ."', '". $object['date'] ."', '". $object['time'] ."', '". $object['flag'] ."']";
    if($count != $count_rows){
        $data_set .= ",";
        $count++;
    }
}
$data_set .= "]";
echo $data_set;
	

2 个答案:

答案 0 :(得分:1)

查询中返回的每一行都会在$objects内有一个数组。所以你可以遍历这些,然后对结果做一些事情。

foreach($objects as $object){
    echo $object['column_name'];
}

此代码将回显为返回的每一行指定的列名的值。

在对结果做一些事情之前查看$对象结构的好方法是

print "<pre>";
var_dump($objects);

编辑 - 试试这个

$data_set = "[";
    $count_rows = count($objects);
    $count = 1;

    foreach($objects as $object){
        $data_set .= "['". $object['basketID'] ."', '". $object['date'] ."', '". $object['time'] ."', '". $object['flag'] ."']";

        if($count != $count_rows){
            $data_set .= ",";
            $count++;
        }
    }

    $data_set .= "]";

答案 1 :(得分:1)

请尝试print_r($ objects)。

&#34; print_r - 打印有关变量的人类可读信息&#34; - 请参阅PHP documentation