PHP Search Engine Display's nothing - php

I am running the following code and I cannot display anything on the page after the search is run. I know the connection is correct and that the search query is being loaded, so it must be something functionally wrong
<?php
$title = $_POST['search'];
echo $title ;
$con = mysqli_connect("localhost", "user", "password") or die ('Could not connect, this is the error: ' . mysql_error());
mysqli_select_db($con,"db") or die ('Sorry could not access database at this time. This is the error: ' . mysql_error());
if(isset($_POST['search']){
$searchq = $_POST['search'];
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
$search_sql = "SELECT title FROM gamereview WHERE tags LIKE '%".$_POST['search']."%' ";
$search_query = mysqli_query($con, $search_sql) or die("could not search");
$count = mysqli_num_rows($query);
if($count==0){
$output = 'THere was no search results!';
} else
while ($row = myqli_fetch_array($query, MYSQLI_BOTH)){
$title = $row['title'];
$id = $row['id'];
$output .= '<div>' .$id.' '.$title.'</div>';
}
}
echo $output;
mysql_close($con);
?>

You have assigned $_POST['SEARCH'] to a variable $searchq, yet you are not passing $searchq in your select statement. Also fix the myqli_fetch_array typo!!
$search_sql = "SELECT title FROM gamereview WHERE tags LIKE '%".$searchq."%' ";

Related

Can't get data from MySQL

I'm connected to phpmyadmin but I can not get any data from phpmyadmin.
my php version is 7.2.9 , I made everything that I wanted in database but php can't show the data in site ( I'm using localhost ).
here is the code:
<?php
$key = $_GET['key'];
$terms = explode(" ", $key);
$query = "SELECT * FORM search WHERE ";
foreach ($terms as $each){
$i++;
if($i == 1){
$query .= "keywords LIKE '%$each%' ";
} else{
$query .= "OR keywords LIKE '%$each%' ";
}
echo $query;
}
//connection
mysql_connect("localhost", "root", "");
mysql_select_db('search');
$query = mysqli_query($query);
$numrows = mysqli_num_rows($query);
if($numrows > 0){
while ($row = mysql_fetch_assoc($query)){
$id = $row['id'];
$title = $row['title'];
$description = $row['description'];
$keywords = $row['keywords'];
$link = $row['link'];
echo "<h2><a href='$link'>$title</h2></a>
$description<br /><br />";
}
}
else{
echo "No results found for \"<b>$key</b>\""; }
//disconnect
mysql_close();
?>
You have a couple of mistakes in your PHP/HTML. I'm gonna sum them up here so you can take a look at them:
<h2><a href='$link'>$title</h2></a>$description<br /><br /> This is wrong HTML. Close your a tag inside the h2.
You are connecting to you database through mysql, but querying through mysqli. Connect to your database with mysqli. Mysql_ family of functions have been removed in PHP 7
You have a typo in your query. you have written FORM instead of FROM.
You are exploding your $_GET variable on spaces. But i doubt if a $_GET variable has any spaces to begin with... Check if this is true.
first of all mysql_connect() is not anymore available after php 5. Instead of using mysql use mysqli_connect(). Please how to make connection and query database using php7 here.
https://www.w3schools.com/php/func_mysqli_fetch_row.asp
If still you have problem. Ask for help in comments.
<?php
$query = "SELECT * FORM search WHERE ";
foreach ($terms as $each){
$i++;
if($i == 1){
$query .= "keywords LIKE '%$each%' ";
} else{
$query .= "OR keywords LIKE '%$each%' ";
}
echo $query;
}
//connection
$conn = mysqli_connect("localhost", "root", "","search");
if(!$conn)die("Connection Error");
$query = mysqli_query($conn,$query);
if(!query)die("query error");
$numrows = mysqli_num_rows($query);
if($numrows > 0){
while ($row = mysqli_fetch_assoc($query)){
$id = $row['id'];
$title = $row['title'];
$description = $row['description'];
$keywords = $row['keywords'];
$link = $row['link'];
echo "<h2><a href='$link'>$title</h2></a>
$description<br /><br />";
}
}
else{
echo "No results found for \"<b>$key</b>\"";
}
You are mixing up mysqli and mysql. I have edited your stuff. Please try. Don't forget to fix the portion marked [missing]
<?php
$key = $_GET['key'];
$terms = explode(" ", $key);
foreach ($terms as $each){
$i++;
if($i == 1){
$query .= "keywords LIKE '%$each%' ";
} else{
$query .= " description OR keywords LIKE '%$each%' ";
}
echo $query;
}
//connection
$conn=mysqli_connect("localhost", "root", "");
mysqli_select_db($conn, 'search');
if($result= mysqli_query($conn, $query)){
$numrows = mysqli_num_rows($result);
}
if($numrows > 0){
while($row = mysqli_fetch_array($result)){
$id = $row['id'];
$title = $row['title'];
$description = $row['description'];
$keywords = $row['keywords'];
$link = $row['link'];
//echo "<h2><a href='$link'>$title</h2></a>
//$description<br /><br />";
echo '<h2><a href="' . $link . '">' . $title .
'</h2></a>' . $description . '<br /><br />';
}
}
else{
echo "No results found for \"<b>$key</b>\""; }
//disconnect
mysqli_close();
?>
Here's the Full working Example TESTED at my end (Last night I wasn't at my work machine and couldn't test the code.Later I created a small db and Tested it. I had searched with the dummy key 'ram mary albert' in my example
<?php
$key = $_GET['key'];
$terms = explode(" ", $key);
$qu1 = "SELECT * FROM search WHERE ";
$qu2 = "order by id ASC";
$conn=mysqli_connect("localhost", "root", "");
mysqli_select_db($conn, 'search');
for($i=0; $i< count($terms); $i++){
$query = $qu1 . " keywords LIKE '%$terms[$i]%' " . $qu2;
echo( $query . "<br>" );
$resulter= mysqli_query($conn, $query);
while($row = mysqli_fetch_array($resulter)){;
$id = $row['id'];
$title = $row['title'];
$description = $row['description'];
$keywords = $row['keywords'];
$link = $row['link'];
$EscLink='\'' . $link . '\'';
echo ('<a href="javascript:void(0)" onClick="alert(' .
$EscLink . ')">' . $title . '</a><br>' . $description .
'<br /><br />');
} // Close While
} // Close for
//disconnect
mysqli_close($conn);
?>

