Function with Return

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
joeyd
Forum Newbie
Posts: 10
Joined: Wed Mar 10, 2010 2:08 pm

Function with Return

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Function with Return

Post 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');
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
joeyd
Forum Newbie
Posts: 10
Joined: Wed Mar 10, 2010 2:08 pm

Re: Function with Return

Post 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'); ?>
joeyd
Forum Newbie
Posts: 10
Joined: Wed Mar 10, 2010 2:08 pm

Re: Function with Return

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Function with Return

Post 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));
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Function with Return

Post 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"));
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
joeyd
Forum Newbie
Posts: 10
Joined: Wed Mar 10, 2010 2:08 pm

Re: Function with Return

Post by joeyd »

Aha! That was it and I didn't even think about it. And... thanks much for the help!
Post Reply