I have recently deployed my new website, I use GoDaddy Unlimited Hosting and I'am unable to connect to the database for some reason.
Here's my code for connecting my website to the database:
<?php
ob_start();
session_start();
// db properties
define('DBHOST','some.example.ip.Address');
define('DBUSER','username');
define('DBPASS','password');
define('DBNAME','database-name');
$conn = #mysql_connect (DBHOST, DBUSER, DBPASS);
$conn = #mysql_select_db (DBNAME);
if(!$conn) {
die('Some Error Message');
}
define('included', 1);
?>
And Instead of connecting my website to the database and showing the content It shows the die() error message I have used above, And I tried adding if (! $conn) { mysql_error() or die('some message'); } I can see my website but can't see the db content and when I submit any form It shows this message Query failed. Access denied for user 'root'#'localhost' (using password: NO)
Thank you for your time.
Look into using PDO. mysql_connect is deprecated and generally not considered secure. However, it should still work.
Here's a simple PDO connection:
TRY {
$handler = new PDO('mysql:host=localhost;dbname=NameofDB','username','password');
$handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
CATCH(PDOEXCEPTION $e) {
echo $e->getMessage() . "<br>";
die ('sorry for your luck!');
}
There are lots of tutorials around the web for PDO implementation, and it's generally considered he way to go.
Related
This question already has an answer here:
Why cannot the PHP hide the error message thrown by the MySQL connection?
(1 answer)
Closed last month.
I'm trying to perform a connection between PHP and a MySQL database via Mysqli. I have the following code:
<?php
$database_host = "localhost";
$database_username = "root";
$database_password = "";
$database_name = "test3";
$connection = new mysqli ($database_host, $database_username, $database_password, $database_name);
if ($connection->connect_errno) {
echo ("Can't connect to database because $connection->connect_error"); }
else {
echo ("Connection was successful"); }
?>
I'm using this code for a setup wizard for the users to install a software in their webservers. The point is that if the credentials are correct it prints "Connection was successful", but if wrong its output is:
**Fatal error**: Uncaught mysqli_sql_exception: Access denied for user 'root'#'localhost' (using password: YES) in /opt/lampp/htdocs/uuid/index.php:55
Stack trace:
#0 /opt/lampp/htdocs/uuid/index.php(55): mysqli->__construct('localhost', 'root', '', 'test3')
#1 {main}
thrown in **/opt/lampp/htdocs/uuid/index.php** on line **55**
I currently have error reporting for PHP on (without it, I couldn't get this error message), but regular users who upload the software to a web hosting and they input incorrect credentials will receive a blank page instead.
So how can I print "Can't connect to database because [...]", so users know why is this error being thrown?
In PHP 8.1.0, MySQLi sets the report mode to MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT by default. This causes MySQLi errors to throw exceptions.
So catch the exception and report the error how you want:
try {
$connection = new mysqli ($database_host, $database_username, $database_password, $database_name);
} catch (mysqli_sql_exception $e) {
echo "Can't connect to database because $e";
... return appropriate http status code ...
}
echo "Connection was successful";
Note: The above is just a quick example of handling an exception. Error handling and reporting is a more complex topic, and this is not meant to show the best practice for every application case.
I recently installed the latest version of XAMPP and transferred my database over to it. I created my user accounts on phpmyadmin, however when i try to access the database with any user other than root through PHP I get:
Connection failed: SQLSTATE[HY000] [1045] Access denied for user
It does not work for both users with and without a password. I have tried flushing the privileges deleting and creating the users again but nothing seems to work.
Example connection code:
<?php
$dsn = 'mysql:dbname=test_db;host=127.0.0.1';
$user = 'test_user';
$password = 'test';
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
?>
If you are SURE of your valid credential, it's maybe a charset problem ( due to your database transfert)
Try to go in phpmyadmin, and change the collation on operations tab to "t8_general_ci".
But, in most of time, your error is due to bad credential or bad privileges.
Check it too... But first :).
I have a website that has forms, images, text, etc. I want extract data from the forms and keep a record of them in mySQL. In order to do this do I need to change the file extension from '.html' to '.php'? And if so then will this effect any inline css?
Also, when I need to connect to the server via the php, how to I know the database username, database password, and the database host?
I have go daddy as the web host, and use the CPanel they provide to access the phpmyadmin
Thanks - any help is highly appreciated!
This is the PHP code I have so far, and I keep getting error alerts when I run the test through XAMPP:
mysql_connect() is already deprecated please consider using mysqli or PDO.
PDO . database connection example :-
You should create a separate class containing the functions for basic operation in database and keep that file separate from your other code , just inherit the class and use the connection and function .
<?php
try {
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
foreach($dbh->query('SELECT * from FOO') as $row) {
print_r($row);
}
$dbh = null;
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
?>
As the other answer stated, use pdo or mysqli instead of 'mysql`.
A good way to use PDO is to put the connection code within a file, and include this file anywhere you need to make use of the database.
Let's call this file dbconnector.php.
try
{
$conn = new PDO($connectionString, 'root', '');
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
echo $e->getMessage();
}
Now, wherever you want to use database, just use :-
include 'dbconnector.php'
Now, you can access the connection variable via $conn.
Read more on PDO.
http://www.phpro.org/tutorials/Introduction-to-PHP-PDO.html
I'm having an issue yesterday and today with my host (godaddy) who apparently is having trouble with a particular mysql server... in that it's completely out of commission for a few hours at a time.
Right now this causes my site to not load at all - showing this error:
[phpBB Debug] PHP Warning: in file /home/content/index.php on line 53: mysqli::mysqli() [mysqli.mysqli]: (HY000/2003): Can't connect to MySQL server on 'dbserver.com' (110)
Connect failed: Can't connect to MySQL server on 'dbserver.com' (110)
I'm guessing it's showing that because of this code I have:
// PREPARE DB CONNECTION
$mysqli = new mysqli("dbserver.com", "username", "password", "dbname");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
Which tells the php to stop altogether if mysql is down. So I suppose all I need to do is remove the "exit" function and I'm good to go?
I guess theoretically speaking, why is exit there by default in so many tutorials of how to connect to mysql? It kills the web site if the database is unavailable.
Just looking for thoughts from those who know much more than I please, thank you!
Just bin this:
printf("Connect failed: %s\n", mysqli_connect_error());
And replace it with a placeholder page instead to inform people that s**t happened and you'll be back later.
header('Location: error_db.php');
Displaying good and pretty error-messages to the user is often neglected by most programmers. This is because it can be a real pain in the ass to do the right way.
As Jassica commented, if your entire site function without the database-connection, you can just remove the exit() in your function and the site is good to go.
However, I guess the chances for a 100 % functional site without the database-connection are very small. What most sites to is to redirect the user to a 500-errorpage where they inform the user that something went wrong. Often provided contact-information so the right people can be notified too. Twitter does this, so does Facebook, Reddit and many more.
You can just do:
$mysqli = new mysqli("dbserver.com", "username", "password", "table");
if (mysqli_connect_errno()) {
header("Location: error500.html");
exit();
}
Or something like that.
This should do the trick.
function connect($u,$p,$db,$host) {
try {
$mysqli = new mysqli($host,$u,$p,$db);
$connected = true;
} catch (mysqli_sql_exception $e) {
throw $e;
}
}
try {
connect('username','password','database','host');
} catch (Exception $e) {
echo $e->errorMessage();
}
I am facing database connection problem while trying to connect a .php file through wamp server
the error message is something like- "Access denied for the user " # ' localhost' " for database 'aschool'. 'aschool' is my database name.
Mentioning that I've changed my port number of wamp server, I am worried that is it really
for changing port number or anything else.Here is my code.
$con = mysql_connect();
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("aschool", $con);
After this line the error message comes. I've tried parameters "localhost" inside the mysql_connect()
function or more parameters but the result is same.
Thanks in advance anyone gives me any solution
That's because you are using mysql_connectwrong for your use case.
If you check the documentation page it says that you can also a server-path,
something like mysql_connect('localhost:1234', 'username', 'password').
But you shouldn't use mysql_connect.
Use PDO so that you can use parameterized queries.
In code it would go like this:
try
{
$pdo = new \PDO('mysql:dbname=aschool;host=127.0.0.1', 'myUser', 'myPassword');
} catch (PDOException $exception)
{
// Do something with your exception.
// Echo it, dump it, log it, die it.
// Just don't ignore the exceptions!
}