有一段代码是mysql函数类,用的是mysqli接口,请大虾们帮忙改成用mysql模块的,谢谢先。你们先把代码粘到UE里面,否则会眼花。我对面向对象和mysql模块都不熟练。
<?php
// file mydb.php
class MyDb {
protected $mysqli;
// constructor (create an object of this class)
function __construct() {
require_once('password.php');
$this->mysqli = @new mysqli($mysqlhost, $mysqluser,
$mysqlpasswd, $mysqldb);
// test whether connection OK
if(mysqli_connect_errno()) {
printf("<p>Sorry, no connection! %s\n</p>",
mysqli_connect_error());
// add any needed HTML code to conclude the code
// (</body></html> etc.)
$this->mysqli = FALSE;
exit();
}
}
// destructor (delete object)
function __destruct() {
$this->close();
}
// explicitly terminate object/connection
function close() {
if($this->mysqli)
$this->mysqli->close();
$this->mysqli = FALSE;
}
// execute SELECT return object field
function queryObjectArray($sql) {
if($result = $this->mysqli->query($sql)) {
if($result->num_rows) {
while($row = $result->fetch_object())
$result_array[] = $row;
return $result_array; }
else
return FALSE;
} else {
printf("<p>Error: %s</p>\n", $this->mysqli->error);
return FALSE;
}
}
// execute SELECT return individual value
// Note: return value for error is -1 (not 0)!
function querySingleItem($sql) {
if ($result = $this->mysqli->query($sql)) {
if ($row=$result->fetch_array()) {
$result->close();
return $row[0];
} else {
return -1;
}
} else {
printf("<p>Error: %s</p>\n", $this->mysqli->error);
return -1;
}
}
// execute SQL command without result (INSERT, DELETE, etc.)
function execute($sql) {
if ($this->mysqli->real_query($sql)) {
return TRUE;
} else {
print $this->mysqli->error;
return FALSE;
}
}
// return insert_id
function insertId() {
return $this->mysqli->insert_id;
}
function show_table($sql){
if ($result = $this->mysqli->query($sql)) {
$n = 0;
if(!$result){
echo "<p>No valid query result.</p>\n";
return;
}
if($result->num_rows>0 && $result->field_count>0){
echo "<table class=\"datalist\">";
//column lables
echo "<tr>";
foreach($result->fetch_fields() as $meta)
printf("<th>%s</th>",htmlspecialchars($meta->name));
echo "</tr>\n";
//table content
while($row = $result->fetch_row()){
if($n % 2 == 1){
echo"<tr>";
}else{
echo"<tr class=\"altrow\">";
}
foreach($row as $col)
printf("<td>%s</td>",htmlspecialchars($col));
echo "</tr>\n";
$n = $n+1;
}
echo "</table>";
}
} else {
printf("<p>Error: %s</p>\n", $this->mysqli->error);
return -1;
}
}
}
?>
<?php
// file mydb.php
class MyDb {
protected $mysqli;
// constructor (create an object of this class)
function __construct() {
require_once('password.php');
$this->mysqli = @new mysqli($mysqlhost, $mysqluser,
$mysqlpasswd, $mysqldb);
// test whether connection OK
if(mysqli_connect_errno()) {
printf("<p>Sorry, no connection! %s\n</p>",
mysqli_connect_error());
// add any needed HTML code to conclude the code
// (</body></html> etc.)
$this->mysqli = FALSE;
exit();
}
}
// destructor (delete object)
function __destruct() {
$this->close();
}
// explicitly terminate object/connection
function close() {
if($this->mysqli)
$this->mysqli->close();
$this->mysqli = FALSE;
}
// execute SELECT return object field
function queryObjectArray($sql) {
if($result = $this->mysqli->query($sql)) {
if($result->num_rows) {
while($row = $result->fetch_object())
$result_array[] = $row;
return $result_array; }
else
return FALSE;
} else {
printf("<p>Error: %s</p>\n", $this->mysqli->error);
return FALSE;
}
}
// execute SELECT return individual value
// Note: return value for error is -1 (not 0)!
function querySingleItem($sql) {
if ($result = $this->mysqli->query($sql)) {
if ($row=$result->fetch_array()) {
$result->close();
return $row[0];
} else {
return -1;
}
} else {
printf("<p>Error: %s</p>\n", $this->mysqli->error);
return -1;
}
}
// execute SQL command without result (INSERT, DELETE, etc.)
function execute($sql) {
if ($this->mysqli->real_query($sql)) {
return TRUE;
} else {
print $this->mysqli->error;
return FALSE;
}
}
// return insert_id
function insertId() {
return $this->mysqli->insert_id;
}
function show_table($sql){
if ($result = $this->mysqli->query($sql)) {
$n = 0;
if(!$result){
echo "<p>No valid query result.</p>\n";
return;
}
if($result->num_rows>0 && $result->field_count>0){
echo "<table class=\"datalist\">";
//column lables
echo "<tr>";
foreach($result->fetch_fields() as $meta)
printf("<th>%s</th>",htmlspecialchars($meta->name));
echo "</tr>\n";
//table content
while($row = $result->fetch_row()){
if($n % 2 == 1){
echo"<tr>";
}else{
echo"<tr class=\"altrow\">";
}
foreach($row as $col)
printf("<td>%s</td>",htmlspecialchars($col));
echo "</tr>\n";
$n = $n+1;
}
echo "</table>";
}
} else {
printf("<p>Error: %s</p>\n", $this->mysqli->error);
return -1;
}
}
}
?>
<?php
// file mydb.php
class MyDb {
protected $mysqli;
// constructor (create an object of this class)
function __construct() {
require_once('password.php');
$this-> mysqli = @new mysqli($mysqlhost, $mysqluser,
$mysqlpasswd, $mysqldb);
// test whether connection OK
if(mysqli_connect_errno()) {
printf(" <p> Sorry, no connection! %s\n </p> ",
mysqli_connect_error());
// add any needed HTML code to conclude the code
// ( </body> </html> etc.)
$this-> mysqli = FALSE;
exit();
}
}
// destructor (delete object)
function __destruct() {
$this-> close();
}
// explicitly terminate object/connection
function close() {
if($this-> mysqli)
$this-> mysqli-> close();
$this-> mysqli = FALSE;
}
// execute SELECT return object field
function queryObjectArray($sql) {
if($result = $this-> mysqli-> query($sql)) {
if($result-> num_rows) {
while($row = $result-> fetch_object())
$result_array[] = $row;
return $result_array; }
else
return FALSE;
} else {
printf(" <p> Error: %s </p> \n", $this-> mysqli-> error);
return FALSE;
}
}
// execute SELECT return individual value
// Note: return value for error is -1 (not 0)!
function querySingleItem($sql) {
if ($result = $this-> mysqli-> query($sql)) {
if ($row=$result-> fetch_array()) {
$result-> close();
return $row[0];
} else {
return -1;
}
} else {
printf(" <p> Error: %s </p> \n", $this-> mysqli-> error);
return -1;
}
}
// execute SQL command without result (INSERT, DELETE, etc.)
function execute($sql) {
if ($this-> mysqli-> real_query($sql)) {
return TRUE;
} else {
print $this-> mysqli-> error;
return FALSE;
}
}
// return insert_id
function insertId() {
return $this-> mysqli-> insert_id;
} function show_table($sql){
if ($result = $this-> mysqli-> query($sql)) {
$n = 0;
if(!$result){
echo " <p> No valid query result. </p> \n";
return;
}
if($result-> num_rows> 0 && $result-> field_count> 0){
echo " <table class=\"datalist\"> ";
//column lables
echo " <tr> ";
foreach($result-> fetch_fields() as $meta)
printf(" <th> %s </th> ",htmlspecialchars($meta-> name));
echo " </tr> \n";
//table content
while($row = $result-> fetch_row()){
if($n % 2 == 1){
echo" <tr> ";
}else{
echo" <tr class=\"altrow\"> ";
}
foreach($row as $col)
printf(" <td> %s </td> ",htmlspecialchars($col));
echo " </tr> \n";
$n = $n+1;
}
echo " </table> ";
}
} else {
printf(" <p> Error: %s </p> \n", $this-> mysqli-> error);
return -1;
}
}
}
?>
这样就不眼花啦