0

I have just looked at a YouTube video on how to create a custom database. I have tried to implement this into my site. However, obviously, it isn't working...

I get an error 'Query failed: ' and nothing more from the following code.

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect to database"); 
mysql_select_db("$db_name")or die("cannot select DB");
$con=mysqli_connect("$host","$username","$password","$db_name");

$terms = explode(" ", $search);
$query = "SELECT * FROM search WHERE ";

foreach ($terms as $each){
$i++;
if ($i == 1)
  $query .= "keywords LIKE '%each%' ";
else
   $query .= "OR keywords LIKE '%each%' ";
}

$query = mysql_query($query);
$numrows = mysql_num_rows($query) or die('Query failed: ' . mysql_error() . "<br   />\n$sql"); 

if ($numrows > 0)
   while($row = mysqlfetch_ASSOC($query)){
   $id = $row['id'];
   $title = $row['title'];
   $description= $row['description'];
   $keywords = $row['keywords'];
   $link = $row['link'];

   echo '<h2><a href="$link">$title</a><h2>
   $description<br/><br/>';
   }
else 

   echo "No results found";

   //disconnect
   mysql_close();

?>

To be honest, I only understand about 70% of that and have no idea. The database is connected properly and functions. WHAT AM I DOING WRONG???

mysql_num_rows was causing problems before but after putting a space inbetween WHERE'' and the closing quotation mark, it left me here.

Josh-Mason
  • 319
  • 3
  • 8
  • 22
  • what happens if you move the WHERE, infront of 'keywords like'....does your query return everything? – Kylie Jun 08 '13 at 03:27
  • Move your `or die(... mysql_error()...)` to the query `$query = mysql_query($query) or die('Query failed: ' . mysql_error() . "
    \n$sql");` This is where you will get the error message that you need. Where are you declaring `$search`?
    – Sean Jun 08 '13 at 03:34
  • So, I changed $search to $my search which is what it was supposed to be. $mysearch is declared in a global file and simply gets the input from the search box. I may be getting somewhere as now, when i type in a keyword in the database, it gives '$title' and '$description' as plain text and not their variable counter-parts – Josh-Mason Jun 08 '13 at 13:33
  • GREAT! I done what you said (move the or die thingy) and it works. I just need to be able to display the variable as an actual variable and not $title or $description in plain text. I can't do echo $title; as I need to give it a href value which is a variable also – Josh-Mason Jun 08 '13 at 13:36
  • Do you think it would be wirth posting that in another question as I've had a look online and none of the methods I've come across work – Josh-Mason Jun 08 '13 at 13:36
  • GOT IT!!! echo '

    ' . $description . '
    ';
    – Josh-Mason Jun 08 '13 at 13:43

2 Answers2

1

I'm not sure that this is the problem, but, shouldn't this:

$query .= "keywords LIKE '%each%' ";

be this instead:

$query .= "keywords LIKE '%{$each}%' ";

It is treating each as text and not as the string in the loop.

(same goes two lines below)

mignz
  • 3,648
  • 2
  • 20
  • 21
0

First off....you need to include the $each to include the variable...not just each...

$query .= "WHERE keywords LIKE '%{$each}%' ";

Also....Move the WHERE infront of keywords, so that if a user enters nothing into the search...they will query everything...otherwise if they enter nothing, it will break...

 $terms = explode(" ", $search);
 $query = "SELECT * FROM search ";

  if(!empty($terms)){

  foreach ($terms as $each){
   $i++;
     if ($i == 1)
        $query .= "WHERE keywords LIKE '%{$each}%' ";
      else
        $query .= "OR keywords LIKE '%{$each}%' ";
      }
  }
Kylie
  • 11,421
  • 11
  • 47
  • 78