Solved

Error with mysql_pconnect

Posted on 2012-04-02
13
504 Views
Last Modified: 2012-04-05
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

0
Comment
Question by:stkoontz
13 Comments
 
LVL 5

Expert Comment

by:DecKen
ID: 37795949
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

0
 
LVL 2

Author Comment

by:stkoontz
ID: 37796023
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
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 37796067
Just curious -- why are you making this change to use pconnect()?  What do you understand to be the deficiency in mysqli_connect()?
0
 
LVL 2

Author Comment

by:stkoontz
ID: 37796122
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
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 37796219
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.
0
 
LVL 2

Author Comment

by:stkoontz
ID: 37796317
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
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 37796345
Sure.  Line 10 of the original code snippet appears to have the function arguments in the wrong order. See this link:
http://php.net/manual/en/function.mysql-set-charset.php
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 37796365
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.
0
 
LVL 2

Author Comment

by:stkoontz
ID: 37796490
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
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 37796507
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.
0
 
LVL 7

Expert Comment

by:designatedinitializer
ID: 37804266
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.
0
 
LVL 2

Author Closing Comment

by:stkoontz
ID: 37814085
This was the answer to my question, but it sounds like the best advice is to stick with the non-persistent connections.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 37814473
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
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now