1

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");
6
  • Show us the piece of code where you select the database (by mysql_select_db or raw query) Commented Feb 4, 2013 at 10:09
  • your database connection is not established so its give error. check your connection again. Commented Feb 4, 2013 at 10:10
  • it's worth pointing out that the functions named mysql_xxx() are deprecated and not recommended for use any longer. New PHP code should be written to use either the mysqli_xxx() functions or the PDO library. Commented Feb 4, 2013 at 10:14
  • @SDC Thanks for the reply. In most of my code i am using mysql_xxx(); would it just be a case of changing every mysql_xxx() to mysqli_xxx() to keep up with the coding standards? Commented Feb 4, 2013 at 10:19
  • @GlenRobson - mysqli is very similar to mysql, but there are minor differences. The main one is that you must always include the connection variable returned from mysqli_select_db() as an argument for other mysqli funcs, whereas with the old functions it was optional. Commented Feb 4, 2013 at 10:37

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I have updated my question to show my connection code. I am using mysql_select_db() i think i am using mysql_close(); in my code at the top of the page somewhere. Do you think this is what is causing the errors? Would it be safe to just call the mysql_close(); at the bottom of the page after all of my scripts have been called?
@GlenRobson Find out if you're using mysql_close(). If you're closing the connection and not properly reopening it, that will be what the issue probably is.
Ok ill take a look and get back to you.
1

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

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

1 Comment

MySQLi - improved version of MySQL php extension. It is much faster and safer, than mysql.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.