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
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
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
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
Share on other sites

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.