RE: [PHP] Struggling with MySQL query
| From: | admin at buskirkgraphics dot com | Date: | Tue, 09 Aug 2011 14:26:40 +0000 |
| Subject: | RE: [PHP] Struggling with MySQL query | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-314446@lists.php.net to get a copy of this message | ||
> -----Original Message-----
> From: David Green [mailto:simpill@gmail.com]
> Sent: Tuesday, August 09, 2011 10:14 AM
> To: php-general@lists.php.net
> Subject: [PHP] Struggling with MySQL query
>
> Hi
>
> I have a simple from which uses the post method to get to my page
> script
> results.php
>
> in results.php I have
>
> $find=$_POST["find"]; //this works perfectly, echo $find gives me the
> search
> term as entered
>
> I then connect to mysql and select the db successfully.
>
> After that, I have the following:
>
> $find = strtoupper($find);
> $find = strip_tags($find);
> $find = trim($find);
>
> $data = mysql_query("SELECT * FROM news_items WHERE upper('headline')
> LIKE
> '%$find%'");
>
> while($result = mysql_fetch_array($data)) { //etc
>
> I get no error messages, but no results to work with either. It
> returns a
> "no results" message that I put in further on in the script. A casual
> look
> at the db shows that I should be getting results. I'm pretty sure that
> the
> problem is in the query, but for the life of me I can't see the
> problem.
>
> Kind regards
> David
Suggestion:
$query = "SELECT * FROM news_items WHERE UPPER(headline) LIKE
'%".mysql_real_escape_string($find)."%' ";
$result = mysql_query($query);
//Check your syntax display any errors.
Echo mysql_error();
Echo mysql_errno();
If(mysql_num_rows($result)>= 1) {
While($row = mysql_fetch_assoc($result))
{
print_r($row);
}
}else{
Echo 'nothing to show';
}