4 Replies - 321 Views - Last Post: 11 July 2013 - 11:07 AM Rate Topic: -----

#1 skymonkier  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 107
  • Joined: 14-July 12

Using an Array for a Parameter?

Posted 11 July 2013 - 09:41 AM

I'm working on my own WebRPG project, and I'm borrowing ideas and constructs from another guy who made one and made it open source. I'm going back and writing my own code from scratch, but just borrowing his methods.

I just finished fleshing out the very beginning, and I made a query-doing function.
Before I commit and let it sit until I have the chance to test, would putting an Array() in the place of $ValueArray here work?
function DoQuery($QueryString, $ValueArray) {
	global $QueryCount;
		
	$NewQuery = $DBHandle->prepare($QueryString);
	$NewQuery->execute(array($ValueArray));
		
	$QueryCount++;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Using an Array for a Parameter?

#2 Kruithne  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 99
  • View blog
  • Posts: 442
  • Joined: 28-July 09

Re: Using an Array for a Parameter?

Posted 11 July 2013 - 10:03 AM

Sure you can! Here's a quick example for you below to show you how it could be done. I'm not a major fan of passing SQL queries about as strings, rather just having them static inside a query function.
function doQuery()
{
	$query = $DBHandler->prepare('UPDATE someTable SET someValue = :thing AND someOtherValue = :fling');
	$query->execute(
		array(
			':thing' => 'Fish and chips!',
			':fling' => 'Taters!'
		)
	);

	global $queryCount;	
	$queryCount++;
}

That example shows how it all works, an array going straight into the execution function. If you wanted the values to come from outside the function like you said, you could pass them in like the next example shows.
//Function
function doQuery($values)
{
	$query = $DBHandler->prepare('UPDATE someTable SET someValue = :thing AND someOtherValue = :fling');
	$query->execute($values);

	global $queryCount;	
	$queryCount++;
}

// Call to the function
doQuery(
	array(
		':thing' => 'Fish and chips!',
		':fling' => 'Taters!'
	)
);

One thing I would note is not passing query parameters through so raw in an array like that. If you wanted to stay dynamic you could jiggle it about like so to automatically get the names and so forth.
// Function
function doQuery($values)
{
	$query = $DBHandler->prepare('UPDATE someTable SET someValue = :thing AND someOtherValue = :fling');
	
	foreach ($values as $key => $value)
		$query->bindValue(':' . $key, $value);
	
	$query->execute();

	global $queryCount;	
	$queryCount++;
}
 
// Call to function
doQuery(
	array(
		'thing' => 'Fish and chips!',
		'fling' => 'Taters!'
	)
);

Obviously I can see that you'd probably want to pass the query string through, but I'd so it other ways, so play about with the examples and take a look at PDO in PHP for more information on the functions and such for queries.

http://php.net/manua...ent.execute.php
Was This Post Helpful? 1
  • +
  • -

#3 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3485
  • View blog
  • Posts: 10,049
  • Joined: 08-June 10

Re: Using an Array for a Parameter?

Posted 11 July 2013 - 10:36 AM

note, esp. when using objects, global is a no-go. there are object properties for a reason (or you could pass parameters by reference).
Was This Post Helpful? 0
  • +
  • -

#4 Kruithne  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 99
  • View blog
  • Posts: 442
  • Joined: 28-July 09

Re: Using an Array for a Parameter?

Posted 11 July 2013 - 11:05 AM

Indeed! If you are wanting to track how many queries you are using you could always just implement a static counter that you call every time you execute a query, keeping the statistic programming away from your queries.
class StatisticTracker
{
	public static function TrackQuery()
	{
		self::$queryCount++;
	}
	private static $queryCount;
}

// Call the tracker and add a count
StatisticTracker::TrackQuery();

This post has been edited by Kruithne: 11 July 2013 - 11:05 AM

Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2891
  • View blog
  • Posts: 10,025
  • Joined: 08-August 08

Re: Using an Array for a Parameter?

Posted 11 July 2013 - 11:07 AM

You could also return the value or if $queryCount is a property of an object and that function is a method then that should work. You need a very good reason to use a global. I've done it when manipulating very large arrays because making them local would use up the stack. Other than that I can't think of a good reason.

Oops! The large arrays were in another language. I can't think of a good reason to use them in PHP.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1