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>