Mysqli multi_query Search of 2 Tables Not showing Results - php

I'm up to my neck trying to figure out why my query isn't working. This is what my search.php page results in. I am able to _GET the search term perfectly but can't display the results.
Not sure if the issue is the fetch_array_assoc or what! Here's my code. Any help with this would be appreciated. Not 100% sure if my syntax is correct.
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$button = $_GET ['submit'];
$search = $_GET ['query'];
if (strlen($search) <= 1) {
echo "Search term too short";
}
else {
echo "You searched for <b>$search</b> <hr size='1'></br>";
$con = new mysqli("localhost", "user", "pass", "db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$search_exploded = explode(" ", $search);
foreach ($search_exploded as $search_each) {
$x++;
if ($x == 1) {
$query = "Keyword_ID LIKE '%$search_each%' or Keyword_Name LIKE '%$search_each%' ";
}
else {
$query .= "OR Keyword_ID LIKE '%$search_each%' or Keyword_Name LIKE '%$search_each%' ";
}
}
$construct = mysqli_query($con, "SELECT * FROM profileTable WHERE $query");
$construct = mysqli_query($con, "SELECT * FROM addKeywordTable (Keyword_Name) WHERE $query");
$constructs = mysqli_multi_query($construct);
if (mysqli_multi_query($construct)) {
$numrows = mysqli_num_rows($query);
if ($numrows > 0) {
while ($row = mysqli_fetch_assoc($constructs)) {
$key = $row['Keyword_Name'];
$keyID = $row['keyID'];
$fname = $row['FirName'];
$lname = $row['LaName'];
$mname = $row['MName'];
$suffix = $row['Suffix'];
$title = $row['Title'];
$dept = $row['Dept'];
$phone1 = $row['PH1'];
$phone2 = $row['PH2'];
$email = $row['Email'];
$photo = $row['Photo'];
$bio = $row['BioLK'];
$tags = $row['Tags'];
echo '<h2>$fname $lname</h2>';
echo $key;
echo $title;
echo $dept;
}
}
else {
echo "Results found: \"<b>$x</b>\"";
}
}
}
mysqli_close();
?>
I am trying to search two different tables. addKeywordTable and profileTable. Profile table has all of the profile info for a user. The addKeywordTable stores the keywords/tag names 'Keyword_Name'.
I attempted to create a mysqli_multi_query but its not working at all.

I assuming:
$con is set by
$con = mysqli_connect("host", "user", "password", "db");
mysqli_multi_query : you must all sql commands, except the last, terminate with ;
and concenat $construct with .= . Otherwise you overwrite your $construct.
$construct = "SELECT * FROM profileTable WHERE $query ;");
$construct .= "SELECT * FROM addKeywordTable (Keyword_Name) WHERE $query");
don't set $construct with
$construct = mysqli_query($con, "SELECT * FROM profileTable WHERE $query");
your $construct will only become TRUE or FALSE .
with a variable wich contents TRUE or FALSEyou can not call
$constructs = mysqli_multi_query($con,TRUE);
And you call it wrong
$constructs = mysqli_multi_query($construct);
correct
$constructs = mysqli_multi_query($con,$construct);
You call mysqli_multi_query($construct) twice
$constructs = mysqli_multi_query($construct);
if (mysqli_multi_query($construct)) { ...
the first call is not necessary.
call it only with
if (mysqli_multi_query($con,$construct)) { ...
complete wrong is
if (mysqli_multi_query($construct)) {
$numrows = mysqli_num_rows($query);
if ($numrows > 0) {
while ($row = mysqli_fetch_assoc($constructs)) {
$query is at the moment a simple 'string'
$query = "Keyword_ID LIKE '%$search_each%' or Keyword_Name LIKE '%$search_each%' ";
Also wrong
while ($row = mysqli_fetch_assoc($constructs)) {
To retrieve the resultset from the first query you can use mysqli_use_result() or mysqli_store_result(). All subsequent query results can be processed using mysqli_more_results() and mysqli_next_result().
Call it like this instead
if (mysqli_multi_query($con,$construct)) {
if ($result = mysqli_store_result($con)) {
while ($row = mysqli_fetch_row($result)) {
printf("%s\n", $row[0]);
}
mysqli_free_result($result);
Set $x before you do $x++ .
$x = 0;
You can't be sure that $x is always automatically set to 0 .

Related

PHP Search Box not working?

I have created a search box that searches my website. I have tried really hard to fix it but it didn't work. Please could you help? The code is below:
<?php
$k = $_GET['k'];
$terms = explode(" ", $k);
$query = "SELECT * FROM search WHERE "
for each ($terms as $each) {
$i++;
if ($i == 1)
$query .= "keywords LIKE '%$each%' ";
else
$query .= "OR keywords LIKE '%$each%' ";
}
//connect
mysql_connect("localhost", "my username", "my password");
mysql_select_db("my database");
$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 "<h1><a href='$link'>$title</a></h2>";
$description<br /><br />""
}
}
else
echo "No results found for \"<b>$k/b>\" ";
//disconnect
mysql_close();
?>
first of all, correct syntax. Instead of:
$query = "SELECT * FROM search WHERE "
for each ($terms as $each) {
should be
$query = "SELECT * FROM search WHERE ";
foreach ($terms as $each) {
and OUH, what's this???
$description<br /><br />""
maybe youd learn the basics of syntax at first.

How to retrieve MySQL database data using a php search bar

I have created a search bar using PHP and I want to retrieve the data I have in my MySQL database.
My code is as follows:
<?php
$button = $_GET ['submit'];
$search = $_GET ['search'];
if(!$button)
echo "You searched for '<b>$search</b>' <hr size='1'</br>";
$con=mysqli_connect("localhost", "root", "root", "PM_DB");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$search_exploded = explode (" ", $search);
foreach($search_exploded as $search_each)
{
$x++;
if($x==1)
$construct .="keywords LIKE '%$search_each%'";
else
$construct .="AND keywords LIKE '%$search_each%'";
}
$construct ="SELECT * FROM Leads WHERE $construct";
$run = mysqli_query($construct);
$foundnum = mysqli_num_rows($run);
if ($foundnum==0)
echo "There are no results for <b>'$search'</b>. Please check your spelling.";
else
{
echo "$foundnum results found !<p>";
while($runrows = mysqli_fetch_assoc($run))
{
$Company = $runrows['Clients'];
echo "<a href='$Company'><b>Company</b></a><br>";
}
}
?>
Every time I click search it only returns the error message. What am I missing out? Any suggestions will be highly appreciated! Thanks - Tijger.
Try this code
$search_exploded = explode(" ", $search);
$construct = "SELECT * FROM Leads WHERE keywords LIKE ";
$construct .= "'%".implode("%' OR '%", $search_exploded)."%'";
$run = mysqli_query($construct);
Live demo
As I requested the code from OP
I commented the two lines which were causing error for him
He was executing the right query and then the wrong query again ... that is why getting no results returned.
<?php
$button = $_GET ['submit'];
$search = $_GET ['search'];
if (!$button)
echo "You searched for '<b>$Search</b>' <hr size='1'</br>";
$con = mysqli_connect("localhost", "root", "root", "PM_DB");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$search_exploded = explode(" ", $search);
$construct = "SELECT * FROM Leads WHERE keywords LIKE ";
$construct .= "'%" . implode("%' OR '%", $search_exploded) . "%'";
$run = mysqli_query($construct) or die(mysql_error());
// Why thisssssssssssssssssss??????????? OMG
//$construct = "SELECT * FROM Leads WHERE $construct";
//$run = mysqli_query($construct);
$foundnum = mysqli_num_rows($run) or die(mysql_error());
if ($foundnum == 0)
echo "There are no results for <b>'$search'</b>. Please check your spelling.";
else {
echo "$foundnum results found !<p>";
while ($runrows = mysqli_fetch_assoc($run)) {
$Company = $runrows['Clients'];
echo "<a href='$Company'><b>Company</b></a><br>";
}
}
?>

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.

Can I have files automatically be added to my MySQL database?

I have a custom search function on my site, using PHP. I don't like having to go to my database every time I make a page, so can I write something to automatically add a new page to my database? Here's my code:
<?php
$k = $_GET['k'];
$terms = explode(" ", $k);
$query = "SELECT * FROM search WHERE ";
foreach ($terms as $each){
$i++;
if ($i == 1)
$query .= "keywords LIKE '%$each%' ";
else
$query .= "OR keywords LIKE '%$each%' ";
}
//connect
mysql_connect("*****", "*****", "*****");
mysql_select_db("*****");
$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>
<hr />";
}
}
else
echo "No Results Found for \"<b>$k</b>\". Try Again.";
//disconnect
mysql_close();
?>

Categories