Unable to get mysqli to work

I am playing with PHP on my Synology NAS. I have PHP 7.2 and MariaDB 10 running. I cannot figure out why I am having issues with connecting to my database.

If I use the object oriented version of mysqli I receive " 500 There is an error while processing this request"

<?php 

/* DB CONN INFO */
	$db_server = 'localhost';
	$db_user = 'g_web';
	$db_pass = '[email protected]*I.';
	$db_name = 'barpsc';
	//$db_conn = new mysqli($db_server,$db_user,$db_pass,$db_name);
	$db_conn1 = new mysqli($db_server,$db_user,$db_pass,$db_name);
	$result = mysqli->query("Select 'A world full of php' AS _msg from class_tbl");
	$row = $result->fetch_assoc();
	echo $row['_msg'];
?>

however if I use the older procedural code I get a result. "A world full of "

[b]Procedural[/b]
<?php 

/* DB CONN INFO */
	$db_server = 'localhost';
	$db_user = 'g_web';
	$db_pass = 'myPasswordHere';
	$db_name = 'barpsc';
	//$db_conn = new mysqli($db_server,$db_user,$db_pass,$db_name);
	$db_conn1 = mysqli_connect($db_server,$db_user,$db_pass,$db_name);
	$result = mysqli_query($db_conn1, "Select 'A world full of ' AS _msg from class_tbl");
	$row = mysqli_fetch_assoc($result);
	echo $row['_msg'];
?>

You need to find the php.ini that php is using and set error_reporting to E_ALL and set display_errors to ON, so that php will help you by reporting and displaying all the errors it detects.

You would be getting a fatal error about an undefined constant “mysqli”. OOP usage is $object->method(), so the query() method call would be $db_conn1->query().

I recommend that you forget about the mysqli extension. It is overly complicated and inconsistent, requiring you to learn a completely different set of statements for a non-prepared and a prepared query. Instead, learn and use the much simpler and more modern PDO extension. It treats the result of a non-prepared and a prepared query in exactly the same way, so you are only dealing with one set of php statements to learn. It also works with 12 different database types, so the learning you are doing can be used if you ever need to work with a different database type (the actual sql query syntax may be different.)

1 Like

thanks! This was a great help/

Sponsor our Newsletter | Privacy Policy | Terms of Service