Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ERROR - No database selected 1

Status
Not open for further replies.

evil1966

MIS
Dec 2, 2013
57
US
I've used this code before and it's currently working for one website. Now testing on another it returns No database selected.

Code:
 <?php 
    
	$username="user";
	$host="localhost";
	$password="*****";
	$database="mydatabase";

	$con = mysql_connect($host,$username,$password);
    if(!$con)
    {
	 die("Unable to select database");
    }
    mysql_select_db($database,$con);
		
	$sql = "SELECT * FROM donors ORDER BY lastname, firstname";
	
    $result = mysql_query($sql) or die(mysql_error());

  ?>
I can't seem to find any reason why. I've seen some stuff about saying I should be using mysqli. I tried but got:

"Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given in /home/mediqw5/public_html/bvcenter/test.php on line 31"

and 2 similar errors for the $result line.

Thanks.
 
The first thing is to try to find out why the DB is not being selected.

Code:
mysql_select_db($database,$con) [b]or die(mysql_error())[/b];

In regards to the mysqli error. If you are going to use mysqli, all your functions need to be mysqli. mysqli_select_db function expects the first param you pass it to be an object returned by the mysqli connection function.

PHP Manual said:
$link: Procedural style only: A link identifier returned by mysqli_connect() or mysqli_init()




----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Thanks Vacunita! It turns out I forgot to add database rights to the user. I'm back to work!
 
You're welcome.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top