Forum Moderators: coopster

Message Too Old, No Replies

mysql_pconnect AND mysql_connect

Need help changing from pconnect to connect!

         

UltimateX

6:45 pm on Apr 14, 2005 (gmt 0)

10+ Year Member



Hey guys! I have a problem with one of the scripts i wrote. The script uses seperate files. There's functions.php and some other files that use the functions.php file. Here's a little code fragment that needs to be changed in the functions.php file:


if($db = mysql_pconnect($dbhost, $dbuser, $dbpasswd)){
mysql_select_db($dbname, $db);
} else {
echo mysql_error();
exit();
}

I'm trying to convert the mysql_pconnect to a mysql_connect because my server doesn't allow more than X amount of concurrent connections to the database.

I already tried changing the call to mysql_connect but I get a "No database selected" error when I run it. Any ideas? Any kind of help would be appreciated!

mcibor

6:57 pm on Apr 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What exactly do you have there? Because I use the below code without any problems:

$conn = mysql_connect($dbhost, $dbuser, $dbpasswd) or die(mysql_error());
mysql_select_db($dbname, $conn) or die(mysql_error());

$result = mysql_query($query, $conn) or die(mysql_error());

And it works without any problems.

Post the code if it's still not working.

Best regards
Michal Cibor

UltimateX

7:20 pm on Apr 14, 2005 (gmt 0)

10+ Year Member



That works for me too when I make a seperate file with just that in it and call that file directly from the browser. The script's too big to post it here. But it's like this:

if($db = mysql_pconnect($dbhost, $dbuser, $dbpasswd)){
mysql_select_db($dbname, $db);
} else {
echo mysql_error();
exit();
}

function slashes($var){
if (is_array($var))
return array_map("slashes", $var);
else
return addslashes($var);
}

function increment_user_comment_count($user_id){
global $tb_users;
$sql = "
update
$tb_users
set
total_comments = total_comments + 1
where
id = '$user_id'
";
$query = mysql_query($sql) or die(mysql_error());
}

and so on...

And I'm including this file (functions.php) into the rest of the script. It seems to work fine with the mysql_pconnect command, but when I replace that with mysql_connect, I get a "No database selected" error.

ironik

10:17 pm on Apr 14, 2005 (gmt 0)

10+ Year Member



the 2 functions are virtual identical in the manner that you call them. It's a little strange that it's not working, but perhaps it's the way that you are calling it. Try popping the connection onto a seperate line (you might at least get a better error message back telling you what's wrong):

$db = mysql_connect($dbhost, $dbuser, $dbpasswd);
if (!$db) {
die('Could not connect: ' . mysql_error());
}

$db_selected = mysql_select_db($dbname, $db);
if (!$db_selected) {
die ('Can\'t use '. $dbname .' : ' . mysql_error());
}

UltimateX

4:35 am on Apr 15, 2005 (gmt 0)

10+ Year Member



nope..still the same

dmmh

6:02 am on Apr 15, 2005 (gmt 0)

10+ Year Member



dangerous code if you ask me.......

jd01

7:07 am on Apr 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here is one more example of what could work...
I would try any of the connections listed, but hard line the connection variables as shown below. If they are correct and you still can't connect, the problem is the database, not the code.

If you can connect then the problem is in the passing of the variables.

(You could accomplish the same effect by defining the variables you are using on the page your connection string is on, but make sure if you do they are defined after the external page is included, or the external page will redefine the variables.)

$db = mysql_connect("database host", "user name", "password");
mysql_select_db("database", $db) or die ( mysql_error() . "\n" );

This should tell you where your error is originating, then you know quite a bit more about how to fix it.

mcibor

2:28 pm on Apr 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The problem lies within the function.php

What I would do is to create a function that does all the query stuff:

if(!function_exists('ask')
{
function ask($query)
{
$conn = mysql_connect("host", "user", "passwd") or die(mysql_error());
mysql_select_db("dbname", $conn) or die(mysql_error());
$result = mysql_query($query, $conn) or die(mysql_error());
return $result
}
}

In the function increment_user_comment_count you have to connect to db again:
not just mysql_query(), but the whole thing. In this example you can write: $query = ask($sql);
Michal Cibor

UltimateX

3:16 pm on Apr 17, 2005 (gmt 0)

10+ Year Member



Wait! Maybe that's the problem! Would you have to connect to the db everytime you want to execute the query with mysql_connect? And is there a way around this?

mcibor

3:39 pm on Apr 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, you don't have to connect to db everytime you ask a query.

The problem lies within the function. Any variable that is not passed to the function or is global is not seen.
Eg:

<?php
function f(){$a = 30;}

$a = 20;
f();
echo "a = ".$a;//will return: a = 20, not 30.
?>

Way around it, but I'm not sure, maybe passing the $conn variable to the function:

<?php
$conn = mysql_connect...

function increment_user_comment_count($user_id, $conn){
global $tb_users;
$sql = "update $tb_users set total_comments = total_comments + 1 where id = $user_id'";
$query = mysql_query($sql, $conn) or die(mysql_error());}?>