Solved

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

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

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 110

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
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!

 
LVL 110

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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

733 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