Page 1 of 1

SOLVED How To: Loop DB Calls

Posted: Mon Nov 05, 2012 11:02 am
by Goofan
Hi,
I am looking for a way to combine multiple DB calls to one.

My current code is currently taking up space and will take up more if I continue to make one new "Statement" for every call.

CODE:

Code: Select all

$mydb = "SELECT * FROM economy";
$res = $db->dbquery($mydb);
while($array = $db->fetchArray($res)){
					
        //Economi Table
        //$id = mysql_real_escape_string($array['id']);
	$money = mysql_real_escape_string($array['money']);
	$income = mysql_real_escape_string($array['income']);
}
				
$mydb = "SELECT * FROM research, my_research WHERE id = $id";
$res = $db->dbquery($mydb);
while($array = $db->fetchArray($res)){
					
	//My_Reasearch Table
	$bet = mysql_real_escape_string($array['bet']);
	$completed .= mysql_real_escape_string($array['completed']);
	$lvl = mysql_real_escape_string($array['lvl']);
	$tC = mysql_real_escape_string($array['timeComplete']);
	$dC = mysql_real_escape_string($array['dateComplete']);
					
	//Research Table
	$ptf = mysql_real_escape_string($array['pointstofinish']);
}
My biggest problem is that my different tables got multiple data-rows of the same identifier.

The solution I have been trying to find is:
Have it all in one call. With no more than 40 rows no matter how many calls I got.
Have it all depending on the $mydb

Regards,
Thomas

Re: How To: Loop DB Calls

Posted: Mon Nov 05, 2012 2:42 pm
by requinix
Of all those variables, which you're incorrectly mysql_real_escape_string()ing by the way, which ones do you actually want? And if that one $id assignment is commented out then where is its value coming from?
Whatever your answer, use a JOIN. I'd give an example but it depends on what you want to do.

Re: How To: Loop DB Calls

Posted: Mon Nov 05, 2012 4:34 pm
by Goofan
Hi :)

Sorry, but I messed about with the script alot since I posted this. Was going to remove the thread.




P.S. What do you mean with I am using it incorrectly? How should I use it? D.S.

Regards,
Thomas

Re: How To: Loop DB Calls

Posted: Mon Nov 05, 2012 8:40 pm
by requinix
You only need it when you (1) have a string and you don't know that it's safe for SQL, (2) will be putting it into a SQL query, and (3) are a line or two of code away from doing that. You don't need it when getting stuff out of the database. You don't need it for numbers (as long as you know they're definitely numbers). You don't need it for HTML.

Re: How To: Loop DB Calls

Posted: Tue Nov 06, 2012 6:33 am
by Goofan
Ok Got it!

Thank you very much :)

Regards,
Thomas