?
Solved

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

Posted on 2008-10-20
6
Medium Priority
?
1,417 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
[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
6 Comments
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 1000 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 1000 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 111

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
Independent Software Vendors: 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!

 
LVL 111

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to count occurrences of each item in an array.
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 …
Suggested Courses

800 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