Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php json#U编码不';得不到回音';预计起飞时间_Php_.net_Sql Server - Fatal编程技术网

Php json#U编码不';得不到回音';预计起飞时间

Php json#U编码不';得不到回音';预计起飞时间,php,.net,sql-server,Php,.net,Sql Server,我试图用下面的代码从数据库中提取一些记录,但是我的echo json\u encode($contacts)在代码末尾不打印任何内容。任何echo也没有把它放在上面 <?php require_once(dirname(__FILE__).'/ConnectionInfo.php'); //Set up our connection $connectionInfo = new ConnectionInfo(); $connectionInfo->G

我试图用下面的代码从数据库中提取一些记录,但是我的
echo json\u encode($contacts)在代码末尾不打印任何内容。任何
echo
也没有把它放在上面

<?php
    require_once(dirname(__FILE__).'/ConnectionInfo.php');


    //Set up our connection
    $connectionInfo = new ConnectionInfo();
    $connectionInfo->GetConnection();

    if (!$connectionInfo->conn)
    {
        //Connection failed
        echo 'No Connection';
    }

    else
    {
        //Create query to retrieve all contacts
        $query = 'SELECT Numero_Leccion,Titulo_Leccion,Ejemplo_Leccion FROM leccion';

        $stmt = sqlsrv_query($connectionInfo->conn, $query);

        if (!$stmt)
        {
            //Query failed
            echo 'Query failed';
        }

        else
        {
            $contacts = array(); //Create an array to hold all of the contacts
            //Query successful, begin putting each contact into an array of contacts

            while ($row = sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC)) //While there are still contacts
            {
                //Create an associative array to hold the current contact
                //the names must match exactly the property names in the contact class in our C# code.
                $contact = array("Numero_Leccion"=>$row['Numero_Leccion'],"Titulo_Leccion"=>$row['Titulo_Leccion'],"Ejemplo_Leccion"=>$row['Ejemplo_Leccion']);              
                //Add the contact to the contacts array
                array_push($contacts, $contact);
            }
            //Echo out the contacts array in JSON format
            echo json_encode($contacts);
        }
    }
?>

Put
error\u报告(E\u ALL&~E\u通知);ini_集('display_errors','1')位于脚本顶部的某个位置,并查看是否有任何错误

(摘自)

试试这个:

while ($row = sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC))
{
    $contacts[] = array("Numero_Leccion"=>$row['Numero_Leccion'],"Titulo_Leccion"=>$row['Titulo_Leccion'],"Ejemplo_Leccion"=>$row['Ejemplo_Leccion']);              
}
header('Content-Type: application/json');
echo json_encode($contacts);

你的问题是?它不输出任何内容,或
NULL
,或..?如果我将常规echo放置在echo json_encode($contatcts)上方,则它不进行任何回送。输出为空。另一件事是,如果我回显这个“echo json_encode($contact)”,它会打印arraymy query实际上从数据库中检索了4条记录,而我的代码不会回显它。添加了一个实际问题(请参阅注释),更改了标题a bitnop无错误,如果我回显json_encode($contacts),一切都会显示出来;如果我改为执行echo json_encode($contact),则不会返回任何内容;就像一次只使用一个数组一样,它会回显我想要的json格式的最后3条记录,但联系人不起作用。sql上的表似乎出现了问题。我创建了一个新的数组,它与我拥有的一个完全相同,但它起作用了,我不知道为什么。。。