Click here to Skip to main content
15,042,814 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Warning : mysqli_prepare() expects parameter 1 to be mysqli, null

What I have tried:

login.php

if(empty($username_err) && empty($password_err)){

global $link;
$sql = "SELECT id, username, password FROM users WHERE username = ?";

error line
if($stmt = mysqli_prepare($link, $sql))


And here is config.php

class dbConfig {
private $serverName = 'localhost';
private $userName = 'root';
private $password = '';
private $dbName = 'dg_db';
protected $conn;



public function __construct()
{

$link = mysqli_connect('localhost','root','','dg_db');

{
try{

$dsn ="mysql:host={$this->serverName}; userName={$this->userName}; charset=utf8";
$options=array(PDO::ATTR_PERSISTENT);
$this->conn = new PDO($dsn, $this->userName, $this->password, $options);

}

catch(PDOException $e)
{
echo "Connection Error: ".$e->getMessage();
}


}
}

Warning : mysqli_prepare() expects parameter 1 to be mysqli, null

I am stuck of this
Posted
Updated 17-May-21 21:16pm
Comments
Mohibur Rashid 18-May-21 3:19am
   
yeah, your $link is null. You need to initiate $link. https://www.php.net/manual/en/mysqli.prepare.php . Follow the example #1. Procedure style.

Never store passwords in clear text - it is a major security risk. There is some information on how to do it here: Password Storage: How to do it.[^] - the code is in C#, but the text explains why you do this. For PHP, there are built in methods for hashing passwords: https://www.sitepoint.com/hashing-passwords-php-5-5-password-hashing-api/[^]

And remember: if this is web based and you have any European Union users then GDPR applies and that means you need to handle passwords as sensitive data and store them in a safe and secure manner. Text is neither of those and the fines can be .... um ... outstanding. In December 2018 a German company received a relatively low fine of €20,000 for just that.
   
Either you forgot to call __construct or the call to mysqli_connect failed for some reason. But since you do not check to see whether it succeeded any failure there will go unrealised.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900