Solved

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

Posted on 2008-10-20
6
1,344 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 108

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 108

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 108

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
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.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now