Solved

Error with mysql_pconnect

Posted on 2012-04-02
13
511 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 109

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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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 109

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
 
LVL 109

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 109

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 109

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 109

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
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 …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

839 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