gpt4 book ai didi

PHP:未选择数据库

转载 作者:可可西里 更新时间:2023-10-31 23:53:04 24 4
gpt4 key购买 nike

我无法理解为什么连接到数据库的功能不起作用,我已经三重检查了变量是否有任何错误。

在第 12 行的 class.php 上使用 mysql_error 函数我得到以下错误:

No database selected

类.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, $username, $password, $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
?>

索引.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, $username, $password, $db){
$this->db = $db;
$this->link = mysql_connect($host, $username, $password, $db);
mysql_select_db($this->db, $this->link) or die (mysql_error());
}

关于PHP:未选择数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7155553/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com