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
Share on other sites

[code]SELECT * FROM articles WHERE body LIKE '%ubuntu%'[/code]

There is another query being ran. But in the code it's chopped up in an odd way i dont understand

[code]$query = "SELECT * FROM articles " .

"WHERE body LIKE '%".$keywords['0']."%'";[/code]
Link to comment
Share on other sites

right now it only searches for the 'body' row, i'd like it to search the body and title row, and return the article if either shows a match. i've tried adding it, but due to the php format, and it being chopped up like that, i can't get the syntax correct
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.