session_start(); include "mainPageClass.php"; /*database account*/ $_servername = "localhost"; $_username = "******"; $_password = "******"; $_db = "mto"; $conn = new mysqli($_servername, $_username, $_password, $_db); // Create connection if ($conn->connect_error) { // Check connection die("Connection failed: " . $conn->connect_error); } $sql = "SELECT * FROM products;"; $result = mysqli_query($conn, $sql) or die("<br/><br/>" . mysqli_error($conn)); //shows the problem is any problem with mySQL syntax if ($result->num_rows > 0) { $prodP = new productsPage(); $prodP->title = "Products"; $prodP->keywords = ""; if (isset($_SESSION["activeUser"])) { $prodP->content = "session: " . $_SESSION["activeUser"]["userEmail"]; } $prodP->active = "Products"; $prodP->result = $result; $prodP->displayPage(); } else { echo "Nothing found."; } $conn->close();
} $search_exploded = explode(" ", $search); $x = 0; foreach ($search_exploded as $search_each) { $x++; $sql = ""; if ($x == 1) { //to search in each field of the table $sql .= "Concat(prodName, '', prodShortDescription, '', prodDescription, prodCategory) LIKE '%{$search_each}%' "; } else { $sql .= "AND Concat(prodName, '', prodShortDescription, '', prodDescription, prodCategory) LIKE '%{$search_each}%' "; } } $sql_final = "SELECT * FROM products WHERE {$sql};"; $result = mysqli_query($conn, $sql_final) or die("<br/><br/>" . mysqli_error($conn)); //shows the problem is any problem with mySQL syntax if ($result->num_rows > 0) { //items found and displayed $success = true; } else { $supportMsg = "Nothing found. Make sure you have no spelling mistakes."; } $conn->close(); } } $search_result = new productsPage(); $search_result->supportMsg = $supportMsg; if ($success) { $search_result->result = $result; } $search_result->displayPage();