• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1462
  • Last Modified:

Warning: mysqli_select_db() [function.mysqli-select-db] error message

I am attempting to learn PHP. I am working my way through a book. In the following code, I receive a message telling me that I have successfully connected to the database server and have opened the database.  But I recevie the following error messages:

Warning: mysqli_select_db() [function.mysqli-select-db]: Couldn't fetch mysqli in C:\xampp\htdocs\FlightlogEntries.php on line 29

Warning: mysqli_close() [function.mysqli-close]: Couldn't fetch mysqli in C:\xampp\htdocs\FlightlogEntries.php on line 30

I was wondering if someone could explain why I am getting this error.
<?php
$DBConnect = mysqli_connect("localhost", "username", "password");
if (!$DBConnect)
	echo "<p>The database server is not available.</p>";
	else {
		echo "<p>Successfully connected to the database server.</p>";
		$DBName = "flightlog";
		if (mysqli_select_db($DBConnect, $DBName))
			echo "<p>Successfully opened the database.</p>";
		else
			echo "<p>The database is not available.</p>";
		mysqli_close($DBConnect);
		}
mysqli_select_db($DBConnect, $DBName);
mysqli_close($DBConnect);
?>

Open in new window

0
acecraig100
Asked:
acecraig100
2 Solutions
 
Ray PaseurCommented:
Use mysqli_error() to find out what the error is.  See:
http://us.php.net/manual/en/function.mysql-error.php


if (!$resource_id = mysqli_select_db($DBConnect, $DBName)) { echo mysqli_error(); die(' - MYSQLI SELECT FAILED'); }

Open in new window

0
 
hieloCommented:
if the else clause is not executing, then the last two statements would generate errors because you do not have an active connection.
<?php
$DBConnect = mysqli_connect("localhost", "username", "password");
if (!$DBConnect){
      echo "<p>The database server is not available.</p>";
	 printf("Errormessage: %s\n", mysqli_error($DBConnect));
}
else {
          echo "<p>Successfully connected to the database server.</p>";
          $DBName = "flightlog";
          if (mysqli_select_db($DBConnect, $DBName))
                  echo "<p>Successfully opened the database.</p>";
          else
                  echo "<p>The database is not available.</p>";
          mysqli_close($DBConnect);
		mysqli_close($DBConnect);
}
?>

Open in new window

0
 
Ray PaseurCommented:
You might also want to check the balance of your brackets - count pairs of left and right  { }

And while you're at it, be aware that there are meanings to capitalization in PHP.  Defined constants are typically always in all caps, varilbles are in lower case, and classes are capitalized.  This is not law, but convention and a best practice in programming.

Also, I may have given you an incorrect reference.  It looks like mysqli is OO code, and your code above looks like procedural code.  The mysqli reference is available here:

http://us2.php.net/manual/en/class.mysqli.php

Best regards, ~Ray
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Ray PaseurCommented:
FWIW here is my db connect script that I include in all the pages that need access to the db.  The local function "warning_RAY" sends me an email when an error occurs.

If you do what hielo and I suggest (testing the success of those connect and select statements) you will be able to debug this very quickly.

The advantage of a separate connect script that you include in every page is this: once you have it right, you can "set it and forget it!"

HTH, ~Ray
<?php // ../db_link.php IN THE ROOT DIRECTORY (above WWW)
 
$db_host	= "localhost";
$db_name	= "username_databasename";
$db_user	= "username_databaseuser";
$db_word	= "appropriate_password";
 
// CONNECT TO THE DATA BASE
if (!$db_connection = @mysql_connect("localhost", "$db_user", "$db_word")) {
	$errmsg	= mysql_errno() . ' ' . mysql_error();
	echo "\n\n\n\n<!-- ! db_connection -->";
	echo "\n<!-- $errmsg -->\n\n\n\n";
	warning_RAY($errmsg);
}
 
// SELECT THE DATA BASE
if (!$db_sel = @mysql_select_db($db_name, $db_connection)) {
	$errmsg	= mysql_errno() . ' ' . mysql_error();
	echo "\n\n\n\n<!-- ! db_sel -->";
	echo "\n<!-- $errmsg -->\n\n\n\n";
	warning_RAY($errmsg);
 
}
 
?>

Open in new window

0
 
AielloJCommented:
One of your else clauses appears to be unbalanced.  I always reccomend the use of opening and closing braces in if's, while's, etc.  You can eliminate them when there's only one line.  The problem occurrs when a second line is added to an if or else, properly indented to look like it's part of the condition, and the result is hours of head-banging debugging.
0
 
acecraig100Author Commented:
I goofed. I should have deleted lines 14 and 15 in my code. That took care of the error messages. Thanks for your help.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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