/    Sign up×
Community /Pin to ProfileBookmark

Auto complete

Hi, I have a auto complete script. I want to display the data in select box and also it takes some time to fetch the value from database. How do we make it faster.

<html>
<head>
<title></title>
<script type=”text/javascript” src=”javascripts/prototype.js”></script>
<script type=”text/javascript” src=”javascripts/effects.js”></script>
<script type=”text/javascript” src=”javascripts/controls.js”></script>

[B]auto.php[/B]

[code=php]</head>
<body>
<table>
<form method=”post” >
<tr >
<td> <input type=”text” name=”pname” id=”pname”>
<div id=”hint”></div>
</td>
</tr>
</table>
</body><script type=”text/javascript”>
new Ajax.Autocompleter(“pname”,”hint”,”server.php”);
</script>

</html>[/code]

[B]server.php[/B]

[code=php]<?php
include “config.php”;

$sql = “SELECT distinct title,id FROM jos_list_categories WHERE title LIKE ‘$_POST[pname]%'”;

$rs = mysql_query($sql) or die(mysql_error());

?>

<ul>

<? while($data = mysql_fetch_assoc($rs)) { ?>
<li style=”overflow-y:auto;overflow-x:none;border:1px medium #666666;”><? echo stripslashes($data[‘title’]);?></li>
<? } ?>

</ul>[/code]

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@Phill_PaffordApr 08.2009 — For one I hope that the $_POST variable is not user input, See SQL Injection.

A couple of questions:

1: is the database table indexed?

2: in the query are you using the index?

3: how large is the table in the database? The larger the table the slower the query.

4: is the database local or remote?

[CODE]
SELECT distinct title,id
FROM jos_list_categories

WHERE title LIKE '$_POST[pname]%'
[/CODE]


You could use the GROUP BY instead of the DISTINCT as GROUP BY will order the results

[CODE]
SELECT title,id
FROM jos_list_categories

WHERE title LIKE '$_POST[pname]%'
GROUP BY title
[/CODE]


I don't think there would be a performance increase but the results should be ordered without an ORDER BY clause
×

Success!

Help @kavionly spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...