DanielW Posted April 25, 2014 Share Posted April 25, 2014 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> Quote Link to comment https://forums.phpfreaks.com/topic/288018-no-database-selected/ Share on other sites More sharing options...
Ch0cu3r Posted April 25, 2014 Share Posted April 25, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/288018-no-database-selected/#findComment-1477285 Share on other sites More sharing options...
Jacques1 Posted April 25, 2014 Share Posted April 25, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/288018-no-database-selected/#findComment-1477286 Share on other sites More sharing options...
Solution DanielW Posted April 25, 2014 Author Solution Share Posted April 25, 2014 ahh thank you, I had been using some video tutorials and they must have been old, I know now where to focus thank you Quote Link to comment https://forums.phpfreaks.com/topic/288018-no-database-selected/#findComment-1477291 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.