BorysSokolov Posted March 23, 2013 Share Posted March 23, 2013 I've made a simple jQuery program which retrieves information from a database based on the value of a textbox. The event is triggered on keyup: $('#textbox').on('keyup', function(){ var category = $(this).val(); $.get('dbRun.php', {category: category}, function(data){ $('#output').html(data); }); }); ...and here's the file that retrieves the data: <?php $linkDB = mysqli_connect('localhost', 'user', '', 'ajax_db'); $queryCategory = mysqli_query($linkDB, "SELECT content FROM site_contents WHERE category = '".$_GET['category']."'"); echo mysqli_fetch_row($queryCategory)[0]; ?> Unfortunately, it runs pretty slow; I'm guessing it's because every time the event is triggered the database has to re-connect, right? Then how could I run the database connect once, at the start of the program, and make the connection available to the ajax file? Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/276059-improve-database-speed-in-simple-program/ Share on other sites More sharing options...
Jessica Posted March 23, 2013 Share Posted March 23, 2013 First thing id do is make sure There is an index on the column. Sounds like its text, not the primary key. Quote Link to comment https://forums.phpfreaks.com/topic/276059-improve-database-speed-in-simple-program/#findComment-1420554 Share on other sites More sharing options...
Jessica Posted March 23, 2013 Share Posted March 23, 2013 Also if you only want one result limit the query to 1. Quote Link to comment https://forums.phpfreaks.com/topic/276059-improve-database-speed-in-simple-program/#findComment-1420555 Share on other sites More sharing options...
BorysSokolov Posted March 26, 2013 Author Share Posted March 26, 2013 Also if you only want one result limit the query to 1. I've tightened up the code a little, like you suggested, and uploaded the program with free hosting, which seems to have eliminated the delay before the message is posted (I'm guessing their database is faster); but that still leaves me with one minor issue: how do I trigger the event to update the chat window whenever a new message is submitted? Currently I'm using intervals, but it seems really inefficient; is there a batter way? I've thought about fetching all the messages at the start, and then just posting each new message directly to the chat window, so whenever the scroll height of the window would change, I could post the new message into the database right after, but I still haven't attempted it. Would that be a good technique? Quote Link to comment https://forums.phpfreaks.com/topic/276059-improve-database-speed-in-simple-program/#findComment-1421054 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.