Jump to content

Returning an array in a user function...


MinusMyThoughts

Recommended Posts

Hey, guys!I'm attempting to build a function library for myself so I can save time while programming, but I'm running into some issues right off the bat.Here's what I'm trying to do:I want to build a function called "queryTable()" that will pass a series of parameters to allow me to search my database tables and return an array.Here's what I'm having trouble with:I can't seem to get my array to carry on outside the function. I tried looking through the documentation, but because I'm not too clear on the terminology of what I'm trying to do, I'm not getting far.Here's the function I'm attempting to use:

function queryTable($tableName,$select,$where,$orderbyASC_DESC,$limit)	{		$query = "SELECT * FROM {$tableName}";				if ( !empty($select) )			$query = "SELECT {$select} FROM {$tableName}";				if ( !empty($where) )			$query .= " WHERE {$where}";					if ( !empty($orderbyASC_DESC) )			$query .= " ORDER BY {$orderbyASC_DESC}";					if ( !empty($limit) )			$query .= " LIMIT {$limit}";				$result = mysql_query($query) or die(mysql_error());				return $queryTable = mysql_fetch_array($result) or die(mysql_error());			}

My goal is to be able to use a variable later, such as "$queryTable['studentName']" in code outside the function, but so far I'm not having any luck.I'd appreciate any feedback, either relevant to my problem or relevant to the general look and cleanliness of the function itself. Like I said, I've never done this before, so I'm all for constructive criticism.Thanks!-Jason

Link to comment
Share on other sites

As far as I'm aware, variables in functions persist only inside that function, unless made global with the global keyword like so:

return global $queryTable = mysql_fetch_array($result) or die(mysql_error());

However, I don't think that would be usefull in your case. I'm not sure if that's even correct to be honest.Try to execute the function as the value of a variable and see if you can access the variable as an array then:

function queryTable($tableName,$select,$where,$orderbyASC_DESC,$limit)	{/* your code */	 	}$myQuery = queryTable("Students");echo $myQuery['studentName'];

I haven't tested any of this. I'm only suggesting a possible approach.

Link to comment
Share on other sites

Thanks, boen_robot!I'm closer, now, but still not quite there...Running a slightly modified function:

function queryTable($tableName,$select,$where,$orderbyASC_DESC,$limit)	{		$query = "SELECT * FROM {$tableName}";				if ( !empty($select) )			$query = "SELECT {$select} FROM {$tableName}";				if ( !empty($where) )			$query .= " WHERE {$where}";					if ( !empty($orderbyASC_DESC) )			$query .= " ORDER BY {$orderbyASC_DESC}";					if ( !empty($limit) )			$query .= " LIMIT {$limit}";				$result = mysql_query($query) or die(mysql_error());				$array = mysql_fetch_assoc($result);		return $array;		}

I'm calling the variable later as:

<?=queryTable('aKa_SOM','*','','studentID DESC','1'); echo $array['studentName'];?>

The output is "Array" from that call.If I use:

<?=$queryTable = queryTable('aKa_SOM','*','','studentID DESC','1'); echo $queryTable['studentName'];?>

This returns "ArrayJohnny Student" when called.So, I guess I don't understand how to declare the variable as an array instead of a variable. I thought it was "array $array;" but I'm having no luck with that...Any suggestions?-Jason

Link to comment
Share on other sites

Like boen_robot suggested, if you declare a variable inside of a function, the scope of that variable is for that function alone. The only way to access it would be to do one of the following:1) Declare the variable first and write directly to that global $array variable.

$array;function queryTable($tableName,$select,$where,$orderbyASC_DESC,$limit)

2) Declare the variable first and pass a reference to it to your function:

$array;function queryTable($tableName,$select,$where,$orderbyASC_DESC,$limit, &$array)// I don't know if &$array is the way to do it...in C# it'd be "out array"

3) Or, this is the most common way, assign the return value of the function to a local variable

$array = queryTable(.....);

Link to comment
Share on other sites

<?=$queryTable = queryTable('aKa_SOM','*','','studentID DESC','1'); echo $queryTable['studentName'];?>

You're painfully close.First, a general note. It is generally considered good practice to not use the short open tags (<?). This is what the XML preamble looks like:<?xml version="1.0" encoding="utf-8"?>If short tags are allowed, then the <? ?> around that line causes PHP to get invoked, which produces a syntax error and execution stops. For that reason, a lot of servers disable the short tags and require the full <?php. So, it's a good practice to always use the full tags.Which brings me to the <?= shortcut. This is the same thing as saying "<? echo", so this statement:

<?=$queryTable = queryTable('aKa_SOM','*','','studentID DESC','1'); echo $queryTable['studentName'];?>

is equivalent to this:

<? echo $queryTable = queryTable('aKa_SOM','*','','studentID DESC','1'); echo $queryTable['studentName'];?>

So you can see that you are telling it to print the results from the function, which is an array. That's why you see the word "array", if you ask it to echo an array it just prints the word "array".So, all you need to do is leave off that first echo. This would do what you want:

<?php $queryTable = queryTable('aKa_SOM','*','','studentID DESC','1'); echo $queryTable['studentName']; ?>

or, expanded out:

<?php $queryTable = queryTable('aKa_SOM','*','','studentID DESC','1'); echo $queryTable['studentName']; ?>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...