Jump to content

This Query Not Working


Net123
 Share

Recommended Posts

hello i am making ajax php mysql live search script everything working fine by ajax but my problem is only about query and output correct result it was not working perfectlyit was showing results but .............

$searchq = $_GET['searchq']; $result = mysql_query("SELECT * FROM files WHERE name LIKE '%" . $searchq . "%' LIMIT 2");if (!$result) {    echo 'Could not run query: ' . mysql_error();    exit;}while($row = mysql_fetch_array($result)) {echo $row['name'];}mysql_free_result($result);  you can know from this query

Link to comment
Share on other sites

it was not showing up the exact mach likeif i searched for 'w3schools' it was showing "quackit" and under what to do for this ??

Edited by Net123
Link to comment
Share on other sites

That doesn't make sense based on the code. This query: SELECT * FROM files WHERE name LIKE '%w3schools%' LIMIT 2 Would not return a record where the name is set to "quackit". It doesn't match the condition. Either way though, you need to escape the value going into the query: $result = mysql_query("SELECT * FROM files WHERE name LIKE '%" . mysql_real_escape_string($searchq) . "%' LIMIT 2");

Link to comment
Share on other sites

oh god error in this please help me what was error in thisAJAX

/* ---------------------------- *//* XMLHTTPRequest Enable *//* ---------------------------- */function createObject() {var request_type;var browser = navigator.appName;if(browser == "Microsoft Internet Explorer"){request_type = new ActiveXObject("Microsoft.XMLHTTP");} else {request_type = new XMLHttpRequest();}return request_type;}var http = createObject();/* -------------------------- *//* SEARCH *//* -------------------------- */function searchNameq() {searchq = encodeURI(document.getElementById('searchq').value);document.getElementById('msg').style.display = "block";document.getElementById('msg').innerHTML = "Searching for <strong>" + searchq+"";// Set te random number to add to URL requestnocache = Math.random();http.open('get', 'searchfast.php?name='+searchq+'&nocache = '+nocache);http.onreadystatechange = searchNameqReply;http.send(null);}function searchNameqReply() {if(http.readyState == 4){var response = http.responseText;document.getElementById('search-result').innerHTML = response;}}

Link to comment
Share on other sites

Well, the code to create the XHR object isn't that great. There's a better example here: http://www.w3schools...uest_create.asp You should also use encodeURIComponent instead of encodeURI when you're encoding a value for the querystring. You also have an unclosed <strong> tag, and there are spaces around the equal sign for the nocache variable in the URL that shouldn't be there. The readystate handler isn't checking the response code either, so that may display an error message from the server if the request failed. Other than that, you should be using the Javascript debugging tools in your browser to look for error messages from Javascript.

Link to comment
Share on other sites

just add my site nameif i tried directly to my php pagehtpp://........................//in-search.php?searchq=Snehait was showing perfectly no any errors in mysql or php contents the problems are @ the ajax..........

Edited by Net123
Link to comment
Share on other sites

You're showing different scripts it seems. Maybe you forgot to sync them yourself.If I type "q" as the query, I get request to the URL

/in-search.php?name=q&nocache=0.9572214348696474

Notice that the query is in "name". And your PHP script instead uses

$_GET['searchq']

which means that the SQL statement ends up ALWAYS returning the first two entries.Change

http.open('get', 'in-search.php?name='+searchq+'&nocache = '+nocache);

(which is different from what you're showing above BTW... I took this here from the link you gave me)to

http.open('get', 'in-search.php?searchq='+searchq+'&nocache = '+nocache);

To avoid similar problems, I highly suggest you open up your browser's development tool (as specified in point 3.2. at this topic), and monitor the URLs (and the shown headers and body) that pops up as you search.

  • Like 1
Link to comment
Share on other sites

oh god i thank you very very much i tryied to fix this for past three day thank you very much..........

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...