Solved

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

Posted on 2008-10-20
6
1,378 Views
Last Modified: 2013-12-13
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
Comment
Question by:acecraig100
6 Comments
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 22763276
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
 
LVL 82

Assisted Solution

by:hielo
hielo earned 250 total points
ID: 22763298
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
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 22763308
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 109

Expert Comment

by:Ray Paseur
ID: 22763351
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
 
LVL 13

Expert Comment

by:AielloJ
ID: 22763461
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
 

Author Comment

by:acecraig100
ID: 22763685
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

808 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