PHP“未选择数据库”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14684417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:48:05  来源:igfitidea点击:

PHP "No Database Selected"

phphtmlmysql

提问by Glen Robson

I have recently started creating a website in PHP. Most of my code is working however I have found a problem that i am having trouble correcting.

我最近开始用 PHP 创建一个网站。我的大部分代码都在工作,但是我发现了一个我无法纠正的问题。

I have a page where there are lots of calls to the database. at the top of my page I am connecting and using a SELECT to get some information on the related product.

我有一个页面,其中有很多对数据库的调用。在我的页面顶部,我正在连接并使用 SELECT 来获取有关相关产品的一些信息。

At the bottom of the page I am connecting again getting comments on the related product.

在页面底部,我再次连接以获取对相关产品的评论。

My code is very lengthy so it is hard for me to show in here but the basics of this code are as follows:

我的代码很长,所以我很难在这里展示,但这段代码的基础如下:

PHP CODE
  SELECT's
  UPDATE's

HTML
  TABLE's
  IMAGE's
  ect...

PHP
  SELECT's

My code successfully runs all of my code when I first load up the page however when I click a button to update a table (in the top section of the code) the update is processed however the comments section at the bottom of the page brings back the following errer:

当我第一次加载页面时,我的代码成功运行了我的所有代码,但是当我点击一个按钮来更新一个表格(在代码的顶部)时,更新被处理但是页面底部的评论部分带回来以下错误:

"no database selected."

“未选择数据库。”

Both selects from the top of the page and the bottom of the page are connection to the same database and same table. Also the comments section that is bringing back the error works fine before I click the update button.

页面顶部和页面底部的选择都连接到同一个数据库和同一个表。在我单击更新按钮之前,带回错误的评论部分也能正常工作。

The code where my code says the error is happening is:

我的代码说发生错误的代码是:

$commResult = mysql_query("SELECT u.id, u.USERNAME, c.COMMENT, c.DATE_ADDED, c.ACTIVE FROM USERS u, COMMENTS c WHERE c.box_id = $boxId ORDER BY c.DATE_ADDED DESC") or die (mysql_error());;

while ($row = mysql_fetch_array($commResult))
{
     //Do processing here.
}

My code for connection to the database. This is in a seperate file and i use a require_once(config.php); to call it at the top of every page that my code needs to connect to the database.

我的连接数据库的代码。这是在一个单独的文件中,我使用 require_once(config.php); 在我的代码需要连接到数据库的每个页面的顶部调用它。

$username = "user";
$password = "password";
$hostname = "localhost"; 

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password) 
or die("Unable to connect to MySQL");

//select a database to work with
$selected = mysql_select_db("databasename",$dbhandle) 
or die("Could not select databasename");

回答by EM-Creations

I believe you may be opening a MySQL connection at the top of the script, closing it and reopening it without forgetting to select the database.

我相信您可能会在脚本顶部打开一个 MySQL 连接,关闭它并重新打开它而不会忘记选择数据库。

If you're using the old mysql_ functions (which you shouldn't really be using for new code anymore, take a look at MySQLiand PDO) then you may have forgotten this line of code:

如果您正在使用旧的 mysql_ 函数(您不应该再将其用于新代码,请查看MySQLiPDO),那么您可能已经忘记了这行代码:

mysql_select_db("databaseName");

Or, if you're using "raw queries", you may be missing this line:

或者,如果您使用“原始查询”,您可能会错过这一行:

mysql_query("USE databaseName");

Although on the same script it's probably not worth closing the database connection, it might be a better idea to keep it open throughout the script.

尽管在同一个脚本上关闭数据库连接可能不值得,但在整个脚本中保持打开它可能是一个更好的主意。

回答by Xazzzi

Did you call mysql_select_db("Your Database Name")or mysql_query("Your Database Name", "Query")?

你打电话mysql_select_db("Your Database Name")还是mysql_query("Your Database Name", "Query")

And yes, you better don't use mysql, use mysqli instead.

是的,你最好不要使用 mysql,而是使用 mysqli。