2013-10-10 26 views
0

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

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); 
} 

CODE1
<?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); 
} 
?> 

CODE2
<?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() 
    { 
    } 
} 
?> 

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

,系统显示错误消息

其在表

+0

你在哪里选择要连接的数据库?它看起来并不像在连接中选择实际的数据库。 – Fluffeh

+0

@ Fluffeh我将编辑我的问题,并添加我选择数据库的类 – LebDev

+0

似乎查询选择*从学生给出的问题,你可以在你的数据库中运行这个查询,并确保没有错误? – Jianhong

回答

0

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

如果你正确地切换到mysqli,我敢打赌你的事情会正常工作。为什么?因为mysqli是面向对象的,并且mysql_ *不是,所以你正在构建一个面向对象的程序。

嘿,我敢打赌,你可以在10分钟内将你的代码转到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?)

1

如果第一语句返回真错误,那么整个语句,因此必须从不执行所述第二部分是真实的。

例如:

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

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

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

+0

问题是,查询一定不能给出错误,它必须返回一个响应,我不知道错误在哪里 – LebDev

+0

查询出现只有数据或没有数据,我看你的代码没有数据处理情况,不明白你要求它返回一个错误 – sunysen

+0

如果查询是正确的,它是查询返回学生表中的数据如果不是系统显示此错误消息...问题是查询是正确的,他们是没有语法错误,但系统仍然显示错误消息 – LebDev