Jump to content

Recommended Posts

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.

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.