Unable to set database connection encoding

Good day people,

I’m a noobie with regard to PHP, which I pick up recently. On page 120-121 of the book, I encountered an error although I followed the codes provided there.

<?php

$link = mysql_connect(‘localhost’, ‘root’);
if (!$link)
{
$output = ‘Unable to connect to the database server.’;
include ‘output.html.php’;
exit();
}
if(!mysqli_set_charset($link, ‘utf8’))
{
$output = ‘Unable to set database connection encoding.’;
include ‘output.html.php’;
exit();
}

if(!mysqli_select_db($link, ‘ijdb’))
{
$ouput = ‘unable to locate the joke database.’;
include ‘output.html.php’;
exit();
}

$output = ‘Database connection established.’;
include ‘output.html.php’;

?>

I did not set any password on MySQL, hence removing the password allowed me to enable the DB connection. However, coming to the second part, I was not able to set the database connection encoding.
This was the error message I saw:

Warning: mysqli_set_charset() expects parameter 1 to be mysqli, resource given in C:\xampp\htdocs\Eugene\MySQL1\index.php on line 10

Unable to set database connection encoding.

Any reason why?

Thanks!

Are you trying to use mysql_ calls or mysqli_ calls - you can’t mix them together, you have to use one or the other.

ahh…it works when I only use mysqli_calls. I have another question…everything works fine with mysqli, however, when I tried using purely on mysql_calls, it came out with this error…

Warning: mysql_set_charset() expects parameter 1 to be string, resource given in C:\xampp\htdocs\Eugene\MySQL1\index.php on line 10

Unable to set database connection encoding.

Thanks!

You gave it a resource instead of a string…

mysql_set_charset( 'charset name', $mysql_connect );