Jump to content

MySQL searching


Dville

Recommended Posts

Here is my code

[code]// counting the offset

$offset = ($pageNum - 1) * $rowsPerPage;



$keywords = explode(" ", $search);



$query = "SELECT * FROM articles " .

"WHERE body LIKE '%".$keywords['0']."%'";



for ($i=1; $i<count($keywords); $i++) {

$query = $query." AND body LIKE '%".$keywords[$i]."%'";

}

$query = $query." ORDER BY id DESC LIMIT $offset, $rowsPerPage";



$result2 = mysql_query($query) or die(mysql_error());

?>



<form method="GET" action="search.php">

<b>Search:</b> <input type="text" name="search" size="20" />

<input type="submit" value="Search!" />

</form><br><br>

<table width="50%" style="border:1px solid #000000;">

<?php

$keywords = explode(" ", $search);



$query = "SELECT COUNT(id) AS numrows FROM articles " .

"WHERE body LIKE '%".$keywords['0']."%'";



for ($i=1; $i<count($keywords); $i++) {

$query = $query." AND body LIKE '%".$keywords[$i]."%'";

}



$result = mysql_query($query) or die('Error, query failed');

$row = mysql_fetch_array($result, MYSQL_ASSOC);

$numrows = $row['numrows'];



// how many pages we have when using paging?

$maxPage = ceil($numrows/$rowsPerPage);[/code]


But I would also like to search both 'body' and 'title'. Due to syntax issues, and not knowing which exact line to edit, I have failed at trying to get it to search both rows.

Thanks in advanced to anyone who can help me with this.
Link to comment
https://forums.phpfreaks.com/topic/17589-mysql-searching/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.