Jump to content

Select function


Shazer2

Recommended Posts

I am designing my own framework, and I have created a select function. I have set a parameter to be an array by default, with the default value of *. When I check if it's an array (with is_array) it says that it isn't. Does anyone have any idea why it would say that?

function select($table, $rows = array('*'), $cond = "") {
	$query = "SELECT {$rows} FROM `{$table}`";

	if (isset($cond)) {
		$query .= " WHERE {$cond}";
	}
	if (is_array($rows)) {
		if (isset($rows) && $rows != "*") {
			foreach ($rows as $v) {
				print_r($v);
			}
		}
	}


	//return mysql_query($query);
}

 

Any help is appreciated.

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/253200-select-function/
Share on other sites

I just added some quick debugging (see comments in the code), and it seems fine to me.

 

function select($table, $rows = array('*'), $cond = "") {
$query = "SELECT {$rows} FROM `{$table}`";
if (isset($cond)) {
	$query .= " WHERE {$cond}";
}
if (is_array($rows)) {
	return "is array"; // <<<---- HERE
	if (isset($rows) && $rows != "*") {
		foreach ($rows as $v) {
			print_r($v);
		}
	}
} else { 
	return "not array"; // <<<--- AND HERE
}
}
echo select(); // returns "is array"

Link to comment
https://forums.phpfreaks.com/topic/253200-select-function/#findComment-1298023
Share on other sites

Well of course it won't be an array when you explicitly pass a string; any value you pass to the function will override the default value. You'd need to pass the argument as an array.

 

echo select("boot", array("id"), "id=1");

 

You're also going to need to modify the function to implode that array with commas to make it usable in the query string.

Link to comment
https://forums.phpfreaks.com/topic/253200-select-function/#findComment-1298025
Share on other sites

 

Just as a quick side note about your code:

	if (isset($cond)) {
		$query .= " WHERE {$cond}";
	}

 

isset() is probably not what you want.  isset will be true pretty much always for a parameter, unless it's value is null.  If you want to check that it's got a value that is not the empty string, use !empty() or just if ($var).

 

	if (is_array($rows)) {
		if (isset($rows) && $rows != "*") {

 

Checking isset() is a bit pointless there.  if it is an array, it is set.  Also, as mentioned above isset would only return false if you pass null, not if you pass say an empty string or empty array.  Also, if $rows is an array, then it is guaranteed to != '*'.  Perhaps you ment $rows[0] != '*'?

 

function select($table, $rows = array('*'), $cond = "") {
	$query = "SELECT {$rows} FROM `{$table}`";

	if ($cond){
		$query .= " WHERE {$cond}";
	}

	if (is_array($rows) && $rows[0] != '*') {
			foreach ($rows as $v) {
				print_r($v);
			}
	}


	//return mysql_query($query);
}

Link to comment
https://forums.phpfreaks.com/topic/253200-select-function/#findComment-1298060
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.