I am trying to connect to MySQL database with pdo but keep getting this error:
Warning: Use of undefined constant username - assumed 'username' (this will throw an Error in a future version of PHP) in C:\xampp\htdocs\first\index.php on line 2
Warning: Use of undefined constant password - assumed 'password' (this will throw an Error in a future version of PHP) in C:\xampp\htdocs\first\index.php on line 2
Fatal error: Uncaught PDOException: SQLSTATE[HY000] [1045] Access denied for user 'username'#'localhost' (using password: YES) in C:\xampp\htdocs\first\index.php:2 Stack trace: #0 C:\xampp\htdocs\first\index.php(2): PDO->__construct('mysql:host=loca...', 'username', 'password') #1 {main} thrown in C:\xampp\htdocs\first\index.php on line 2
here's my code:
<?php
$pdo = new PDO('mysql:host=localhost;port=3306;dbname=dbname', username, password);
?>
It's because the username and password are not variables. They are missing the $
<?php
$username = 'myUsername';
$password = 'myPassword';
$pdo = new PDO('mysql:host=localhost;port=3306;dbname=dbname', $username, $password);
?>
Related
Warning: mysqli_connect(): (HY000/1045): Access denied for user
'usr'#'localhost' (using password: YES) in
/storage/emulated/0/htdocs/includes/condb.php on line 9 Failed to
connect to MySql: Access denied for user 'usr'#'localhost' (using
password: YES) Warning: mysqli_query() expects parameter 1 to be
mysqli, boolean given in /storage/emulated/0/htdocs/includes/condb.php
on line 19
Warning: mysqli_error() expects parameter 1 to be mysqli, boolean
given in /storage/emulated/0/htdocs/includes/condb.php on line 20
Unable to connect
Im trying to connect to the database using this code but im an unable to. I dont know what i actually did wrong. Ive tried googling and reading some articles but none of those work.
UPDATED CODE still getting the error message above.
$server = "127.0.0.1"; $username = "usr"; $password = "123"; $database
= "contents";
$conn = mysqli_connect($server,$username,$password,$database);
if(mysqli_connect_errno($conn)) {
echo "Failed to connect to MySql: ".mysqli_connect_error(); }
$sql = "INSERT INTO contents('subj','article','day') VALUES
('".$_POST['This is the subj']."', '".$_POST['This is the
content']."', '".$_POST['2019-02-01 10:15:59']."')";
if(!mysqli_query($conn,$sql)) {
die("Unable to connect".mysqli_error($conn)); }else {
echo "Connected"; }
?>
Still getting the same error above unfortunately it was not a typo error. Still couldn't get the solution to this problem it has been days. Can someone with problem solving skills help me.
You don't have to pass $conn inside mysqli_connect_errno()
remove $conn
$conn = mysqli_connect($server,$username,$password,$database);
if(mysqli_connect_errno()) {
echo "Failed to connect to MySql: ".mysqli_connect_error();
}
You can fix this error by changing your mysql username and password. There are many tutorials for changing mysql user and password with a little search
would like to ask about configuration of XAMPP mySQL database.
I have set my xampp document root to drive D, and now i unable to connect to SQL database and always get error.
Fatal error: Uncaught Error: Call to undefined function mysql_connect() in D:\localserver\connection.php:3 Stack trace: #0 D:\localserver\login.php(4): include() #1 {main} thrown in D:\localserver\connection.php on line 3
the file that handle the connection look like this
<?php
$connect = mysql_connect("localhost","root","");
if(!$connect) {
die ('connection fail!!!');
} else {
print ('connection okay!!!');
}
$connectdb = mysql_select_db('admin_login');
if(!$connectdb) {
die ('connection fail!!!');
} else {
print ('connection okay!!!');
}
?>
The mysql_connect() function is from a library that is deprecated since a couple of years and has been removed in PHP 7.
Use mysqli_connect() or PDO.
UPDATE
You can pass the name of the database into mysqli_connect() and get rid of the extra mysqli_select_db(). If you want to user mysqli_select_db() in procedural style instead of object oriented it expects the link that is returned by mysqli_connect() as the first parameter and the database name as the second one like this:
$link = mysqli_connect("localhost", $user, $password);
$db = mysqli_select_db($link, $dbname);
I have an error like this in my code
Fatal error: Uncaught Error: Call to undefined function
mysql_real_escape_string() in C:\xampp\htdocs\wp\server.php:12 Stack
trace: #0 C:\xampp\htdocs\wp\register.php(1): include() #1 {main}
thrown in C:\xampp\htdocs\wp\server.php on line 12
My code is
$username="";
$email="";
$errors=array();
//conct to sever
$db = mysqli_connect('localhost','registration');
//register button click
if (isset($_POST['register'])){
$username=mysql_real_escape_string($conn,$_POST['username']);
$email=mysql_real_escape_string($conn,$_POST['email']);
$password_1=mysql_real_escape_string($_POST['password_1']);
$password_2=mysql_real_escape_string($_POST['password_2']);
Can someone explain What is the function of mysql_real_escape_string() in php?
Since you're using msqli, you should use the mysqli function:
$username = mysqli_real_escape_string($conn,$_POST['username']);
I have received:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '5538ac14bb2ca7514f9f4d8826f3c45e'')' at line 1' in C:\xampp\htdocs\register.php:19 Stack trace: #0 C:\xampp\htdocs\register.php(19): PDO->exec('insert into use...') #1 {main} thrown in C:\xampp\htdocs\register.php on line 19.
How can this be solved, like what should be done?
<?php
session_start();
// If the form has been submitted
if (isset($_POST['submitted'])){
// Create a database connection
$db = new PDO("mysql:dbname=johnsoa7_db;host=localhost", "root", "");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Get and sanitise the inputs, we don't need to do
// this with the password as we hash it anyway
$safe_forename = $db->quote($_POST['forename']);
$safe_lastname = $db->quote($_POST['lastname']);
$safe_email = $db->quote($_POST['email']);
$hashed_password = $db->quote(md5($_POST['password']));
// Insert the entry into the database
$query = "insert into users values (default, $safe_forename, $safe_lastname, $safe_email, '$hashed_password')";
$db->exec($query);
// Get the ID
$id = $db->lastInsertId();
// Output success or the errors
echo "Congratulations! You are now registered. Your ID is: $id";
}
?>
You have an error in this line:
$query = "insert into users values (default, $safe_forename, $safe_lastname, $safe_email,'$hashed_password')";
default should be quoted if it is string.
If it is a variable, you missed $.
Please see the comment by #ceejayoz:
As he said you don't need quoted around $hashed_password...
Config file:
$db=array(
'host' => 'localhost',
'user' => 'root',
'pass' => 'secret',
'name' => 'jack',
);
Index file
$mysql = new mysqli($db['host'],$db['user'],$db['pass'],$db['name']);
$mysql->set_charset('utf8mb4');
if($mysql->connect_errno)
{
die('Database connection could not be established. Error number: '.$mysql->connect_errno);
}
I get:
Database connection could not be established. Error number: 1045
Please guys, tell me what to do and I will respond you with the answer in 30 seconds!
UPDATE:
Okay so if i make change line to
$mysql = new mysqli(localhost,root,secret,jack);
it works, but how do I get it work with the tags?
Error No: 1045 refer to as Access denied for user 'root'#'localhost' (Using password: NO). so check you password and username for the database. Try passing the password as empty.