Forum Moderators: coopster
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!
$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
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.
$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());
}
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.
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
}
}
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());}?>