NULL连接失败:PHP


NULL Connect failed: PHP

我正在学习Youtube中的Web服务教程。我遵循了第一个教程。当网页加载(json_code.php)时,我收到一个错误"NULL连接失败:"。有3个php文件。

这是我的json_code.php

<?php
//header('content-Type':application/json');
require_once('StudentDB.php');
require_once('mysqli_connect.php');
$json_data=json_decode('{"first_name"}:"Dale"}');
var_dump($json_data);
class Address{
public $street="";
public $city="";
public $state="";
function __construct($street,$city,$state){
$this->street="$street";
$this->city="$city";
$this->state="$state";
}
}
class Student{
public $first_name="";
public $last_name="";
public $age="0";
public $enrolled="false";
public $married="null";
public $address;
public $phone;
function __construct($first_name,$last_name,$age,$enrolled,$married, $street,$city,$state,$ph_home,$ph_mobile){
    $this->first_name = $first_name;
    $this->last_name=$last_name;
    $this->age=$age;
    $this->enrolled=$enrolled;
    $this->married=$married;
    $this->address=new Address($street,$city,$state) ;
    $this->phone=array("home"=>$ph_home,"mobile"=>$ph_mobile);

}}
$dale_cooper = new Student("Dale", "Cooper", 35, true, null,
        "123 Main St", "Seattle", "WA", "4125551212", "4125552121");
if(mysqli_connect()){
    printf("Connect failed:%s'n",mysqli_connect_error());
    exit();
}
$query="SELECT * FROM students WHERE student_id IN(1,2)";
$student_array=array();
if($result=$dbc->query($query)){
    while ($obj=$result->fetch_object()) {
        printf("%s %s %s %s %s %s %s %s %s %s %s %s %s</br>",
            $obj->first_name,$obj->last_name,$obj->email,
            $obj->street,$obj->city,$obj->state,$obj->zip,
            $obj->phone,$obj->birth_date,$obj->sex,
            $obj->date_entered,$obj->lunch_cost,$obj->student_id);

        $temp_student=new StudentDB($obj->first_name,
            $obj->last_name,$obj->email,$obj->street,$obj->city,$obj->state,$obj->zip,  $obj->phone,$obj->birth_date,$obj->sex, $obj->date_entered,$obj->lunch_cost,$obj->student_id);
        $student_array[]=$temp_student;
        echo "<br/><br/>";
        echo'{"students":[';
        $dale_data=json_conde($student_array[0]);
        echo $dale_data;
        echo ' , <br/>';
        $dale_data=json_encode($student_array[1]);
        echo $dale_data . "<br/>";
        echo']}';
        $result->close;
        $dbc>close();
}}
?>

这是mysqli_connect.php

<?php
DEFINE ('DB_USER', 'root');
DEFINE ('DB_PASSWORD', '');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'studentdb');
$dbc = @mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
OR die('Could not connect to MySQL: ' .
mysqli_connect_error());
?>

这是StudentDB.php

<?php
class StudentDB{
    public $first_name="";
    public $last_name="";
    public $email="";
    public $street="";
    public $city="";
    public $state="";
    public $zip;
    public $phone="";
    public $birth_date="";
    public $sex="";
    public $date_entered="";
    public $lunch_cost="";
    public $student_id="";
function __construct($first_name,$last_name,$email,$street,$city,$state,$zip,$phone,$birth_date,$sex,$date_entered,$lunch_cost,$student_id){
$this->first_name="$first_name";
$this->last_name="$last_name";
$this->email="$email";
$this->street="$street";
$this->city="$city";
$this->state="$state";
$this->zip="$zip";
$this->phone="$phone";
$this->birth_date="$birth_date";
$this->sex="$sex";
$this->date_entered="$date_entered";
$this->lunch_cost="$lunch_cost";
$this->student_id="$student_id";
}}
?>

有点打字错误:

$json_data=json_decode('{"first_name"}:"Dale"}');

应该是

$json_data=json_decode('{"first_name":"Dale"}');


又有一个小打字错误。。

if(mysqli_connect()){
    printf("Connect failed:%s'n",mysqli_connect_error());
    exit();
}

应该是

if(!mysqli_connect()){
    printf("Connect failed:%s'n",mysqli_connect_error());
    exit();
}

(注意


$dale_data=json_conde($student_array[0]);

应该是

$dale_data=json_encode($student_array[0]);


整个区块可能不在while循环中。。

    echo "<br/><br/>";
    echo'{"students":[';
    $dale_data=json_conde($student_array[0]);
    echo $dale_data;
    echo ' , <br/>';
    $dale_data=json_encode($student_array[1]);
    echo $dale_data . "<br/>";
    echo']}';
    $result->close;
    $dbc>close();