Displaying data using MySQL class in PHP -
i know how display data without using class, have been asked use mysql class instead. seems have initiate class , use written functions, whatever try display - doesn't work. getting "resource id #14", means should connection. please help!
this part of mysql class:
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 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 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; } ....
and php code:
<?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 * job"; $db->query($sql); $output = $db->query_result; $result = $db->fetch_row(); $smarty->assign('rez', $result); $smarty->assign('output', $output); $smarty->display('index.tpl'); ?>
Comments
Post a Comment