PHP MySQL connecting error - php

I have a question that is really bothering my understanding of working with php and MySQL. The problem is when i try to connect to my online DB.
If I Write my code like this:
define('DB_NAME', 'name');
define('DB_USER', 'user');
define('DB_PASS', 'pass!');
define('DB_HOST', 'localhost');
I connect with no problems. Great, that works!
But, when i want to connect like this:
$servername = "localhost";
$username = "user";
$password = "pass";
$dbname = "name";
$conn = new mysqli($servername, $username, $password, $dbname);
I get this error: "Access denied for user..."
Can somebody please help me with this silly problem?

This code works for both options
<?php
//Creates static credentials
define('DB_NAME', 'data');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_HOST', 'localhost');
//Creates connection to the database
$con = new mysqli(DB_HOST, DB_USER, DB_PASSWORD);
//Checks for connection
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
//If there are no connection, error
if (!$con) {
die('Could not connect' . mysqli_error());
}
//Select the 'data' database
$con->select_db(DB_NAME);
//Checks if database 'data' has been selected
if (mysqli_select_db($con, DB_NAME)) {
echo "Database exists <br>";
} else {
echo "Database does not exist";
}
if($con){
echo "connection succss";
}
?>
option 2
<?php
$servername="localhost";
$username ="root";
$password="";
$dbname="test";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}else{
echo "connected";
}
?>
Those are my codes, and also yours is working fine, might be a problem, with your username/password

Try this:
$mysql_host = 'localhost';
$mysql_user = 'user';
$mysql_password = 'pass';
$mysql_database = 'localhost';
$conn = mysql_connect($mysql_host, $mysql_user, $mysql_password);
#mysql_select_db($mysql_database);
Also check the privelges for that user.

This happens some time because you have changed user name and password of data base or you can try make your user as root and password empty.

Related

Accessing a MySQL database with php file

I have a MySQL database and I am trying to figure out how to access it with a php file. I keep getting an error, so I was wondering if that was because my hostname is incorrect. If so, could anyone help me identify exactly what it is? The database is hosted on this website: https://christopherliao2002.000webhostapp.com/.
Below is my HTML code.
<?php
//Step1
$db = mysqli_connect('localhost','***********','password','******')
or die('Error connecting to MySQL server.');
?>
<html>
<head>
</head>
<body>
<h1>PHP connect to MySQL</h1>
<?php
//Variables
$servername = "localhost";
$username = "*****";
$password = "****";
$dbname = "******";
//Connection to database
$conn = new mysqli($servername, $username, $password, $dbname);
//Test connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
</body>
</html>
Try this format
$servername = "localhost"; // This is fixed for 000webhost.
$username = "id***_name";
$password = "****";
$dbname = "id***_name";
Try this without the try / catch:
//Variables
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";
//Connection to database
$conn = new mysqli($servername, $username, $password, $dbname);
//Test connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());}
Reference: https://www.w3schools.com/php/php_mysql_connect.asp
Here the problem is with your hostname. Normally the hostname will be 'localhost' and in 000webhost the hostname is something like mysql#.000webhost.com and '#' needs to be replaced with the number for that you need to check your hosting providers members area.
And please don't post your username and password publicly
$db = mysqli_connect('mysql#.000webhost.com','xxxxx','password','xxxxxx')
or die('Error connecting to MySQL server.');
?>

convert master connecting error

I got following error when i run code:
Notice: Use of undefined constant newdb - assumed 'newdb' in C:\xampp\htdocs\cj\include\conn.php on line 9 No database selected
The following code for connecting i am using in conn.php file
,
<?php
$lusername = "root";
$lpassword = "";
$lhostname = "localhost";
// connection to the database
$dbhandle = ($GLOBALS["___mysqli_ston"] = mysqli_connect($lhostname, $lusername, $lpassword)) or die("Unable to connect to mysql");
mysqli_select_db($GLOBALS["___mysqli_ston"], newdb) or die("Unable to connect to mysql");
// echo "Connected to mysql<br>";
?>
how to fix it?
This is just something to consider: try using it without ["___mysqli_ston"]
Example
<?php
$servername = "localhost";
$username = "";
$password = "yourpass";
$dbname = "yourDBname";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Read special character sets
$conn->set_charset("utf8");
// Check connection
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
?>
Just an example of different easier approach all up to you.
For the converter
Example
<?php
$servername = "localhost" ;
$username = "root";
$pass = "";
$con = ($GLOBALS["___mysqli_ston"] = mysqli_connect($servername, $username, $pass)) or die("Problem occur in connection");
$db = ((bool)mysqli_query($con, "USE " . info));
?>

Getting connection timed out with DB connection

I am trying to connect to a remote rackspace DB
PHP:
<?php
$servername = "XX.XX.XX.XX";
$username = "username";
$password = "password";
$dbname = "table";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
And I get
Warning: mysqli::mysqli(): (HY000/2002): Connection timed out
Try this
<?php
$servername = "XX.XX.XX.XX";
$username = "username";
$password = "password";
$dbname = "table";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();die();
}
?>

MYSQL Database connection for php

I am using my website database with php mysql_connect but my hosting provider is saying I need to use mysqli_connect.
When I edit connection file and update change method its not working with mysqli.
Currently using this code:
MSQL Method:
$con=mysql_connect("localhost","username","password");
mysql_select_db("databasename");
and for mysqli I am using this code but fail
MYSQLI Method:
$con = mysqli_connect("localhost","my_user","my_password","my_db");
Please help. Why is mysqli not working? Is my method wrong?
Try this:
Example (MySQLi Object-Oriented)
<?php
$servername = "localhost";
$username = "username";
$password = "password";
` $dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
Example (MySQLi Procedural)
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>

PHP script cannot access database in sql

I am trying to connect to a MySQL database through a php script. It gives me this error from mysql_error(): Access denied for user '#localhost' to database 'userinfo'
userinfo is the database.
my script is this
<?php
$servername = "localhost";
$username = "root";
$password = "'mm'";
$database = "userinfo";
$conn = mysqli_connect($servername, $username, $password);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully<br>";
mysql_select_db($database) or die(mysql_error());
echo "connected successfully to db:" . $database . "<br>";
?>
you are connecting using
mysqli_
function
and selecting data with
mysql_
avoid using both at the same time. since they're incompatible.
use the mysqli_ alternative instead
mysqli_select_db($conn, $database);
and
mysqli_error($conn)
Please keep in mind this isn't the safest way. But since you have said your learning this it is a start.
<?php
$servername = "localhost";
$username = "root";
$password = "mm";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
http://www.w3schools.com/php/php_mysql_connect.asp
To select data from the database
http://www.w3schools.com/php/php_mysql_select.asp
It appeared you where combining the old mysql in php with the new mysqli

Categories