PHP Exits When I Try to Connect to a Database Using MySQLi

Fishy

I have been developing a website recently, and I came across an error. When I tried to do print_r(error_get_last()); It output some text about mysql not being supported anymore, and to use MySQLi. I was all for it. When I edited my database class to support MySQLi, it exits every time it tries to connect. I have put echo 'hi<br />'; before and after the mysqli function, and a or trigger_error("Error: " . print_r(error_get_last())); at the end of it, but all it outputs is hi, and nothing else. My database is created in phpMyAdmin, and has all the correct permissions, but it just cannot connect. This is my connection code:

echo 'hi<br />';
$this->db = mysqli($host, $dbuser, $dbpass, $dbname) or trigger_error("Error: " . print_r(error_get_last()));
echo 'hi<br />';
$this->connect_start = microtime(true);
if ($this->db->connect_errno > 0) die ($this->debug(true));

It is in the constructor of a class that looks like this:

function db($host, $dbname, $dbpass, $dbuser)

and it is called like this:

$db = new db($host, $dbname, $dbpass, $dbuser);
Gerald Schneider

Change this:

$this->db = mysqli() 

to

$this->db = new mysqli()

mysqli is not a function, it's a class. If you had error reporting on you'd get a message like this:

PHP Fatal error: Call to undefined function mysqli()

That's the reason why the rest of your code isn't executed, the execution stops right there.

Also note that new mysqli() will never return false, so your or ... part becomes useless. If you want to check for connection errors you have to check $this->db->connect_error after the connection attempt.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Error 500 when I try to connect MySql with php's mysqli

PHP error finding database when trying to connect to it with mysqli_connect()

is my database password exposed when I connect using php?

How to solve error on SMTP connect when I try send email from php using phpMailer? POP/IMAP

Failing to connect to database (HY000/1045), trying to connect to a database using php mysqli_connection

When I try to echo data from database using php my html page breaked

Can't connect to my MySQL database using PHP mysqli_connect()

Problems in connection when I try to connect GKE cluster using kubectl

Buttons not rendering when I try to connect to Dropbox using the Javascript API

php connect mysql database via mysqli

Can't connect to phpMyAdmin database using mysqli

PHP undefined variable when I try to extract data from database

PHP error message when I try to check a value from database

PHP OPP - My first try to connect on database

Connect to Cloud SQL Database Using mysqli_connect()

Why I can't connect to a MySQL 5.3.36 shared server using mysqli_connect and PHP 7.4, but it will work using php 7.1?

Getting error mysqli::real_connect(): (HY000/2002): No such file or directory when i try to access my project on live server

Php error when using mysqli

I cant connect to my MYSQL Database using Php

When does PHP connect to the database?

I can't UPDATE database mysqli PHP

Search database for matching results, using PHP and MYSQLI

php mysqli "No database selected" using class

Unable to connect to database using PHP

xxamp connect to database using php

My XAMPP server returns error when I try to connect mysql with php

Load more when scroll to bottom from database using GET variables- PHP/ Mysqli

I get an error whenever I try to connect database

How can i insert real time of an event into database using php mysqli?