anevins Posted March 10, 2011 Share Posted March 10, 2011 Hi, I have a problem if users search something which doesn't match the data in the table I'm selecting from. At the moment I'm trying this code: <?php require_once('inc/global.inc.php'); # search.inc.php /* * This is the search content module. * This page is included by index.php. * This page expects to receive $_GET['terms']. */ // Redirect if this page was accessed directly: if (!defined('BASE_URL')) { // Need the BASE_URL, defined in the config file: require_once ('../includes/config.inc.php'); // Redirect to the index page: $url = BASE_URL . 'index.php?p=search'; // Pass along search terms? if (isset($_GET['terms'])) { $url .= '&terms=' . urlencode($_GET['terms']); } header ("Location: $url"); exit; } // End of defined() IF. // Print a caption: echo '<h2>Search Results</h2>'; // Display the search results if the form // has been submitted. if (isset($_GET['terms']) && ($_GET['terms'] != 'Search...') ) { $terms = $_GET['terms']; $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die *('Error connecting to MySQL server'); // Query the database. $query = "SELECT * FROM product WHERE title LIKE '%$terms%'"; // Fetch the results. //$row = mysqli_fetch_array($result); // Print the results: $result=mysqli_query($dbc,$query); if ($result == 0){ echo "<h3>Sorry,</h3>"; echo "<p>your search: "" .$terms. "" returned zero results</p>"; } while($row=mysqli_fetch_assoc($result)) { $output[] = '<ul>'; $output[] = '<li>'.$row['title'] .': £'.$row['price'].'<br /><img src="'.$row['img'].'" alt="'.$row['title'].'" /></li>'; $output[] = '</ul>'; } echo join('',$output); } else { // Tell them to use the search form. echo '<p class="error">Please use the search form at the top of the window to search this site.</p>'; } ?> But I'm getting this error: Object of class mysqli_result could not be converted to int So that little if statement here doesn't work: if ($result == 0){ echo "<h3>Sorry,</h3>"; echo "<p>your search: "" .$terms. "" returned zero results</p>"; } Is there another way I can display no search matched results? Thank you for reading, Andrew. Link to comment https://forums.phpfreaks.com/topic/230242-search-results/ Share on other sites More sharing options...
jordanmoore Posted March 10, 2011 Share Posted March 10, 2011 Hello, I took a quick look at your code and I've had this following idea. Try using this line of code: if ($result === false){ echo "<h3>Sorry,</h3>"; echo "<p>your search: "" .$terms. "" returned zero results</p>"; } Link to comment https://forums.phpfreaks.com/topic/230242-search-results/#findComment-1185711 Share on other sites More sharing options...
anevins Posted March 10, 2011 Author Share Posted March 10, 2011 That gets rid of one error; for that particular if statement, but now I have another error which I think is greater than this if statement. Here's the new error: Undefined variable: output So the new code is: <?php require_once('inc/global.inc.php'); # search.inc.php /* * This is the search content module. * This page is included by index.php. * This page expects to receive $_GET['terms']. */ // Redirect if this page was accessed directly: if (!defined('BASE_URL')) { // Need the BASE_URL, defined in the config file: require_once ('../includes/config.inc.php'); // Redirect to the index page: $url = BASE_URL . 'index.php?p=search'; // Pass along search terms? if (isset($_GET['terms'])) { $url .= '&terms=' . urlencode($_GET['terms']); } header ("Location: $url"); exit; } // End of defined() IF. // Print a caption: echo '<h2>Search Results</h2>'; // Display the search results if the form // has been submitted. if (isset($_GET['terms']) && ($_GET['terms'] != 'Search...') ) { $terms = $_GET['terms']; $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die *('Error connecting to MySQL server'); // Query the database. $query = "SELECT * FROM product WHERE title LIKE '%$terms%'"; // Fetch the results. //$row = mysqli_fetch_array($result); // Print the results: $result=mysqli_query($dbc,$query); if ($result == false){ echo "<h3>Sorry,</h3>"; echo "<p>your search: "" .$terms. "" returned zero results</p>"; } while($row=mysqli_fetch_assoc($result)) { $output[] = '<ul>'; $output[] = '<li>'.$row['title'] .': £'.$row['price'].'<br /><img src="'.$row['img'].'" alt="'.$row['title'].'" /></li>'; $output[] = '</ul>'; } echo join('',$output); } else { // Tell them to use the search form. echo '<p class="error">Please use the search form at the top of the window to search this site.</p>'; } ?> Link to comment https://forums.phpfreaks.com/topic/230242-search-results/#findComment-1185713 Share on other sites More sharing options...
jordanmoore Posted March 10, 2011 Share Posted March 10, 2011 It looks like you didn't create the output array before you started using it. Try this: <?php require_once('inc/global.inc.php'); # search.inc.php /* * This is the search content module. * This page is included by index.php. * This page expects to receive $_GET['terms']. */ // Redirect if this page was accessed directly: if (!defined('BASE_URL')) { // Need the BASE_URL, defined in the config file: require_once ('../includes/config.inc.php'); // Redirect to the index page: $url = BASE_URL . 'index.php?p=search'; // Pass along search terms? if (isset($_GET['terms'])) { $url .= '&terms=' . urlencode($_GET['terms']); } header ("Location: $url"); exit; } // End of defined() IF. // Print a caption: echo '<h2>Search Results</h2>'; // Display the search results if the form // has been submitted. if (isset($_GET['terms']) && ($_GET['terms'] != 'Search...') ) { $terms = $_GET['terms']; $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die *('Error connecting to MySQL server'); // Query the database. $query = "SELECT * FROM product WHERE title LIKE '%$terms%'"; // Fetch the results. //$row = mysqli_fetch_array($result); // Print the results: $result=mysqli_query($dbc,$query); if ($result === false){ echo "<h3>Sorry,</h3>"; echo "<p>your search: "" .$terms. "" returned zero results</p>"; } $output = Array(); while($row=mysqli_fetch_assoc($result)) { $output[] = '<ul>'; $output[] = '<li>'.$row['title'] .': £'.$row['price'].'<br /><img src="'.$row['img'].'" alt="'.$row['title'].'" /></li>'; $output[] = '</ul>'; } echo join('',$output); } else { // Tell them to use the search form. echo '<p class="error">Please use the search form at the top of the window to search this site.</p>'; } ?> Link to comment https://forums.phpfreaks.com/topic/230242-search-results/#findComment-1185715 Share on other sites More sharing options...
anevins Posted March 10, 2011 Author Share Posted March 10, 2011 Brilliant, thank you, I'm almost there. I'm still getting a blank page where there should be "Sorry, your search ...". Have I positioned this if statement in the wrong place? if ($result == false){ echo "<h3>Sorry,</h3>"; echo "<p>your search: "" .$terms. "" returned zero results</p>"; } Link to comment https://forums.phpfreaks.com/topic/230242-search-results/#findComment-1185718 Share on other sites More sharing options...
jordanmoore Posted March 10, 2011 Share Posted March 10, 2011 Are you using the three equals signs - "===" - like I suggested or just the two - "==" - like I saw in your code snippet? If you're using just the two then try replacing "false" with "null". However if you use the three then you should be able to use "0", "false" or "null". Link to comment https://forums.phpfreaks.com/topic/230242-search-results/#findComment-1185719 Share on other sites More sharing options...
anevins Posted March 10, 2011 Author Share Posted March 10, 2011 I am sorry, I missed out that. Not to worry, here's the new if statement: if (mysqli_num_rows($data) === 0) { mysqli_query($dbc, $query); echo "Thank you, your report has been received."; } Okay I've still got the blank page Link to comment https://forums.phpfreaks.com/topic/230242-search-results/#findComment-1185720 Share on other sites More sharing options...
jordanmoore Posted March 10, 2011 Share Posted March 10, 2011 Right, I've got a final idea on this - but unfortunately I'm not very good at php mySql related queries. I've looked at the php manual and your query should be returning false. Here is the code I think may work for you: <?php require_once('inc/global.inc.php'); # search.inc.php /* * This is the search content module. * This page is included by index.php. * This page expects to receive $_GET['terms']. */ // Redirect if this page was accessed directly: if (!defined('BASE_URL')) { // Need the BASE_URL, defined in the config file: require_once ('../includes/config.inc.php'); // Redirect to the index page: $url = BASE_URL . 'index.php?p=search'; // Pass along search terms? if (isset($_GET['terms'])) { $url .= '&terms=' . urlencode($_GET['terms']); } header ("Location: $url"); exit; } // End of defined() IF. // Print a caption: echo '<h2>Search Results</h2>'; // Display the search results if the form // has been submitted. if (isset($_GET['terms']) && ($_GET['terms'] != 'Search...') ) { $terms = $_GET['terms']; $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die *('Error connecting to MySQL server'); // Query the database. $query = "SELECT * FROM product WHERE title LIKE '%$terms%'"; // Fetch the results. //$row = mysqli_fetch_array($result); // Print the results: if($result=mysqli_query($dbc,$query)){ $output = Array(); while($row=mysqli_fetch_assoc($result)) { $output[] = '<ul>'; $output[] = '<li>'.$row['title'] .': £'.$row['price'].'<br /><img src="'.$row['img'].'" alt="'.$row['title'].'" /></li>'; $output[] = '</ul>'; } echo join('',$output); } else { echo "<h3>Sorry,</h3>"; echo "<p>your search: "" .$terms. "" returned zero results</p>"; } } else { // Tell them to use the search form. echo '<p class="error">Please use the search form at the top of the window to search this site.</p>'; } ?> If this does not work then I'm not quite sure what you could do to fix it. You may try tracing the result variable to see the contents. I hope you're able to get this sorted. Link to comment https://forums.phpfreaks.com/topic/230242-search-results/#findComment-1185729 Share on other sites More sharing options...
anevins Posted March 10, 2011 Author Share Posted March 10, 2011 Thank you for kindly spending your time helping me out. Unfortunately this hasn't worked and I still receive a blank results page instead of the desired feedback. All comments are welcome. Thanks. Link to comment https://forums.phpfreaks.com/topic/230242-search-results/#findComment-1185733 Share on other sites More sharing options...
anevins Posted March 10, 2011 Author Share Posted March 10, 2011 Link to comment https://forums.phpfreaks.com/topic/230242-search-results/#findComment-1185833 Share on other sites More sharing options...
jordanmoore Posted March 10, 2011 Share Posted March 10, 2011 Well I'm back again with another idea, try using this script: <?php require_once('inc/global.inc.php'); # search.inc.php /* * This is the search content module. * This page is included by index.php. * This page expects to receive $_GET['terms']. */ // Redirect if this page was accessed directly: if (!defined('BASE_URL')) { // Need the BASE_URL, defined in the config file: require_once ('../includes/config.inc.php'); // Redirect to the index page: $url = BASE_URL . 'index.php?p=search'; // Pass along search terms? if (isset($_GET['terms'])) { $url .= '&terms=' . urlencode($_GET['terms']); } header ("Location: $url"); exit; } // End of defined() IF. // Print a caption: echo '<h2>Search Results</h2>'; // Display the search results if the form // has been submitted. if (isset($_GET['terms']) && ($_GET['terms'] != 'Search...') ) { $terms = $_GET['terms']; $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die *('Error connecting to MySQL server'); // Query the database. $query = "SELECT * FROM product WHERE title LIKE '%$terms%'"; // Fetch the results. //$row = mysqli_fetch_array($result); // Print the results: $result=mysqli_query($dbc,$query); if (!$result){ echo "<h3>Sorry,</h3>"; echo "<p>your search: "" .$terms. "" returned zero results</p>"; } else { $output = Array(); while($row=mysqli_fetch_assoc($result)) { $output[] = '<ul>'; $output[] = '<li>'.$row['title'] .': £'.$row['price'].'<br /><img src="'.$row['img'].'" alt="'.$row['title'].'" /></li>'; $output[] = '</ul>'; } echo join('',$output); } } else { // Tell them to use the search form. echo '<p class="error">Please use the search form at the top of the window to search this site.</p>'; } ?> Link to comment https://forums.phpfreaks.com/topic/230242-search-results/#findComment-1185836 Share on other sites More sharing options...
anevins Posted March 10, 2011 Author Share Posted March 10, 2011 Welcome back I've tried that script and I receive the same results; the same blank page Link to comment https://forums.phpfreaks.com/topic/230242-search-results/#findComment-1185846 Share on other sites More sharing options...
jordanmoore Posted March 10, 2011 Share Posted March 10, 2011 Have you tried printing the "$result" variable to see what content it holds? Also, you could try placing several traces over the script, just as simple as something like echo "1"; - where you increment the number. This will give you a good idea of where the code is going. Link to comment https://forums.phpfreaks.com/topic/230242-search-results/#findComment-1185860 Share on other sites More sharing options...
anevins Posted March 10, 2011 Author Share Posted March 10, 2011 If it's possible, I'll come back to this thread tomorrow as I have to sleep now, Thanks for your help. Link to comment https://forums.phpfreaks.com/topic/230242-search-results/#findComment-1185885 Share on other sites More sharing options...
anevins Posted March 11, 2011 Author Share Posted March 11, 2011 Okay I'm back, I've outputted $result and it gave me this error: Object of class mysqli_result could not be converted to string Link to comment https://forums.phpfreaks.com/topic/230242-search-results/#findComment-1186076 Share on other sites More sharing options...
anevins Posted March 11, 2011 Author Share Posted March 11, 2011 Forgive me if I'm late to reply, I'll try and visit these threads in my timetable intervals between lectures. Link to comment https://forums.phpfreaks.com/topic/230242-search-results/#findComment-1186082 Share on other sites More sharing options...
anevins Posted March 11, 2011 Author Share Posted March 11, 2011 I want to use the num rows mysqli function so, if there are results greater than 0, do the query, else output 'no results etc.' I don't know how to implement this through my code, could anyone help me out here? <?php require_once('./includes/connectvars.php'); # search.inc.php /* * This is the search content module. * This page is included by index.php. * This page expects to receive $_GET['terms']. */ // Redirect if this page was accessed directly: if (!defined('BASE_URL')) { // Need the BASE_URL, defined in the config file: require_once ('../includes/config.inc.php'); // Redirect to the index page: $url = BASE_URL . 'index.php?p=search'; // Pass along search terms? if (isset($_GET['terms'])) { $url .= '&terms=' . urlencode($_GET['terms']); } header ("Location: $url"); exit; } // End of defined() IF. // Print a caption: echo '<h2>Search Results</h2>'; // Display the search results if the form // has been submitted. if (isset($_GET['terms']) && ($_GET['terms'] != 'Search...') ) { $terms = $_GET['terms']; $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die *('Error connecting to MySQL server'); // Query the database. $query = "SELECT * FROM product WHERE title LIKE '%$terms%'"; // Fetch the results. //$row = mysqli_fetch_array($result); // Print the results: $result=mysqli_query($dbc,$query); if (!$result){ echo "<h3>Sorry,</h3>"; echo "<p>your search: "" .$terms. "" returned zero results</p>"; } else { $output = Array(); while($row=mysqli_fetch_assoc($result)) { $output[] = '<ul>'; $output[] = '<li>'.$row['title'] .': £'.$row['price'].'<br /><img src="'.$row['img'].'" alt="'.$row['title'].'" /></li>'; $output[] = '</ul>'; } echo join('',$output); } } else { // Tell them to use the search form. echo '<p class="error">Please use the search form at the top of the window to search this site.</p>'; } ?> Link to comment https://forums.phpfreaks.com/topic/230242-search-results/#findComment-1186184 Share on other sites More sharing options...
anevins Posted March 11, 2011 Author Share Posted March 11, 2011 I'm trying to output a message when there are no results for the search, this is my code: <?php require_once('./includes/connectvars.php'); # search.inc.php /* * This is the search content module. * This page is included by index.php. * This page expects to receive $_GET['terms']. */ // Redirect if this page was accessed directly: if (!defined('BASE_URL')) { // Need the BASE_URL, defined in the config file: require_once ('../includes/config.inc.php'); // Redirect to the index page: $url = BASE_URL . 'index.php?p=search'; // Pass along search terms? if (isset($_GET['terms'])) { $url .= '&terms=' . urlencode($_GET['terms']); } header ("Location: $url"); exit; } // End of defined() IF. // Print a caption: echo '<h2>Search Results</h2>'; // Display the search results if the form // has been submitted. if (isset($_GET['terms']) && ($_GET['terms'] != 'Search...') ) { $terms = $_GET['terms']; $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die *('Error connecting to MySQL server'); // Query the database. $query = "SELECT * FROM product WHERE title LIKE '%$terms%'"; // Fetch the results. $result=mysqli_query($dbc,$query); // Print the results: $num_rows = mysql_num_rows($result); if ($num_rows > 0){ $output = Array(); $output[] = '<ul>'; $output[] = '<li>'.$row['title'] .': £'.$row['price'].'<br /><img src="'.$row['img'].'" alt="'.$row['title'].'" /></li>'; $output[] = '</ul>'; echo join('',$output); } else { echo "<h3>Sorry,</h3>"; echo "<p>your search: "" .$terms. "" returned zero results</p>"; } } else { // Tell them to use the search form. echo '<p class="error">Please use the search form at the top of the window to search this site.</p>'; } ?> I think there's some conflict with the mysql and mysqli funcitons, got any ideas anyone? Link to comment https://forums.phpfreaks.com/topic/230242-search-results/#findComment-1186306 Share on other sites More sharing options...
anevins Posted March 11, 2011 Author Share Posted March 11, 2011 Thanks for all your help everyone, I have finished with the search as it is completed. Link to comment https://forums.phpfreaks.com/topic/230242-search-results/#findComment-1186312 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.