我正在使用 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/