0

我正在使用 php 和 mysql 来处理 web 开发示例,但问题是系统显示有关选择数据库的错误消息有人可以帮我解决这个问题吗???

客户端.js

function GetAllStudents()
{
    var formRequest = new FormData();
    formRequest.append('getStudents', "getAllStudents");

    var xhr = new XMLHttpRequest();
    xhr.addEventListener("load", uploadComplete, false);
    xhr.open("GET", 'StudentService/getAllStudents.php');
    xhr.send(formRequest);
}


function GetStudentByID(id)
{
    var formRequest = new FormData();
    formRequest.append('sid', id);

    var xhr = new XMLHttpRequest();
    xhr.addEventListener("load", uploadComplete, false);
    xhr.open("POST", 'StudentService/getStudentByID.php');
    xhr.send(formRequest);
}

function uploadComplete(evt)
{
    console.log(evt.target.responseText);
}

代码1

<?php
 require_once('../ConnectionManager.php');


$response = array();

$db = ConnectionManager::getInstance();

$result = mysql_query("SELECT * FROM student") or die("their was an error in table");

if(mysql_num_rows($result) >0)
{
    $response["student"] = array();

    while($row = mysql_fetch_array($result))
    {
        $student = array();
        $student["ID"] = $row["ID"];
        $student["Index"] = $row["Index"];
        $student["Name"] = $row["Name"];

        array_push($response["student"], $student);
    }
    $response["success"] = 1;
    echo json_encode($response);
}
else
{
    $response["success"] = 0;
    $response["message"] = "No Students Found!!";

    echo  json_encode($response);
}
?>

代码2

<?php
require_once 'Connection.php';
class ConnectionManager
{
    static $connection = null;
    public static function getInstance()
    {
        if(ConnectionManager::$connection = null)
            ConnectionManager::$connection = new Connection();

         return ConnectionManager::$connection;
    }

    private function __construct()
    {
    }
    private function __clone()
    {
    }
}
?>

代码3

<?php
class Connection
{
    function __construct()
    {
        $this->connect();
    }

    function __destruct()
    {
        $this->close();
    }

    function connect()
    {
     require_once('db_config.php'); 


        $connection = mysql_connect(SERVER, USER, PASSWORD) or die(mysql_error());

        $dbConnect = mysql_select_db(DATABASE) or die("their was an error in the databse!!!");
        return $connection;
    }

    function close()
    {
        mysql_close();
    }
}
?>

系统显示错误信息

他们是表中的错误

4

2 回答 2

1

如果第一个语句返回 true,那么整个语句必须为 true,因此第二部分永远不会执行。

例如:

$x = 3;
true or $x++;
echo $x;  // 3

false or $x++;
echo $x; // 4

因此,如果您的查询不成功,它将评估 die() 语句并结束脚本。

于 2013-10-10T23:23:01.363 回答
0

简单的答案是您有范围问题:您的数据库选择必须发生在正确的范围内。但是,正确的答案是,不要使用 mysql_* 。它已被弃用。它已经被弃用多年了。如果您费心查看 PHP 手册,您就会知道这一点。切换到mysqli真的不难。

如果您正确地将所有内容切换到 mysqli,我敢打赌您一切都会好起来的。为什么?因为 mysqli 是面向对象的,而 mysql_* 不是,而且你正在构建一个面向对象的程序。

嘿,我敢打赌,我可以在不到十分钟的时间内把你的代码交给 mysqli。开始了:

class Connection
{
   private $mysqli;
function __construct()
{
}

function __destruct()
{
     $this->close();
}

function getMysqli()
{
    if(!isset($this->mysqli)) {
        require_once('db_config.php'); 

        $this->mysqli = new mysqli(SERVER, USER, PASSWORD, DATABASE);
        if($this->mysqli->connect_error) {
            die($this->mysqli->connect_error);
        }
    }
    return $mysqli;
}

function close()
{
    if(isset($this->mysqli)) {
        $this->mysqli->close();
    }
}
}

其次是这个(虽然我什至不确定你为什么想要这个):

require_once 'Connection.php';
class ConnectionManager
{
static $connection = null;
public static function getInstance()
{
    if(ConnectionManager::$connection = null)
        ConnectionManager::$connection = new Connection();

     return ConnectionManager::$connection->getMysqli();
}

private function __construct()
{
}
private function __clone()
{
}
}

紧随其后的是:

 require_once('../ConnectionManager.php');

$response = array();

$db = ConnectionManager::getInstance();

$result = $db->query("SELECT * FROM student");
if($db->error) {
    die($db->error);
}

if($result->num_rows >0)
{
    $response["student"] = array();

    while($row = $result->fetch_assoc())
    {
    $student = array();
    $student["ID"] = $row["ID"];
    $student["Index"] = $row["Index"];
    $student["Name"] = $row["Name"];

    array_push($response["student"], $student);
    }
    $response["success"] = 1;
    echo json_encode($response);
}
else
{
    $response["success"] = 0;
    $response["message"] = "No Students Found!!";

    echo  json_encode($response);
}

看那个。我花了大约八分钟。如果您仍然遇到错误,请告诉我。如果我有拼写错误或其他什么,我不会感到惊讶,尤其是您尝试在代码 2 中执行的静态实现。(为什么不跳过代码 3 并在代码 2 中创建您的 mysqli?)

于 2013-10-11T00:08:35.417 回答