Calling a Function

I have a function called ObtainUserName in a page called functions.php. For an instance im working on my page purchase_edit.php. Inside that page i have the following data Name, Date, Means of Payment, Total, Books and all of them are in php. This is what im talking about. If i remove the ObtainUserName, it displays the iduser with no problem. if i put ObtainUserName its display a funny background color that displays <>Name</p> only…

  <h1>Check Purchase</h1>
  <p>Name: <?php echo ObtainUserName($row_Datapurchase['iduser']); ?></p>

  <p>Date:<?php echo $row_Datapurchase['datepurchase']; ?></p>

  <p>Means of Payment:<?php echo $row_Datapurchase['typepayment']; ?></p>

  <p>Total:<?php echo $row_Datapurchase['total']; ?></p>

  <p>Books:</p>

Hi CaliforniaLove,

Welcome to the SitePoint.

Please supply the source of your function and don’t forget to enclose the script inside three backticks with and linefeeds.

Hi, John Betong

This is my source function.

function ObtainProductName($identifier)
{

    global $database_databaseConnection, $databaseConnection;
    mysql_select_db($database_databaseConnection, $databaseConnection);
    $query_QueryFunction = sprintf("SELECT name FROM book WHERE idbook = %s",$identifier);
    $QueryFunction = mysql_query($query_QueryFunction, $databaseConnection) or die(mysql_error());
    $row_QueryFunction = mysql_fetch_assoc($QueryFunction);
    $totalRows_QueryFunction = mysql_num_rows($QueryFunction);

    return $row_QueryFunction['name']; 
    mysql_free_result($QueryFunction);
}

Try this:

function ObtainProductName($identifier)
{
  global $database_databaseConnection, $databaseConnection;

  mysql_select_db($database_databaseConnection, $databaseConnection);

  $query_QueryFunction = sprintf("SELECT name FROM book WHERE idbook = %s",$identifier);

  $QueryFunction = mysql_query($query_QueryFunction, $databaseConnection) or die(mysql_error());

  $row_QueryFunction = mysql_fetch_assoc($QueryFunction);

  $totalRows_QueryFunction = mysql_num_rows($QueryFunction);

  $result = $row_QueryFunction['name']; 

  // DEBUG - unrem following line to see $result
  // var_dump( $result );die;

  return $result;

  # return $row_QueryFunction['name']; 
  # mysql_free_result($QueryFunction);
}

I think the return is not necessary and also clears/removes $row_QueryFunction[‘name’];

edit
inserted missing $result into var_dump();

im getting a fatal error.

  1. Fatal error: Call to undefined function ObtainUserName() in C:\wamp\www\bookstore\admin\purchase_edit.php on line 82
    Call Stack
#	Time	Memory	Function	Location
1	0.0032	150968	{main}( )	..\purchase_edit.php:0

The function being called is not the same name as the function you supplied.

The ObtainProductName function works fine. ObtainUserName is the function im having a problem with…

I have to ask. Then why did you post the code for ObtainProductName ??

If function ObtainProductName(…) is OK then try copying and pasting the function ObtainUserName (…) immediately below function ObtainProductName(…)

function ObtainProductName($val=''test)
{
  echo __METHOD__; die;
}

You may also try this function:

http://php.net/manual/en/function.function-exists.php

Then what will happen

It will prove that the function exists. If it does exist then copy and paste the script from the function that cannot be found.

One step at a time…

Can you simply tell me how to do this…

Put that in your code and run the file.

how are do u run the file. plz

Where do i put this… In the ObtainUserName Function

“The ObtainProductName function works fine. ObtainUserName is the function im having a problem with…”

If the above function is OK then do the following:

function ObtainProductName($whatever=NULL)
{
 ...
}


function ObtainUserName($whatever=NULL)
{
 echo ___METHOD___;
die;
}

The function ObtainUserName comes first… Then Function ObatinProductName… Do you want it like this…

function ObtainUserName($whatever=NULL)
{
echo METHOD;
die;
}

function ObtainProductName($whatever=NULL)
{

global $database_databaseConnection, $databaseConnection;
mysql_select_db($database_databaseConnection, $databaseConnection);
$query_QueryFunction = sprintf(“SELECT name FROM book WHERE idbook = %s”,$identifier);
$QueryFunction = mysql_query($query_QueryFunction, $databaseConnection) or die(mysql_error());
$row_QueryFunction = mysql_fetch_assoc($QueryFunction);
$totalRows_QueryFunction = mysql_num_rows($QueryFunction);

return $row_QueryFunction[‘name’];
mysql_free_result($QueryFunction);
}

I do not think the function order is a problem, so give it a try.

Off topic
Please note that preceding text with the < highlights PHP scripts but removes underscores. to prevent this from happening insert three backticks on a new line, then your script followed by a new line and three more backticks.

correct, the problem is the PHP error #2.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.