| | |
Wild Card search not working...
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Jan 2009
Posts: 151
Reputation:
Solved Threads: 3
Hey, I need to create a search engine that searchs through a database but the wild card doesn't work for me:
Any help would be great. Thanks.
PHP Syntax (Toggle Plain Text)
<?php include ('header.php'); $s = $_POST['search_term']; $by = $_POST['by']; if(!$s && $by) // Check that the details are entered correctly { echo '<p>You have not entered the required fields</p>'; exit(); } // LIKE '%$searchterm%' $query = "SELECT * FROM products WHERE artist LIKE '% $s %'"; $result = mysql_query($query) or trigger_error("Query: $query \n<br />Mysql error: " . mysql_error()); if(mysql_affected_rows() >= 1) { while($row = mysql_fetch_array($result)) { echo '<p>Your search result for ' .$search_term. ''; echo '<blockquote>'; echo '<img src="' .$row['pic']. '">'; echo '<p>' .$row['desc']. '<br />'; echo 'Price: ' .$row['price']. '<br />'; echo '</blockquote>'; } }else{ echo '<p>No artist found, did you spell it right?</p>'; } ?>
Any help would be great. Thanks.
If you ask me questions through Private messaging I won't reply.
1
#2 Oct 26th, 2009
Remove the spaces between the %s and the value.
If the user entered 'Google' then the search query would look like:
This would return values where the products column had an entry of 'something Google something'
If the user entered 'Google' then the search query would look like:
PHP Syntax (Toggle Plain Text)
$query = "SELECT * FROM products WHERE artist LIKE '% Google %'";
AJAX is not a programming language, scripting language or any other sort of language.
It is acheived by using JavaScript http functions.
So, AJAX = JavaScript.
It is acheived by using JavaScript http functions.
So, AJAX = JavaScript.
1
#3 Oct 27th, 2009
Hey.
Also, on line #15 you use the mysql_affected_rows function, where you should be using the mysql_num_rows function.
The mysql_affected_rows function only returns the number or rows affected by INSERT, UPDATE, REPLACE or DELETE queries. SELECT queries have no affect on any rows, and as such, the function doesn't return a valid result for them.
Also, on line #15 you use the mysql_affected_rows function, where you should be using the mysql_num_rows function.
The mysql_affected_rows function only returns the number or rows affected by INSERT, UPDATE, REPLACE or DELETE queries. SELECT queries have no affect on any rows, and as such, the function doesn't return a valid result for them.
Please do not ask for help in a PM. Use the forums.
And use [code] tags!
And use [code] tags!
•
•
Join Date: Sep 2009
Posts: 557
Reputation:
Solved Threads: 64
0
#4 Oct 27th, 2009
if your query is failing try to echo the query and execute it directly in the mysql and post if any error you getting there. or else post the error you echoing in your php
"The discipline of writing something down is the first step towards making it happen."
follow me on twitter
follow me on twitter
1
#6 Oct 27th, 2009
•
•
•
•
Also addslashes(); to your search term somtimes users entering quotes can cause sql to fail.
It should be used on any data that is to be inserted into a MySQL query, especially user-supplied data. (See SQL Injection)
Last edited by Atli; Oct 27th, 2009 at 1:18 pm.
Please do not ask for help in a PM. Use the forums.
And use [code] tags!
And use [code] tags!
0
#7 Oct 28th, 2009
•
•
•
•
The mysql_real_escape_string function would be better. It does more than just add slashes, it escapes any char that would mess up the query.
It should be used on any data that is to be inserted into a MySQL query, especially user-supplied data. (See SQL Injection)
If anyone is interested:
PHP Syntax (Toggle Plain Text)
function make_safe($string) { $string = preg_replace('#<!\[CDATA\[.*?\]\]>#s', '', $string); $string = strip_tags($string); $string = htmlentities($string, ENT_NOQUOTES, 'UTF-8', false); $string = stripslashes($string); $string = mysql_real_escape_string($string); return $string; }
They throw us away like yesterdays jam - Maurice Mossley
Please - rep if someone helps you, it can't be traded for stuff, but it's nice.
Please - rep if someone helps you, it can't be traded for stuff, but it's nice.
![]() |
Similar Threads
- contains forms of inflectional (MS SQL)
- wildcard search (PHP)
- Windows firewall not working properly (Windows Software)
- Wild card (MS Access and FileMaker Pro)
- mysql search and display data help.... (MySQL)
- Probably simple...but...Organize all images collected ... (Windows NT / 2000 / XP)
- F4 key pressed (Java)
- Sound Card isnt working! (PCI and Add-In Cards)
Other Threads in the PHP Forum
- Previous Thread: New to PHP and wanting to go on a course
- Next Thread: Different function for different buttons in php-mysql question
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array beginner binary body broken cakephp checkbox class cms code cron curl database date date/time directory display download dynamic echo email error file files folder form forms function functions google href htaccess html image include insert integration ip java javascript joomla limit link list login loop mail menu mlm mod_rewrite msqli_multi_query multiple mycodeisbad mysql oop parse paypal pdf php problem query radio random recursion regex remote script search send seo server sessions sms soap source space sql static structure syntax system table tutorial update upload url validation validator variable video web webdesign wordpress xml youtube





