|
-
Jul 27th, 2012, 09:36 AM
#1
Thread Starter
Addicted Member
PHP to mySQL Connect to DB
Alright, I am at a very beginner stage of learning PHP and have attempted to find the solution to this problem on my own; but with no luck. So any advice is appreciated.
I have a website that is hosted on Bluehost and have setup a database with tables on the same host, same account. I can login to this database through phpMyAdmin, and did so to setup the tables. So I know my username/password is correct.
The script on my web page is this:
PHP Code:
<?php include 'header.php';
$username ="anaddmin_rmd";
$password = "DidThis65?";
$host = "localhost";
$conn = new mysqli($host, $username, $password);
if (!$conn)
{
die('Could Not Connect to Server: ' . mysql_error());
}
mysql_select_db ("anaddmin_stpsormd") or die ("Could Not Connect to Database:" . mysql_error());
$sql="INSERT INTO options (options_make, options_model, options_ecp, options_bootapp, options_burnapp, options_mcu, options_ch721)
VALUES ('$_POST[make]','$_POST[model]','$_POST[ecp]','$_POST[bootapp]','$_POST[burnapp]','$_POST[mcu]','$_POST[ch721]',)";
if (!mysql_query($sql,$conn))
{
die ('Error: ' . mysql_error());
}
echo "Record added";
?>
Username and Password were altered for security but the format is exactly the same as above, in case it's part of the problem.
So anyway, I have an HTML form on the page with a few text-boxes and when I fill them out and click 'submit', I get this error:
Could Not Connect to Database:Access denied for user 'root'@'localhost' (using password: NO)
On a seperate occassion I have verified that the server connects just fine. I only receive an error when attempting to select the database (name has not been altered).
Any ideas? I'd really appreciate it...
-
Jul 27th, 2012, 09:51 AM
#2
Thread Starter
Addicted Member
Re: PHP to mySQL Connect to DB
I should add that I have created/verified the user specified within the connect code is assigned to the database. I use this same account to access it via phpMyAdmin.
I'm not sure where this root@localhost is coming from since I'm not trying to connect as root.
--
Also, should
PHP Code:
mysql_select_db ("anaddmin_stpsormd") or die ("Could Not Connect to Database:" . mysql_error());
really be
PHP Code:
mysql_select_db ('anaddmin_stpsormd') or die ('Could Not Connect to Database:' . mysql_error());
? I need to learn more about when to use double and single quotes in PHP.
Last edited by Minolwen; Jul 27th, 2012 at 09:55 AM.
-
Jul 27th, 2012, 02:07 PM
#3
Thread Starter
Addicted Member
Re: PHP to mySQL Connect to DB
PHP Code:
<?php include 'header.php'; $user ="anatar_bgg"; $pass = "RatDag52?"; try { $dbh = new PDO('mysql:host=localhost;dbname=ansdain_tsdffd', $user, $pass); //Connect to Database. $dbh->exec("SET CHARACTER SET utf8"); //Set Character Code to UTF8. $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //Catch Connection Errors. $make = $_POST["make"]; $model = $_POST["model"]; $ecp = $_POST["ecp"]; $bootapp = $_POST["bootapp"]; $burnapp = $_POST["burnapp"]; $mcu = $_POST["mcu"]; $ch721 = $_POST["ch721"]; //Establish Variables Submitted by Form. $dbh->beginTransaction(); $dbh->exec("INSERT INTO options (options_make, options_model, options_ecp, options_bootapp, options_burnapp, options_mcu, options_ch721) VALUES ('" . $make . "','" . $model . "','" . $ecp . "','" . $bootapp . "','" . $burnapp . "','" . $mcu . "','" . $ch721 . "')"); $dbh->commit(); //Insert Variables into Database. } catch (Exception $e) { $dbh->rollBack(); echo "Failed: " . $e->getMessage();}
$dbh = null;
?>
Works
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|