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");
mysql_select_dbor raw query)mysql_xxx()are deprecated and not recommended for use any longer. New PHP code should be written to use either themysqli_xxx()functions or the PDO library.mysqliis very similar tomysql, but there are minor differences. The main one is that you must always include the connection variable returned frommysqli_select_db()as an argument for othermysqlifuncs, whereas with the old functions it was optional.