SOLVED How To: Loop DB Calls

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

SOLVED How To: Loop DB Calls

Post 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
Last edited by Goofan on Tue Nov 06, 2012 6:56 am, edited 1 time in total.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How To: Loop DB Calls

Post 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.
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

Re: How To: Loop DB Calls

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How To: Loop DB Calls

Post 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.
User avatar
Goofan
Forum Contributor
Posts: 305
Joined: Wed Nov 04, 2009 2:11 pm
Location: Sweden

Re: How To: Loop DB Calls

Post by Goofan »

Ok Got it!

Thank you very much :)

Regards,
Thomas
Post Reply