Jump to content

Archived

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

Dville

MySQL searching

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.

Share this post


Link to post
Share on other sites
Post the final value of the $query variable.

Share this post


Link to post
Share on other sites
Not sure what you're asking for, but here is all of my search.php page

http://phpfi.com/141089

Share this post


Link to post
Share on other sites
I don't want your search page... I want you to echo the value of the query variable.

Share this post


Link to post
Share on other sites
says

[code]SELECT COUNT(id) AS numrows FROM articles WHERE body LIKE '%ubuntu%'[/code]

Share this post


Link to post
Share on other sites
Well, then for some reason, your keywords array is empty, becuase you're never concatenating the other fields.

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
Well, yes, there are two queries, but again, it'll make more sense if you see the final SQL query, not it's PHP equivalent.

Share this post


Link to post
Share on other sites
Then your keywords array is empty.

Share this post


Link to post
Share on other sites
I dunno what that means. wouldnt the keyword be 'ubuntu', which i see is in there

Share this post


Link to post
Share on other sites
Then I'm totally confused as to what you're having trouble with.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
Right -- all I'm saying is that the following code is never being executed.
[code]
for ($i=1; $i<count($keywords); $i++) {

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

}
[/code]

Share this post


Link to post
Share on other sites

×

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.