Jump to content

No database selected


DanielW
Go to solution Solved by 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
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.