我知道如何在不使用类的情况下显示数据,但我被要求使用mysql类。似乎我必须启动课程并只使用书面函数,但无论我试图显示什么 - 它都不起作用。我得到的只是“资源ID#14”,这意味着一切都应该与连接良好。请帮忙!
这是MySQL类的一部分:
class SQL //MySQL
{
var $link_id;
var $query_result;
var $num_queries = 0;
function connect($db_host, $db_username, $db_password, $db_name = '', $use_names = '', $pconnect = false, $newlink = true) {
if ($pconnect) $this->link_id = @mysql_pconnect($db_host, $db_username, $db_password);
else $this->link_id = @mysql_connect($db_host, $db_username, $db_password, $newlink);
if (!empty($use_names)) $this->query("SET NAMES '$use_names'");
if ($this->link_id){
if($db_name){
if (@mysql_select_db($db_name, $this->link_id)) return $this->link_id;
else die(mysql_error());// = "Can't open database ('$db_name')";
if (!empty($use_names)) $this->query("SET NAMES '$use_names'");
}
} else die(mysql_error());// = "Can't connect to mysql server";
}
function db($db_name) {
if ($this->link_id){
if (@mysql_select_db($db_name, $this->link_id)) return $this->link_id;
else die(mysql_error());// = "Can't open database ('$db_name')";
} else die(mysql_error());// = "Can't connect to database";
}
function query($sql){
$this->query_result = @mysql_query($sql, $this->link_id);
if ($this->query_result){
++$this->num_queries;
return $this->query_result;
} else {
$error = "
<pre>
QUERY: \n {$sql} \n\n
ERROR: <span style=\"color:red\">" . mysql_error() . " </span>
</pre>
";
die($error);
}
}
function result($query_id = 0, $row = 0, $field = NULL){
return ($query_id) ? @mysql_result($query_id, $row, $field) : false;
}
function fetch_row($query_id = 0){
return ($query_id) ? @mysql_fetch_row($query_id) : false;
}
function fetch_array($query_id = 0){
return ($query_id) ? @mysql_fetch_array($query_id) : false;
}
function fetch_assoc($query_id = 0){
return ($query_id) ? @mysql_fetch_assoc($query_id) : false;
}
function num_rows($query_id = 0){
return ($query_id) ? @mysql_num_rows($query_id) : false;
}
....
和我的PHP代码:
<?php
require_once('C:/wamp/www/smarty-3.1.21/libs/Smarty.class.php');
include('C:/wamp/www/smarty-3.1.21/libs/Mysql.class.php');
$smarty = new Smarty();
$smarty->enableSecurity();
$smarty->setTemplateDir('C:\wamp\www\forum\templates');
$smarty->setCompileDir('C:\wamp\www\forum\templates_c');
$smarty->setConfigDir('C:\wamp\www\forum\configs');
$smarty->setCacheDir('C:\wamp\www\forum\cache');
$db = new SQL();
$db->connect('localhost','root','','job', '', false, true);
$db->db('job');
$sql = "SELECT * FROM job";
$db->query($sql);
$output = $db->query_result;
$result = $db->fetch_row();
$smarty->assign('rez', $result);
$smarty->assign('output', $output);
$smarty->display('index.tpl');
?>