Call to undefined function mysqli_connect() issue [duplicate] - php

This question already has answers here:
Fatal error: Call to undefined function mysqli_connect() in... while connecting PHP 5.4.22 and MySQL 5.5 with Apache 2.4.7
(5 answers)
Closed 3 years ago.
I am writing a simple registration code. I want to save username and password in db.
While running the page of registration.php on localhost I got this error.
Fatal error: Uncaught Error: Call to undefined function mysqli_connect() in C:\Users\Ammad Hassan\www\db.php:4 Stack trace: #0 C:\Users\Ammad Hassan\www\registration.php(10): require() #1 {main} thrown in C:\Users\Ammad Hassan\www\db.php on line 4
db.php is shown below
<?php
// Enter your Host, username, password, database below.
$con = mysqli_connect("localhost","root","","register");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
Registration.php
<?php
require('db.php');
// If form submitted, insert values into the database.
if (isset($_REQUEST['username'])){
// removes backslashes
$username = stripslashes($_REQUEST['username']);
//escapes special characters in a string
$username = mysqli_real_escape_string($con,$username);
$password = stripslashes($_REQUEST['password']);
$password = mysqli_real_escape_string($con,$password);
$query = "INSERT into `users` (username, password)
VALUES ('$username', '".md5($password)."')";
$result = mysqli_query($con,$query);
if($result){
echo "<div class='form'>
<h3>You are registered successfully.</h3>
<br/>Click here to <a href='login.php'>Login</a></div>";
}
}else{
?>
I want to run this page so that I can enter in the form.

Check if mysqli library is enabled in your php.ini.
Also consider using PDO instead, it is better and not tied to MySQL.

Related

mysqli_query() expects parameter 1 to be mysqli, boolean given [duplicate]

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
How do I display a MySQL error in PHP for a long query that depends on the user input? [duplicate]
(6 answers)
Reference - What does this error mean in PHP?
(38 answers)
Closed 4 years ago.
Hey can you guys help me with this because I can't figure it out here are the errors.
[04-Nov-2018 15:21:52 UTC] PHP Warning: mysqli_connect(): (HY000/1045):
Access denied for user 'freeload_retain'#'vps28004.inmotionhosting.com'
(using password: NO) in /home/freeloadboard/public_html/insert.php on
line 7
[04-Nov-2018 15:21:52 UTC] PHP Warning: mysqli_select_db() expects
parameter 1
to be mysqli, boolean given in /home/freeloadboard/public_html/insert.php
on line 21
[04-Nov-2018 15:21:52 UTC] PHP Warning: mysqli_query() expects parameter
1 to be mysqli, boolean given in
/home/freeloadboard/public_html/insert.php on line 45
Program:
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$con = mysqli_connect('##.##.###.##','freeload_retain','');
if(!$con)
{
echo "Not Connected to Server. ";
}
if(!mysqli_select_db($con, 'freeload_retain'))
{
echo "Database Not Selected. ";
}
$Companyname = $_POST['companyname'];
$Username = $_POST['username'];
$Password = $_POST['password'];
$Email = $_POST['email'];
$sql = "INSERT INTO clients (companyname, username, password, email)
VALUES ('$Companyname', '$Username', '$Password', '$Email')";
if(!mysqli_query($con, $sql))
{
echo "Not Inserted. ";
}
else
{
echo "Inserted. ";
}
?>
Hope you guys find out the answer soon!
Also I'm reusing this question because I can't wait another day to make another question but thanks for helping me out!
To answer your question: It's not working because you're wrapping the column names in brackets, remove these and it should work. You also have a typo. ($comapnyname = $_POST['companyname'];), should be $companyname.
However, there's a few other, bigger issues with your code. You're using the mysql functions, which are deprecated and completely removed from PHP7.
Next to that, you should use prepared statements and bind_param to prevent SQL injections, escaping strings will not accomplish this.
This what it would look like using prepared statements.
// ... Set your database connection variables
/*
* Create the database connection, notice the
* usage of mysqli instead of mysql
*/
$connect = new mysqli($host, $user, $password, $database);
/*
* The query, notice the usage of a question mark
* on the place of the variables to be inserted
*/
$sql = "INSERT INTO client (cname, tname, pname, ename) VALUES (?, ?, ?, ?)";
// Prepare the query using $connect->prepare()
$stmt = $connect->prepare($sql);
// Check if the query was prepared
if(!$stmt) {
// ... Handle your error
}
// Get $_POST variables
$companyname = $_POST['companyname'];
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
if(!$stmt->bind_param('ssss', $companyname, $username, $password, $email)) {
// ... Handle your error
}
if(!$stmt->execute()) {
// ... Handle your error
} else {
echo 'Record inserted.';
}
It also seems that you're inserting the passwords into your database as clear-text, this is a big issue. You should hash them. Write two functions, one to hash the password and one to verify it when users log in.
The first function will return the password hash and the second one will return TRUE or FALSE if the password is correct or incorrect.
function hashPassword($password) {
return password_hash($password, PASSWORD_DEFAULT);
}
function verifyPassword($password, $hash) {
return password_verify($password, $hash);
}

authenticate login with mysql [duplicate]

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Reference - What does this error mean in PHP?
(38 answers)
Closed 4 years ago.
as stated in my username, desperately need help so sorry if its a duplicate post!
I'm trying to do up a login page that redirects me to my home page if authentication fails so my input will cross check with the database(mysql) then output either successful or error. but the result always show error. im pretty sure that it didnt went into my 1st if checking statement.
As shown below is my code:
<?php
$servername = "localhost";
$username = "read";
$password = "projecttest";
$dbname = "test-member";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$password1 = $_POST["password"];
$username1 = $_POST["username"];
$chkpassword = " SELECT password FROM member WHERE password = $password1 ";
$chkusername = " SELECT username FROM member WHERE username = $username1";
if ($conn->query($chkpassword) == TRUE ) {
echo "successful log in"
?>
<INPUT TYPE="hidden" NAME="redirect"
VALUE="http://localhost/IPproject_test1/home.php">
<?php
}
else if ($conn->query($chkpassword) == FALSE ) {
echo "error";
}
$conn->close();
?>
The issue is your query.
Let's assume the user enters as password asdf1234. Your query would look like this:
SELECT password FROM member WHERE password = asdf1234
That will fail because MySQL thinks asdf1234 is a column. If you escape the string, it should work.
$chkpassword = "SELECT password FROM member WHERE password = '{$password}'"
So the query looks like this:
SELECT password FROM member WHERE password = 'asdf1234'
I still wouldn't check on == TRUE tho, but on !== null

My task is about PHP connectivity with xampp server which is not connecting and giving me erroe [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Kindly show me the error in my code i am trying to connect the sign in form with database using php but i am getting these errors:
Warning: mysqli_connect(): (HY000/1045): Access denied for user
'root'#'localhost' (using password: YES) in
C:\xampp\htdocs\MyProject\php\signin.php on line 9
Notice: Trying to get property of non-object in
C:\xampp\htdocs\MyProject\php\signin.php on line 11
Fatal error: Uncaught Error: Call to a member function query() on
boolean in C:\xampp\htdocs\MyProject\php\signin.php:17 Stack trace: #0
{main} thrown in C:\xampp\htdocs\MyProject\php\signin.php on line 17
<?php
$servername = "localhost";
$username = "root";
$password="";
$dbname = "myproject";
// Create connection
$conn = mysqli_connect($servername,$username,$password,$dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO sign_in (username,password)";
if ($conn->query($sql) === TRUE) {
echo "New record inserted successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
You are getting this ERROR because of your is mixed up with mysqli_* functions and PDO code.
Methods of both PDO connection and mysqli_* are different.
Here is your error.
******Here is your error*****
$sql = "INSERT INTO sign_in (username,password)";
if ($conn->query($sql) === TRUE) { //ERROR line
echo "New record inserted successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
You have use function mysqli_query() instead of accessing member function of $conn, just take below correct example.
// Perform queries
mysqli_query($conn,"SELECT * FROM Persons");
mysqli_query($conn,"INSERT INTO Persons (FirstName,LastName,Age) VALUES ('Glenn','Quagmire',33)");
mysqli_close($conn);
Hope you get the point and will help you..
There must be a password on root mysql account that you're not providing here:
$password="";
Other parts of the error is a direct consequence of this, $conn became a boolean and not an object as the mysqli_connect() is unsuccessful (it gives back false on failure).
Your query is not correct, since no values where given to insert. It should look like this:
$sql = "INSERT INTO sign_in (username,password) VALUES ($username,$password)";

mysqli_query not accepting my mysqli_connect and proper use of parameters of mysqli_query

i have set my configdb.php on a different page and include it on my other php pages..
here is my configdb.php
<?php
$hostname ="localhost";
$username ="root";
$password ="";
$db ="practicedb";
$connect = mysqli_connect($hostname,$username,$password) or die("cannot connect to server");
mysqli_select_db($connect,$db) or die("database not found!");
?>
these are the errors that i get:
Notice: Undefined variable: configdb in /Applications/XAMPP/xamppfiles/htdocs/practicesystem/add.php on line 14
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /Applications/XAMPP/xamppfiles/htdocs/practicesystem/add.php on line 14
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in /Applications/XAMPP/xamppfiles/htdocs/practicesystem/add.php on line 15
Notice: Undefined variable: configdb in /Applications/XAMPP/xamppfiles/htdocs/practicesystem/add.php on line 28
this is my add.php where i INSERT items into database from the $_POST method from a previous php page..
<?php
include "configdb.php";
$studid=$_POST['studid'];
$lastname=mysql_real_escape_string($_POST['lastname']);
$firstname= mysql_real_escape_string($_POST['firstname']);
$middlename= mysql_real_escape_string($_POST['middlename']);
$email=$_POST['email'];
$check = "SELECT * from studinfo where stud_id = '".$studid."'";
$qry = mysqli_query($configdb,$check);
$num_rows = mysqli_num_rows($qry);
if($num_rows > 0){
// Here we are checking if username is already exist or not.
echo "The person you have entered is already existing. Please try again.";
echo 'Try Again';
exit;
}
$query = "INSERT INTO studinfo (stud_id,lastname,firstname,middlename,email) VALUES ('".$studid."','".$lastname."','".$firstname."','".$middlename."','".$email."');";
//echo $query;
mysqli_query($configdb, $query);
echo "Thank You for Registration.";
echo 'Click Here to login you account.';
exit;
?>
i don't know and i am not sure what to put on the first parameter of mysqli_query..
i tried putting this code $con=mysqli_connect("localhost","root","","practicedb"); it worked but its not practical putting that on every php page where i should connect to the database...
Yet another question on a silly typo...
$connect = mysqli_connect( ...
vs.
$qry = mysqli_query($configdb,$check);
so the error message clearly says: Undefined variable: configdb

Warning: mysqli_query() expects parameter 1 to be mysqli boolean given [duplicate]

This question already has answers here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(2 answers)
Closed 3 years ago.
After submitting something through my form I get the welcome part and then the mysql connection error because mysql is off, it goes away when I turn it on and then the boolean error. "Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given in C:\xampp\htdocs\welcome.php on line 25"
<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?><br>
Your password is <?php echo $_POST["password"]; ?><br>
You have purchased the <?php echo $_POST["sub_type"]; ?>
<?php
$mysqli_host = "localhost";
$mysql_username = "root";
$mysql_password = "123";
$site_db = "test";
$info_name = $_POST["name"];
$info_pass = $_POST["password"];
$info_email = $_POST["password"];
$sub_type = $_POST["sub_type"];
$con=mysqli_connect($mysqli_host,$mysql_username,$mysql_password,$site_db);
// Checks connection to twitch webpanel database and inserts registreation info
if (mysqli_connect_errno());
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_query($con,"INSERT INTO Users (Username, Password, Email, Subcription)
VALUES ('$info_name', '$info_pass', '$info_email', '$sub_type')");
?>
</body>
</html>
Most probably, you have a connection error in mysqli_connect. Wrong credentials or MySQL is down.
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
//you need to exit the script, if there is an error
exit();
}
if (mysqli_connect_errno());
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit(); // **this is missing**
}
mysqli_query($con,"INSERT INTO Users (Username, Password, Email, Subcription)
VALUES ('$info_name', '$info_pass', '$info_email', '$sub_type')");
Here is your bug:
if (mysqli_connect_errno());
There should be no semicolon on the end.
Also according to the documentation you should be using this to check for connection errors:
if (mysqli_connect_error())
But as I said in a comment, I recommend using PDO instead of mysqli. And make sure you properly escape values inserted into the database and encrypt the password with pbkdf2 or scrypt.

Categories