Page 1 of 1

Function with Return

Posted: Fri Apr 09, 2010 1:20 pm
by joeyd
Hello,

I am trying to put a function together to pull row fields from a database and not quite sure how to do it. Here is what I do have...

This is an include in my index.php page: function.php

Code: Select all

<?php
// Get Page by ID using echo Page('548'); 

function Page ($PageID) {

$colname_rsTest = "-1";
if (isset($PageID)) {
  $colname_rsTest = $PageID;
}

mysql_select_db($database_conn, mysql_pconnect($hostname_conn,$username_conn,$password_conn));
$query_rsTest = sprintf("SELECT * FROM tblpages WHERE PageID = %s", GetSQLValueString($colname_rsTest, "int"));
$rsTest = mysql_query($query_rsTest, mysql_pconnect($hostname_conn,$username_conn,$password_conn)) or die(mysql_error());
$row_rsTest = mysql_fetch_assoc($rsTest);
$totalRows_rsTest = mysql_num_rows($rsTest);

return $PageTitle = $row_rsTest['PageTitle'];

mysql_free_result($rsTest);
}
?>
This is the code that calls the function "Page". It passes along the ID of the page: index.php

Code: Select all

<?php echo Page($_GET['s']);?>

This works fine and I can pull the page title. But what I am trying to do is return more than just a page title. The page consists of several database fields...

MetaTitle
PageTitle
Content

I am looking for a way to call the function and pull each field like so...

Code: Select all

<?php echo Page($_GET['s'], $PageTitle);?>
<?php echo Page($_GET['s'], $MetaTitle);?>
<?php echo Page($_GET['s'], $Content);?>
Or something similar. The main thing is to use the same function to pull all three fields separately. Any help in direction or samples would be much appreciated.

Re: Function with Return

Posted: Fri Apr 09, 2010 1:41 pm
by AbraCadaver
Several ways to do it. I would just return the entire row from the Page() function:

Code: Select all

return $row_rsTest;
Then use it:

Code: Select all

$page = Page($_GET['s']);
echo $page['MetaTitle'];

//or kinda clumsy but some like it

extract(Page($_GET['s']));
echo $MetaTitle;
Or you can pass the function something:

Code: Select all

function Page($PageID, $field='') {

	if (!isset($PageID)) {
		return false;
	}	
	mysql_select_db($database_conn, mysql_pconnect($hostname_conn,$username_conn,$password_conn));
	$query_rsTest = sprintf("SELECT * FROM tblpages WHERE PageID = %s", GetSQLValueString($colname_rsTest, "int"));
	$rsTest = mysql_query($query_rsTest, mysql_pconnect($hostname_conn,$username_conn,$password_conn)) or die(mysql_error());
	$row_rsTest = mysql_fetch_assoc($rsTest);
	mysql_free_result($rsTest);
	
	if(isset($field, $row_rsTest[$field])) {
		return $row_rsTest[$field];
	} else {
		return $row_rsTest;
	}
	
}
And use it like:

Code: Select all

echo Page($_GET['s'], 'MetaTitle');

Re: Function with Return

Posted: Fri Apr 09, 2010 2:31 pm
by joeyd
Thanks Abra,

I tried both options you have and the first options just returns the word "Array".

I tried the second option and they return no values. Which I like this one just for the ease of syntax. But not sure whats up. Any thoughts on why it returns no values?

Here is what I have...

The Include File:

Code: Select all

<?php

function Page($PageID, $field='') {

        if (!isset($PageID)) {
                return false;
        }      
        mysql_select_db($database_conn, mysql_pconnect($hostname_conn,$username_conn,$password_conn));
        $query_rsTest = sprintf("SELECT * FROM tblpages WHERE PageID = %s", GetSQLValueString($colname_rsTest, "int"));
        $rsTest = mysql_query($query_rsTest, mysql_pconnect($hostname_conn,$username_conn,$password_conn)) or die(mysql_error());
        $row_rsTest = mysql_fetch_assoc($rsTest);
        mysql_free_result($rsTest);
       
        if(isset($field, $row_rsTest[$field])) {
                return $row_rsTest[$field];
        } else {
                return $row_rsTest;
        }
	
mysql_free_result($rsTest);
}
?>
The Index Page:

Code: Select all

<?php echo  Page($_GET['s'], 'MetaTitle'); ?></ br>
<?php echo  Page($_GET['s'], 'PageTitle'); ?>

Re: Function with Return

Posted: Fri Apr 09, 2010 2:55 pm
by joeyd
Doing some tests to make sure the variables were being passed and they are. The first arg was a number and the second came back as the field name being called.

Arg 1 = 547 // Page ID
Arg 2 = MetaTitle // Name of the field

So these two args are making it to the function. It just does not want to return the data in the fields.

Re: Function with Return

Posted: Fri Apr 09, 2010 3:10 pm
by AbraCadaver
joeyd wrote:Doing some tests to make sure the variables were being passed and they are. The first arg was a number and the second came back as the field name being called.

Arg 1 = 547 // Page ID
Arg 2 = MetaTitle // Name of the field

So these two args are making it to the function. It just does not want to return the data in the fields.
Before:

Code: Select all

if(isset($field, $row_rsTest[$field])) {
Add:

Code: Select all

echo highlight_string(print_r($row_rsTest, true));

Re: Function with Return

Posted: Fri Apr 09, 2010 3:15 pm
by AbraCadaver
Nevermind. I had removed your $colname_rsTest assignment. Change the $query to this:

Code: Select all

$query_rsTest = sprintf("SELECT * FROM tblpages WHERE PageID = %s", GetSQLValueString($PageID, "int"));

Re: Function with Return

Posted: Fri Apr 09, 2010 3:23 pm
by joeyd
Aha! That was it and I didn't even think about it. And... thanks much for the help!