Help - Search - Members - Calendar
Full Version: [php/mysql] Search Script
Zymic Webmaster Forums > Web Design & Development > Server Side Scripting > PHP
Madz0r

Hai, so I've made this little script that searches through my database of 'articles'
and returns based on if the search string matches words in the title of the article.

It does work, but I was wondering if any of you could spot anything that I should have
done differently. In terms of effeciency, easier to code or whatnot. My current database is not
big, but I might aswell learn how to be efficient right away.


Here is my code:
CODE
<?php
// SET NUMBER OF RESULTS
$numresults = 0;

// IF ACCESSED BY THE RAW FILE SEND THEM TO INDEX

if(!isset($_POST["search_submit"])) {
header("Location: index.php");
die();
} else {
// IF THE BOX DOES NOT CONTAIN ANYTHING
if(empty($_POST["search_string"])) {
echo 'Please enter something to search for.';
} else {
$searchsql = "SELECT * FROM blog_articles";
$searchresults = mysql_query($searchsql,$mysql_connect);
while($searchrow = mysql_fetch_array($searchresults)) {
// WHAT TO SEARCH FOR
$searchresult = stripos($searchrow["blog_title"],$_POST["search_string"]);
// CHECK IF THE STRING IS FOUND IN TITLES
if($searchresult !== false) {
// STRING WAS FOUND, ECHO LINK
echo '<h1><a href="index.php?p=article&article=';
echo $searchrow["blog_article_id"];
echo '">';
echo $searchrow["blog_title"];
echo '</a></h1>';
// ADD TO NUMBER OF RESULTS
$numresults = $numresults + 1;
}
}
// ECHO THE AMOUNT OF RESULTS
echo $numresults . ' posts found.';
}
}
mysql_close();

?>


Thanks in advance,
/Madz0r
swordz
There's a huge saving to be had, if you do the searching in the MySQL query.

Try:
CODE
$searchsql = "SELECT * FROM `blog_articles` WHERE `blog_title` LIKE '%" . mysql_real_escape_string($_POST["search_string"]) . "%'";


Someone check this query?

swordz
Alex
Comments on that optimisation, and the rest of the script
  • is "SELECT *" necessary, if you don't need every column don't pull them all
  • you may want to split the input by spaces and generate a series of OR LIKE "%term%" combinations
  • you don't need a new echo for each variable you output, you can stack the echo (e.g. echo 'text' , $var , 'text';) or concatenate (same with . instead of ,). Stacking is more efficient. I see you did concatenate for some though, so is that intentional? Weird formatting to make intentionally.
  • There's a shorthand for increments, you can do ++$numresults; or $numresults++; (the two return different values, but in a statement on their own they are in essence identical in PHP - I just use the former because it's more efficient in C++ and I've taken the habit across tongue.gif)
  • Interesting indentation for this kind of language, is that a derivative of pretty-printing from LISP-style languages? You might find things end up /very/ far indented doing it that way, I'd consider fixed length indentation levels for this kind of language.
Madz0r
QUOTE(swordz @ Jan 21 2009, 11:44 PM) *
There's a huge saving to be had, if you do the searching in the MySQL query.

Try:
CODE
$searchsql = "SELECT * FROM `blog_articles` WHERE `blog_title` LIKE '%" . mysql_real_escape_string($_POST["search_string"]) . "%'";


Someone check this query?

swordz


It seems to work, swordz.
Thanks alot smile.gif

QUOTE(Alex @ Jan 22 2009, 12:00 AM) *
Comments on that optimisation, and the rest of the script
  • is "SELECT *" necessary, if you don't need every column don't pull them all
  • you may want to split the input by spaces and generate a series of OR LIKE "%term%" combinations
  • you don't need a new echo for each variable you output, you can stack the echo (e.g. echo 'text' , $var , 'text';) or concatenate (same with . instead of ,). Stacking is more efficient. I see you did concatenate for some though, so is that intentional? Weird formatting to make intentionally.
  • There's a shorthand for increments, you can do ++$numresults; or $numresults++; (the two return different values, but in a statement on their own they are in essence identical in PHP - I just use the former because it's more efficient in C++ and I've taken the habit across tongue.gif)
  • Interesting indentation for this kind of language, is that a derivative of pretty-printing from LISP-style languages? You might find things end up /very/ far indented doing it that way, I'd consider fixed length indentation levels for this kind of language.


As for the concetenation and indenation, it is just for readability. I realize I could have put the whole echo block in to one single echo, but I would have a alot of problems reading it .

Swordz's query helped me on the SELECT * subject, as I was coding this I kinda knew it wasn't the right way to do it.


Thanks for the help guys, I'll try it some more when I get back from school.


/Madz0r
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2012 Invision Power Services, Inc.