Solved

Error with mysql_pconnect

Posted on 2012-04-02
13
513 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 110

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 110

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 110

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 110

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 110

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 110

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

749 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