php - 使用 php/mysql 系统显示错误消息

标签 php mysql select

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

client.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();
    }
}
?>

系统显示错误信息

表格中有错误

最佳答案

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

例如:

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

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

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

关于php - 使用 php/mysql 系统显示错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19307800/

相关文章:

php - 使用 MySQL 的物化 View (汇总表)的首选方法

mySQL 仅选择包含任何顺序的一些字母的数据

sql - 如何检索具有另一个公共(public)字段的行(字段中具有最大值)?

php - 错误未记录到php错误日志中

php - CodeIgniter:DB Insert 返回 '1' 但表中没有任何内容

java - 将自动增量从默认值重置为大数时出现问题

javascript - 所选 HTML 不适用于 Angular 6

php - 安装 laravel ( sh : 1: composer: not found) error

php - 如何为图表创建动态颜色列表

mysql - Java和mySQL从相关数据库索引中选择