Link to home
Start Free TrialLog in
Avatar of stkoontz
stkoontzFlag for United States of America

asked on

Error with mysql_pconnect

I'm trying to switch from mysqli_connect to mysql_pconnect.  When I use the below code, I get the error message "Warning: mysql_set_charset() expects parameter 1 to be string..."  Anyone have any ideas what my problem is?

Thanks,

Steve

<?php
$link = mysql_pconnect('host', 'username', 'password');
if (!$link)
{
	$error = 'Unable to connect to the database server.';
	include 'http://registration.cenational.org/output.php';
	exit();
}

if (!mysql_set_charset($link, 'utf8'))
{
	$output = 'Unable to set database connection encoding.';
	include 'http://registration.cenational.org/output.php';
	exit();
}

if (!mysql_select_db($link, 'table'))
{
	$error = 'Unable to locate the database.';
	include 'http://registration.cenational.org/output.php';
	exit();
}
?>

Open in new window

Avatar of DecKen
DecKen
Flag of Ireland image

Try changing this part of the code
if (!mysql_set_charset($link, 'utf8'))
{
	$output = 'Unable to set database connection encoding.';
	include 'http://registration.cenational.org/output.php';
	exit();
}

Open in new window


to
if (!mysql_set_charset('utf8',$link))
{
	$output = 'Unable to set database connection encoding.';
	include 'http://registration.cenational.org/output.php';
	exit();
}

Open in new window

Avatar of stkoontz

ASKER

Thanks for the quick response.  I tried switching the code and got this error message...

mysql_select_db(): supplied argument is not a valid MySQL-Link resource

Steve
Just curious -- why are you making this change to use pconnect()?  What do you understand to be the deficiency in mysqli_connect()?
I've been told that it's better to have the connection stay open, then to have it open and close multiple times.  From what I understand, with pconnect there's less chance of running out of connections because the system will see that the connection is already open and not need to open it again.

Steve
It "sounds like a good idea" but I'm not sure it's an advantage.  See the large red warning on this page (and the user-contributed notes).
http://php.net/function.mysql-pconnect

See also the discussion here:
http://php.net/manual/en/features.persistent-connections.php

If you find that to be TLDR, then the executive summary might be this: If you have found that your web site is unacceptably slow and you have optimized all the queries and indexed all the needed columns, and nothing has helped, and you have determined that the only identifiable cause of the slowness is the latency period caused by the SQL connection, you might consider persistent connections.  Absent all that, just use the regular connection functions.  In other words, it's a lot of effort, and it has a considerable risk (table locking, name collisions) for very, very little performance advantage.  I would not do it.
Thanks for the advice.  I'll keep that in mind and probably not switch unless I run into problems down the road.  

But I would like to know how to make persistent connections work if I need to.  Can you, or anyone else, help me with that?

Thanks,

Steve
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Then on line 17, ... the same kind of thing.
http://php.net/manual/en/function.mysql-select-db.php

The mysqli arguments are often reversed when compared to the mysql arguments.
I think I got it to work.  But it looks like I'm going to have to switch the code throughout my website

$rsClass = mysqli_query($link,"SELECT id_class...)

will need to become
$rsClass = mysql_query("SELECT id_class..., $link)

is that correct?

Thanks,

Steve
Yep.  Pretty annoying, isn't it!  If you have some kind of abstraction layer, you may be able to make the change there, but if you have many queries throughout many scripts it's just a matter of slogging through to the end.
Just do yourself a favor and use mysqli extension or PDO.
That mysql_ (...) stuff is absolutely outdated.
One day, your server software is gonna get automatically upgraded to some PHP version when it simply isn't supported anymore, and then all your site will break.
(of course that will be quite some time from now... but still... just do it!).

...and do yourself another favor and write a class for it (abstraction layer) like Ray already said.
This was the answer to my question, but it sounds like the best advice is to stick with the non-persistent connections.
I agree - the non-persistent connections make more sense to me in the HTTP environment.  In case you're interested in seeing the effect of repeated connect functions, use microtime() to check the time before and after the connect function.  You may be pleasantly surprised!  Best regards, ~Ray