PHP:未选择数据库

发布于 2024-12-01 01:22:57 字数 1548 浏览 1 评论 0原文

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

在第 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, $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
?>

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>

I'm failing to understand why the function to connect to the database is not working, I have triple checked if there are any mistakes in the variables.

Using mysql_error function on class.php at line 12 I get the following 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, $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
?>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无名指的心愿 2024-12-08 01:22:57

您正在使用 $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());
}

You're using $this->db but never setting it. Try this:

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());
}
两人的回忆 2024-12-08 01:22:57

您无法设置$this->db

You are failing to set $this->db.

独留℉清风醉 2024-12-08 01:22:57

首先

mysql_select_db($this->db, $this->link) or die (mysql_error());

应该是:

mysql_select_db($db, $this->link) or die (mysql_error());

并且

$res = mysql_query($sql);

应该是

$res = mysql_query($sql, $this->link);

First of all

mysql_select_db($this->db, $this->link) or die (mysql_error());

should be:

mysql_select_db($db, $this->link) or die (mysql_error());

and

$res = mysql_query($sql);

should be

$res = mysql_query($sql, $this->link);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文