PHP数据库选择问题
我对 PHP 的更新感到有点困惑,自从我上次用 PHP 编码以来已经有大约 3 年了。欢迎任何见解!我将向您提供尽可能多的信息来解决此错误,所以就开始吧!
文件
- config.php
- database.php
- news.php
- BLnews.php
- index.php
包含
- config.php -> news.php
- 数据库.php -> news.php
- news.php -> BLnews.php
- BLnews.php -> index.php
现在,我当前代码的问题是正在建立数据库连接,但我的数据库拒绝被选择。我的查询应该可以工作,但是由于我的数据库没有被选择,所以进行任何数据交换有点烦人!
config.php
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "test";
?>
database.php
<?php
class Database {
//-------------------------------------------
// Connects to the database
//-------------------------------------------
function connect() {
if (isset($dbhost) && isset($dbuser) && isset($dbpass) && isset($dbname)) {
$con = mysql_connect($dbhost, $dbuser, $dbpass) or die("Could not connect: " . mysql_error());
$selected_db = mysql_select_db($dbname, $con) or die("Could not select test DB");
}
}// end function connect
} // end class Database
?>
News.php
<?php
// include the config file and database class
include 'config.php';
include 'database.php';
...
?>
BLnews.php
<?php
// include the news class
include 'news.php';
// create an instance of the Database class and call it $db
$db = new Database;
$db -> connect();
class BLnews {
function getNews() {
$sql = "SELECT * FROM news";
if (isset($sql)) {
$result = mysql_query($sql) or die("Could not execute query. Reason: " .mysql_error());
}
return $result;
}
?>
index.php
<?php
...
include 'includes/BLnews.php';
$blNews = new BLnews();
$news = $blNews->getNews();
?>
...
<?php
while($row = mysql_fetch_array($news))
{
echo '<div class="post">';
echo '<h2><a href="#"> ' . $row["title"] .'</a></h2>';
echo '<p class="post-info">Posted by <a href="#"> </a> | <span class="date"> Posted on <a href="#">' . $row["date"] . '</a></span></p>';
echo $row["content"];
echo '</div>';
}
?>
好吧,这几乎是应该获取信息的所有内容,但是由于 $result = mysql_query($sql) 中的 mysql_error 或 die("无法执行查询。原因:" .mysql_error());
我可以看到错误并且它说:
无法执行查询。原因:未选择数据库
我真的不知道为什么它不起作用,而且我已经摆弄它很长一段时间了。非常欢迎您的帮助,我提前感谢您!
问候
柠檬
I'm in a bit of a pickle with freshening up my PHP a bit, it's been about 3 years since I last coded in PHP. Any insights are welcomed! I'll give you as much information as I possibly can to resolve this error so here goes!
Files
- config.php
- database.php
- news.php
- BLnews.php
- index.php
Includes
- config.php -> news.php
- database.php -> news.php
- news.php -> BLnews.php
- BLnews.php -> index.php
Now the problem with my current code is that the database connection is being made but my database refuses to be selected. The query I have should work but due to my database not getting selected it's kind of annoying to get any data exchange going!
config.php
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "test";
?>
database.php
<?php
class Database {
//-------------------------------------------
// Connects to the database
//-------------------------------------------
function connect() {
if (isset($dbhost) && isset($dbuser) && isset($dbpass) && isset($dbname)) {
$con = mysql_connect($dbhost, $dbuser, $dbpass) or die("Could not connect: " . mysql_error());
$selected_db = mysql_select_db($dbname, $con) or die("Could not select test DB");
}
}// end function connect
} // end class Database
?>
News.php
<?php
// include the config file and database class
include 'config.php';
include 'database.php';
...
?>
BLnews.php
<?php
// include the news class
include 'news.php';
// create an instance of the Database class and call it $db
$db = new Database;
$db -> connect();
class BLnews {
function getNews() {
$sql = "SELECT * FROM news";
if (isset($sql)) {
$result = mysql_query($sql) or die("Could not execute query. Reason: " .mysql_error());
}
return $result;
}
?>
index.php
<?php
...
include 'includes/BLnews.php';
$blNews = new BLnews();
$news = $blNews->getNews();
?>
...
<?php
while($row = mysql_fetch_array($news))
{
echo '<div class="post">';
echo '<h2><a href="#"> ' . $row["title"] .'</a></h2>';
echo '<p class="post-info">Posted by <a href="#"> </a> | <span class="date"> Posted on <a href="#">' . $row["date"] . '</a></span></p>';
echo $row["content"];
echo '</div>';
}
?>
Well this is pretty much everything that should get the information going however due to the mysql_error in $result = mysql_query($sql) or die("Could not execute query. Reason: " .mysql_error());
I can see the error and it says:
Could not execute query. Reason: No database selected
I honestly have no idea why it would not work and I've been fiddling with it for quite some time now. Help is most welcomed and I thank you in advance!
Greets
Lemon
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在函数中使用的值未设置值。您可能需要将使用的变量转换为
$this->dbName
等,或者以其他方式为使用的变量赋值。编辑有关 config.php 中定义的变量的用户评论:
您确实应该尝试获取适合该类中每个类的数据。最终,您的变量可用于整个应用程序,此时无法判断变量是否被包含
config.php
的文件更改,但在调用database.php
之前。我将使用调试工具并在调用之前验证变量的值或仅验证 var_dump() 变量的值。
The values you use in your functions aren't set with a value. You likely need to convert the variables used to
$this->dbName
etc or otherwise assign values to the variables used.Edit for users comment about variables defined in
config.php
:You really should attempt to get the data appropriate for each class inside that class. Ultimately your variables are available to your entire app, there's no telling at this point if the variable was changed by a file including
config.php
but beforedatabase.php
is called.I would use a debugging tool and verify the values of the variables or just
var_dump()
them before the call.您的数据库类方法 connect 和 selectDb 尝试从未设置的变量($dbhost、$dbname、$con 等)中读取。您可能希望将这些值传递给构造函数并将它们设置为类属性。更好的是,研究 PDO(或 ORM)并忘记创建自己的数据库类。
Your Database class methods connect and selectDb try to read from variables that are not set ($dbhost, $dbname, $con, etc). You probably want to pass those values to a constructor and set them as class properties. Better yet, look into PDO (or an ORM) and forget creating your own db class.