Jump to content
Larry Ullman's Book Forums

Mysqli_Connect.Php, Mysqli_Set_Charset() Error


Recommended Posts

Hi Larry,

I am running Xampp v3.1.0.3.1.0 in Windows 7
Mozilla/5.0(compatable; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0.
Apache/2.4.3 (Win32) OpenSSL/1.0.1cPHP/5.4.7.

My username for MySQL is 'root' and my password  is ''.
That is how my instructor helped us configure our MySQL (I assume that prevented us from creating a password we would forget)last spring and I haven't changed it.
We had a terrible textbook(not yours), and our PHP class was not optimal.
I have since graduated, but want to become proficient enough in PHP and other languages to eventually make a living using them.
I have been frustrated by the quirks of xampp and related issues, but need to master this before I can justify purchasing a server, etc. and doing it right.

When I run this script on page 269, "PHP and MySQL for Dynamic Web Sites, 4th edition", I get this error message.

http://localhost/mysqli_connect.php

<?php # Script 9.2 - mysqli_connect.php

 // This file contains the database access information.
 // this file also establishes a connection to MySQL,
 // selects the database, and sets the encoding.

// Set the database access information as constants:
DEFINE ('DB_USER', 'root');
DEFINE ('DB_PASSWORD', '');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'sitename');

// Make the connection:
$dbc = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die ('Could not connect to MySQL: ' . mysqli_connect_error() );

// Set the encoding ...
mysql_set_charset($dbc, 'utf8');

Message:

Warning: mysql_set_charset() expects parameter 1 to be string, object given in C:\xampp\htdocs\mysqli_connect.php on line 18

I have inserted and removed single and double quotes throughout the script.
I have placed the script in the htdocs file, where it runs, and outside the htdocs file, where it is not found through localhost.

If I comment out the ...

 /* // Set the encoding ...
 mysql_set_charset($dbc, 'utf8');
 */

I get a blank page, as it should appear, I believe.

I have hundreds of questions, but will limit them to these.
If I leave this in my htdocs and comment out the 'mysqli_set_charset' as above, will I be able to complete the text book and have working scripts?

I understand that in real life, with a live server, this is not optimal.
Hopefully, once I master PHP enough to make an e-commerce site run and have a server with Apache on it, not xampp, I will be able to configure my files correctly and in a safe manner.
 
I believe there are only a few instances of the charset being different than utf-8 in our files for this textbook.
Can I run the script as is and maybe remove the comments when I get a flukey answer?

Do I need to upgrade to the latest versions of PHP and MySQL and start from scratch?

MikeFromRehobothBeach.

Link to comment
Share on other sites

 Share

×
×
  • Create New...