php and my sql search db error

Here is the code :-
<?php
include 'include/header.php';
include 'include/connect.php';
mysql_select_db("search_db") or die("Couldn't select database.");
$output = '';
//collect
if(isset($_POST['search']) && $_POST['search'] != "") {
$searchq = $_POST['search'];
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
$query = mysql_query("SELECT * FROM people WHERE name LIKE '%".
$searchq . "%' OR id LIKE '" . $searchq . "';");
$count = mysql_num_rows($query);
if($count == 0) {
$output = 'There was no search results!';
} else {
while ($row = mysql_fetch_array($query)) {
$fname = $row ['name'];
$id = $row ['id'];
$output .= '<div>'.$name.' '.$id.'</div>';
}
}
}
?>
<!--html-->
<form action="search.php" method="post">
<input type="text" name="search" placeholder="Search..">
<input type="submit"value="search">
I am trying to set up search in my website using php and mysql I am getting an error like this " An error has occurred - could not connect to the database."
Let me know search result code like name , user photo, description, type box anyone can help us appreciated

Ignore some letters in a postcode search. e.g Database contains DT11 but user searches DT11 0QD.

I have the code below for a postcode search. The table in my database holds area postcode values such as [DT11] but a user will likely search a complete postcode [DT11 0QD].
How do modify the below code to ignore the extra information and display a result?
<?php
mysql_connect ("localhost","user","password") or die ("could not connect");
mysql_select_db("commun91_pres128") or die ("could not find database");
$output = '';
//collect
if (isset($_POST['search'])){
$searchq = $_POST['search'];
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
$query = mysql_query("SELECT * FROM ps_pcsearch WHERE firstname LIKE '%$searchq%' OR lastname LIKE '%$searchq%'") or die ("could not search");
$count = mysql_num_rows ($query);
if ($count == 0){
$output = 'there were no search results';
}else{
while ($row = mysql_fetch_array ($query)) {
$a = "Your delivery day is . . . ";
$lname = $row['lastname'];
$id = $row ['id'];
$output .= '<div>'.$a.' '.$lname.'</div>';
}
}
}
?>
Just use wildcards:
where $user_postcode like concat('%', postcode, '%')

php Search engine the keywords are not working

