我正在使用MySQL数据库,并在尝试从几个内置的PHP MySQL方法中获取数据时遇到一些问题。我编写了一个与数据库交互的类,以下是相关的部分:
<?php
include("includes/Config.inc.php");
include_once("ChromePHP.class.php");
class Database{
private $db;
private $hostname;
private $username;
private $password;
private $schema;
function __construct() {
if(func_num_args() == 0){
$this->hostname = conf_hostname;
$this->username = conf_username;
$this->password = conf_password;
$this->schema = conf_schema;
}
else{
$params = func_get_args();
$this->hostname = $params[0];
$this->username = $params[1];
$this->password = $params[2];
$this->schema = $params[3];
}
$this->open();
}
private function open(){
$this->db = mysql_connect($this->hostname, $this->username, $this->password) or die ('Error connecting to mysql');
mysql_select_db($this->schema, $this->db);
mysql_query("SET NAMES utf8");
}
public function executeQuery($query){
$results = mysql_query($query, $this->db) or die ("Error in query: $query. ".mysql_error());
return $results;
}
public function executeNonQuery($query){
mysql_query($query, $this->db) or die ("Error in query: $query. ".mysql_error());
$info = mysql_info($this->db);
if($info){
$bits = explode(' ', $info);
return $bits[4];
}
return false;
}
public function close(){
mysql_close($this->db);
}
public function escape($string){
$output = mysql_real_escape_string($string , $this->db);
return $output;
}
public function getRegionTree(){
$query = "SELECT COUNT(parent.Name) - 2 as level, node.Name AS Name, node.ID, node.Parent
FROM Region AS node, Region AS parent
WHERE node.LeftVal BETWEEN parent.LeftVal AND parent.RightVal and node.Name <> 'Earth'
GROUP BY node.ID
ORDER BY node.LeftVal";
$result = $this->executeQuery($query);
$last_level = 0;
$output = '<ul id="regionTree">'.PHP_EOL;
while ($row = mysql_fetch_assoc($result)) {
$link = '<li>'.PHP_EOL.'<a href="#" data-self="'.$row["ID"].'" data-parent="'.$row["Parent"].'">'.$row["Name"]."</a>".PHP_EOL;
$diff = $last_level - $row["level"];
if($diff == 0){
// Sibling
$output .= ($row["level"] != 0) ? '</li>'.PHP_EOL.$link:$link;
}
elseif($diff < 0){
// Child
$demoter = '<ul>'.PHP_EOL;
for ($i=0; $i > $diff; $i--) {
$output .= $demoter;
}
$output .= $link;
}
else{
// Parent
$promoter = '</li>'.PHP_EOL.'</ul>';
for ($i=0; $i < $diff; $i++) {
$output .= ($row["level"] != 0) ? $promoter.PHP_EOL."</li>":$promoter;
}
$output .= $link;
}
$last_level = $row["level"];
}
$output .= "</li></ul>";
return $output;
}
public function addRegion($name, $type, $parentID){
$query = "select Name, Region_Type from Region where ID = ".$parentID;
$result = $this->executeQuery($query);
if($result){
$row = mysql_fetch_assoc($result);
$query = "call AddRegion('".$name."', '".$type."', '".$row["Name"]."', '".$row["Region_Type"]."', @returnCode, @returnMessage)";
$result = $this->executeQuery($query);
if($result){
return true;
}
else{
$query = "select @returnCode as code, @returnMessage as message";
$result = $this->executeQuery($query);
while($row = mysql_fetch_assoc($result)){
print_r($row);
}
return false;
}
}
return false;
}
public function getInfo(){
return mysql_info();
}
public function editRegion($id, $name, $type){
$query = "update Region set Name = '$name', Region_Type = '$type' where ID = $id";
if($this->executeNonQuery($query) > 0){
return true;
}
return false;
}
}
$db = new Database();
?>
使用此Database类,我可以使用ExecuteNonQuery函数成功进行查询,获取区域树,并成功更改数据库中的数据。但是,我使用mysql_info,mysql_affected_rows或其他类似函数的任何尝试都失败了,这使得编写任何错误处理代码变得非常困难。更糟糕的是,如果我运行以下代码:
<?php
$db = mysql_connect("localhost", "User", "Password") or die ('Error connecting to mysql');
mysql_select_db("DB", $db);
mysql_query("update Region set Name = 'test' where ID = 594", $db);
echo mysql_info($db);
?>
我能够按预期获得结果。任何想法?
答案 0 :(得分:0)
你在Database类之外使用mysql_info和mysql_error吗?如果是这样,您如何引用数据库连接?如果您没有识别数据库连接,那么:
MySQL连接。如果未指定链接标识符,则为最后一个 假设由mysql_connect()打开的链接。如果没有找到这样的链接, 它会尝试创建一个,就像调用mysql_connect()一样 参数。如果未找到或建立连接,则为E_WARNING 生成级别错误。
所以内部你应该做mysql_info($this->db);
来获取mysql_info。从外部访问$ db需要你写一些东西来解决它private
。
但是如果你要在面向对象模式下做数据库工作,你真的应该调查PDO。
答案 1 :(得分:0)
在我的脚本中的几个地方添加“或die()”语句之后,我惊讶地发现没有任何内容实际上无法正常工作,相反它没有像我预期的那样工作。我遇到的问题是,拆分mysql_info的输出(在空格字符上)导致数组中的几个索引不包含任何内容,因为mysql_info输出中的不一致空格会生成这些索引。我认为的索引(基于视觉上的空间计算)将包含一个整数,而不是包含一个字符串,我试图将该字符串与数字0进行比较,这将无效。我更新了我的executeNonQuery函数,如下所示:
public function executeNonQuery($query){
mysql_query($query, $this->db) or die ("Error in query: $query. ".mysql_error());
$info = mysql_info($this->db);
if($info){
$output = array();
$bits = explode(' ', $info);
$output["rowsMatched"] = $bits[2];
$output["rowsChanged"] = $bits[5];
$output["warnings"] = $bits[8];
return $output;
}
return false;
}