PHP“未选择数据库” [英] PHP "No Database Selected"

查看:103
本文介绍了PHP“未选择数据库”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



在页面的底部,我再次连接获取有关相关的产品。



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

  PHP代码
SELECT的
UPDATE的
$ b HTML b $ b TABLE的
图片的
ect ...

PHP
SELECT's

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


没有选择数据库。


从页面顶部和页面底部都选择连接到相同的数据库e和同一张桌子。



我的代码表明发生错误的代码是:

  $ 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)或死(mysql_error());; 

while($ row = mysql_fetch_array($ commResult))
{
//在这里处理。
}

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

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

连接到数据库
$ dbhandle = mysql_connect($ hostname,$ username,$ password)
或者die(Unable to connect to MySQL);

//选择一个数据库来处理
$ selected = mysql_select_db(databasename,$ dbhandle)
或者die(Could not select databasename);


解决方案

我相信你可能会在如果你使用的是旧的mysql_函数(你不应该真的在使用这个函数)对于新代码,请查看 MySQLi PDO ),那么你可能已经忘记了这行代码:

  mysql_select_db(databaseName); 

或者,如果您使用的是原始查询,则可能会缺少此行:

  mysql_query(USE databaseName); 

虽然在同一个脚本中可能不值得关闭数据库连接,但最好是在整个脚本中保持打开。


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.

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.

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.

$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");

解决方案

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.

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

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.

这篇关于PHP“未选择数据库”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