Jump to content

No database selected


DanielW

Recommended Posts

Hi 

I am quite wet behind the ears working with php MySQL ect, I have been trying to make a webpage that will search a MySQL database that I have that holds a vareity of information about US States.

 

however when I hit the search button I get the message "No database Selected" ive been trying to figure this out on and off for most of the day.

 

would anyone with more experience be able to see where I am going wrong.

 

Thanks

 

connect.php

<?php
$dbConnect = array('server' => 'localhost',
		   'user' => 'root',
		   'pass' => '',
		   'name' => 'states2014'
		   );

$db = new mysqli($dbConnect['server'],
			$dbConnect['user'],
			$dbConnect['pass'],
			$dbConnect['name']
			);

echo $db ->host_info;
echo "<br>";
echo $db ->connect_errno;
echo "<br>";

if($db->connect_errno>0)
{
	echo "Database Connection Error".$db->connect_error;
	exit;	
}
?>

search.php

<?php

$search_output = "";
if(isset($_POST['searchquery']) && $_POST['searchquery'] != "")
{
$searchquery = preg_replace('#[^a-z 0-9?!]#i', '', $_POST['searchquery']);
if($_POST['filter1'] == "City")
{
$sqlCommand = "SELECT city_id, city_name AS city_name FROM city WHERE city_name LIKE '%$searchquery%'";
} 
else if($_POST['filter1'] == "Attractions")
{
//$sqlCommand = "SELECT city_id, city_name AS city_name FROM city WHERE city_name LIKE '%$searchquery%'";
} 
else if($_POST['filter1'] == "Famous_Person")
{
//$sqlCommand = "SELECT city_id, city_name AS city_name FROM city WHERE city_name LIKE '%$searchquery%'";
}
include_once("connect.php");
$query = mysql_query($sqlCommand) or die(mysql_error());
$count = mysql_num_rows($query);
if($count > 1)
{
$search_output .= "<hr />$count results for <strong>$searchquery</strong><hr />$sqlCommand<hr />";
while($row = mysql_fetch_array($query))
{
    $city_id = $row["city_id"];
    $city_name = $row["city_name"];
    $search_output .= "Item ID: $city_id - $city_name <br/>";
} 
// close while
} 
else 
{
$search_output = "<hr />0 results for <strong>$searchquery</strong><hr />$sqlCommand";
}
}
?>
<html>
<head>
</head>
<body>
<h2>US States Info</h2>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Search For: 
  <input name="searchquery" type="text" size="44" maxlength="88"> 
Within: 
<select name="filter1">
<option value="City">City</option>
<option value="Attractions">Attractions</option>
<option value="Famous_Person">Famous_Person</option>
</select>
<input name="myBtn" type="submit">
<br />
</form>
<div>
<?php echo $search_output; ?>
</div>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/288018-no-database-selected/
Share on other sites

in connect.php you're using mysqli to connect to mysql. But in search.php you're using the old mysql_* functions. These function libraries are not compatible with each other.

 

You need covert the code in search.php to use mysqli_query, mysqli_num_rows and mysqli_fetch_assoc

Hi,

 

you cannot mix the new MySQLi functions with the old mysql_* functions. Those are two entirely different extension from different times. I wonder how you even got this idea, because I see people trying this again and again.

 

You either need to use MySQLi (which is recommended) or stick to the old extension which will be removed in the future. But you can't have both.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.