PHP:未选择数据库
发布时间:2020-12-13 22:15:54 所属栏目:PHP教程 来源:网络整理
导读:我无法理解为什么连接到数据库的功能不起作用,我已经三次检查变量是否有任何错误. 在第12行的class.php上使用mysql_error函数我收到以下错误: No database selected class.php ?phpclass blog { private $host; private $username; private $password; priv
我无法理解为什么连接到数据库的功能不起作用,我已经三次检查变量是否有任何错误.
在第12行的class.php上使用mysql_error函数我收到以下错误: No database selected class.php <?php class blog { private $host; private $username; private $password; private $db; private $link; public function __construct($host,$username,$password,$db){ $this->link = mysql_connect($host,$db); mysql_select_db($this->db,$this->link) or die (mysql_error()); } function get_content(){ $sql = "SELECT * FROM content"; $res = mysql_query($sql); while($row = mysql_fetch_assoc($res)){ echo '<h1>'.$row['title'].'</h1>'; echo '<p>'.$row['body'].'</p>'; } } }// End of Class ?> 的index.php <?php include 'includes/class.php' ?> <?php //Setup Connection $obj = new blog('localhost','root','','blog'); //Connect to DB ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <link rel="stylesheet" type="text/css" href="css/style.css" mce_href="styles1.css"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Mi Blog</title> </head> <body> <div id="page-wrap"> <?php $obj->get_content() ?> </div> </body> </html> 解决方法
您正在使用$this-> db但从未设置它.试试这个:
public function __construct($host,$db){ $this->db = $db; $this->link = mysql_connect($host,$this->link) or die (mysql_error()); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |