Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Error with mysql_pconnect

Posted on 2012-04-02
13
Medium Priority
?
521 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 111

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
Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

 
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 111

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 111

Accepted Solution

by:
Ray Paseur earned 2000 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 111

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 111

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 111

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

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!

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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 …
Suggested Courses

730 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