[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 864
  • Last Modified:

php error - no database selected

Hi everyone,

I'm trying to test out a php registration system up on my remote server. I uploaded all the files but when I go to the rego page, I get the following error:

No database selected

Below is the code I have in my connection file.

I don't know if it's because I've put the files into a subdirectory within the root folder of my site. Would this be causing the problem and do I need to have this reflected in the file path somehow?

Really appreciate any help.
<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_localhost = "localhost:/tmp/mysql5.sock";
$database_localhost = "olink_stm_site";
$username_localhost = "stmEvents";
$password_localhost = "mypassword";
$localhost = mysql_pconnect($hostname_localhost, $username_localhost, $password_localhost) or trigger_error(mysql_error(),E_USER_ERROR); 
?>

Open in new window

0
gwh2
Asked:
gwh2
  • 3
1 Solution
 
ChimerazaCommented:
does this also give you an error?
<?php
 
//Database constants
define("DB_SERVER", "localhost:/tmp/mysql5.sock");
define("DB_USER", "olink_stm_site");
define("DB_PASS", "stmEvents");
define("DB_NAME", "Tm2_mypassword");
 
	
	global $connection;
	$connection = mysql_pconnect(DB_SERVER,DB_USER,DB_PASS);
	if (!$connection) {
		die("Database connection failed: " . mysql_error());
	}
	
	
	$db_select = mysql_select_db(DB_NAME, $connection);  
	if(!$db_select) {
		die("Database selection failed: " . mysql_error());
	}
?>

Open in new window

0
 
ollyatstithiansCommented:
It looks like you have connected to the database server, but not selected the specific database. You need to add a mysql_select_db() function to your file.
Also, do you really need a persistent connection to your database? If not then use mysql_connect() instead of mysql_pconnect().

Olly.
<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_localhost = "localhost:/tmp/mysql5.sock";
$database_localhost = "olink_stm_site";
$username_localhost = "stmEvents";
$password_localhost = "mypassword";
$localhost = mysql_pconnect($hostname_localhost, $username_localhost, $password_localhost) or trigger_error(mysql_error(),E_USER_ERROR); 
$db = mysql_select_db($database_localhost, $localhost);
?>

Open in new window

0
 
ChimerazaCommented:
The connection files can be anywhere...just make sure they are relative to where they are being called..no relative from where they are being kept.

And obviously use this to call it..

<?php require_once("youdir/connection.php");?>
0
 
ChimerazaCommented:
yeah...agree with olly on the p_connect

use mysql_connect if possible...
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now