Solved

php error - no database selected

Posted on 2009-04-02
4
853 Views
Last Modified: 2013-12-13
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
Comment
Question by:gwh2
[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
  • 3
4 Comments
 
LVL 3

Expert Comment

by:Chimeraza
ID: 24048765
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
 
LVL 10

Accepted Solution

by:
ollyatstithians earned 500 total points
ID: 24048784
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
 
LVL 3

Expert Comment

by:Chimeraza
ID: 24048785
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
 
LVL 3

Expert Comment

by:Chimeraza
ID: 24048793
yeah...agree with olly on the p_connect

use mysql_connect if possible...
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
scan php uploads for viruses 5 26
PHP encrypted string and passing to a ASP Page 12 46
while read ID; do 4 56
How to hide contact form only if no validation errors 4 27
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

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