When i would search for the keywords that i have specified in my database it will return everything from my database not just the corresponding links that have the keywords attached to the link. here is my code
<?php
$q = $_GET['q'];
$terms = explode(" ", $q);
$query = "SELECT * FROM search ";
foreach ($terms as $each){
$i=0;
$i++;
if ($i == 1)
$query .= "keywords LIKE '%$each%' ";
else
$query .= "OR keywords LIKE '%$each%' ";
}
//connect
mysql_connect("localhost", "root", "");
mysql_select_db("search");
$query = mysql_query("SELECT * FROM search");
$numrows = mysql_num_rows($query);
if ($numrows > 0){
while($row = mysql_fetch_assoc($query)){
$id = $row['id'];
$title = $row['title'];
$description = $row['description'];
$keywords = $row['keywords'];
$link = $row['link'];
echo "<h3><a href='$link'>$title</a></h3><h4>$link</h4>$description<br /><br />";
}
}
else
echo "<b>No Results Found</b><br><br>Suggestions:<br>
Make sure all words are spelled correctly.<br>
Try different keywords.<br>
Try more general keywords.";
//disconnect
mysql_close();
?>
<?php
$q = $_GET['q'];
$terms = explode(" ", $q);
//connect
mysql_connect("localhost", "root", "");
mysql_select_db("search");
$query = "SELECT * FROM search ";
$i=1;
foreach ($terms as $each){
if ($i == 1) {
$query .= "WHERE ";
$query .= "keywords LIKE '" . mysql_real_escape_string("%" . $each . "%") . "' ";
} else {
$query .= "OR keywords LIKE '" . mysql_real_escape_string("%" . $each . "%") . "' ";
}
$i++;
}
$query = mysql_query($query);
$numrows = mysql_num_rows($query);
if ($numrows > 0){
while($row = mysql_fetch_assoc($query)){
$id = $row['id'];
$title = $row['title'];
$description = $row['description'];
$keywords = $row['keywords'];
$link = $row['link'];
echo "<h3><a href='$link'>$title</a></h3><h4>$link</h4>$description<br /><br />";
}
} else {
echo "<b>No Results Found</b><br><br>Suggestions:<br>
Make sure all words are spelled correctly.<br>
Try different keywords.<br>
Try more general keywords.";
}
//disconnect
mysql_close();
?>
Fixes:
1) Removed second $query that was being defined. It selected all rows.
2) Moved initial $i declaration. It was being set back to 0 each loop.
3) Added WHERE
4) Moved $i++ after the if statement and set initial $i to 1.
5) Added mysql_real_escape_string so that data is escaped properly.
Recommendations:
I highly recommend taking a look at MySQLi (http://us2.php.net/mysqli) or PDO (http://us3.php.net/pdo)
Please let me know if this works or if you need further assistance.
A first sight, i see a couple of errors.
$i=0;
$i++;
if ($i == 1)
$i Will ALWAYS be one are.
you might want to move $i = 0; BEFORE the foreach
$query = mysql_query("SELECT * FROM search");
You build a query, but in the end you're not using it. you probably want to do : $query = mysql_query($query); instead. ( and also for code clarity using a different variable name for the output ? ) .
mysql_query is deprecated. Useless you're in a hurry, check PDO
First, you're missing the WHERE keyword before the conditions. So it should be:
foreach ($terms as $i => $each){
$each = mysql_real_escape_string($each); // Prevent SQL injection
if ($i == 0)
$query .= "WHERE keywords LIKE '%$each%' ";
else
$query .= "OR keywords LIKE '%$each%' ";
}
You don't need to increment your own counter variable, you can use the array indexes from $terms.
Second, after all that work to create $query, you're not using it. You wrote:
$query = mysql_query("SELECT * FROM search");
That should be:
$query = mysql_query($query);
BTW, it's generally a bad idea to reuse variables like that, it gets confusing when you use the same variable for different things. I suggest you call the second $query something like $results.
Change this line
$query .= "keywords LIKE '%$each%' ";
By
$query .= " Where keywords LIKE '%$each%' ";
And also cnhange this line
$query = mysql_query("SELECT * FROM search");
By
$query = mysql_query($query);

How to limit the amount of search results on each page like Google?

I am trying to limit the amount of search results on each page like Google does. I am also using a SQL database to organize my keywords and everything. Here is my code
<?php
$q = $_GET['q'];
$terms = explode(" ", $q);
//connect
mysql_connect("localhost", "root", "") or die ("Could Not Connect");
mysql_select_db("search") or die ("Could Not Connect To database");
$query = "SELECT * FROM search ";
$i=1;
foreach ($terms as $each){
if ($i == 1) {
$query .= "WHERE ";
$query .= "keywords LIKE '" . mysql_real_escape_string("%" . $each . "%") . "' ";
} else {
$query .= "OR keywords LIKE '" . mysql_real_escape_string("%" . $each . "%") . "' ";
}
$i++;
}
$query = mysql_query($query);
$numrows = mysql_num_rows($query);
if ($numrows > 0)
{
while($row = mysql_fetch_assoc($query))
{
$id = $row['id'];
$title = $row['title'];
$description = $row['description'];
$keywords = $row['keywords'];
$link = $row['link'];
echo "<h3><a href='$link'>$title</a></h3><h4>$link</h4>$description<br /><br />";
}
} else {
echo "<b>No Results Found</b><br><br>Suggestions:<br>
Make sure all words are spelled correctly.<br>
Try different keywords.<br>
Try more general keywords.";
}
//disconnect
mysql_close();
?>
After your foreach ($terms as $each){} you need to add an ORDER BY and LIMIT to the query, like so:
$query .= "ORDER BY keywords LIMIT 0, 10";
That will give you the first 10 results. Alter the LIMIT vars as needed. The first number is the offset (zero based, aka first result is 0, sixth result is 5) and the second number is the number of rows to return.
The ORDER BY helps with consistent results. You don't need it, but sometimes results can be weird.
See this question for more info if you need it.

Categories