Undefined function error

I am an intern at a large tech company. One of my side projects is making a web app to manage all of the IPs, serial numbers and licenses in my group’s lab. I am fairly new to PHP. In my database, every IP has an integer “type” field that identifies the IP as that belonging to either a generic IP, one used for management of disk arrays, iSCSI, or FCoE. I then wrote a function in PHP to replace the integer with a corresponding string before sending it to the web application:

[php]function ipTypeConvert($ipType){

	if($ipType == 0)
		return "IP";
	else if($ipType == 1)
		return "Mgmt";
	else if($ipType == 2)
		return "iSCSI";
	else if($ipType == 3)
		return "FCoE";
	else
		return null;
}[/php]

If this isn’t a good way to do this sort of thing (with a PHP function, that is) I’d appreciate other suggestions.

Below is the main function that searches for IP records by name:

[php]public function getIPsByName($search) {

		  if ($search=="") {
		  	$stmt = mysqli_prepare($this->connection, "SELECT * FROM ips");
		  } else {
		  $stmt = mysqli_prepare($this->connection,'SELECT * FROM ips WHERE system_name LIKE \'%'.mysql_escape_string(		              $search).'%\'');
		  }
		  $this->throwExceptionOnError();
		  
		  mysqli_stmt_execute($stmt);
		  $this->throwExceptionOnError();
		  
		  $rows = array();
		  
		  mysqli_stmt_bind_result($stmt, $row->IP, $row->IP_type, $row->host_type, $row->system_name, $row->model, $row->lab, $row->rack, $row->MAC, $row->rack_sn, $row->asset_sn, $row->firmware, $row->username, $row->password, $row->notes, $row->port_number, $row->speed);
		  while (mysqli_stmt_fetch($stmt)) {
      $row[1]=ipTypeConvert($row[1]);
	  $row[2]=hostTypeConvert($row[2]);
	  $rows[] = $row;
      $row = new stdClass();
      mysqli_stmt_bind_result($stmt, $row->IP, $row->IP_type, $row->host_type, $row->system_name, $row->model, $row->lab, $row->rack, $row->MAC, $row->rack_sn, $row->asset_sn, $row->firmware, $row->username, $row->password, $row->notes, $row->port_number, $row->speed);
    }
	
	mysqli_stmt_free_result($stmt);
    mysqli_close($this->connection);

    return $rows;
}[/php]

Before anyone says anything about storing usernames and passwords as plaintext, it’s ok since our group does testing, there is no sensitive data, and they’re mostly just the default ones.

Anyway, when I test my app, it throws an “Call to undefined function ipTypeConvert()” error.

Things I have tried that have not worked:

[ul][li]Adding “$this->” before calling the function[/li]
[li]Adding “public” before “function” in the declaration[/li][/ul]

I’ve looked at a bunch of tutorials for creating and referencing PHP functions, but can’t seem to find the problem :frowning:

I’d greatly appreciate any suggestions. I know it’s probably some dumb syntax thing.

Thanks for your time in advance.

I wouldn’t say there’s anything wrong with doing that but personally I would use an array containing the types:

[php]function ipTypeConvert($ipType){
$types = array(‘IP’, ‘Mgmt’, ‘iSCSI’, ‘FCoE’);

if(isset($types[$ipType])) {
    return $types[$ipType];
}

return null;

}[/php]

For your main problem, is the function defined in the same file? If not, is the containing file required or included before the function is used? Also, what context is this in? Are the function(s) in classes?

Thanks for the array tip, that does look like a cleaner way to do it.

The function is defined in the same file. It is in a class that was auto-generated by Adobe Flash builder from my database. It works otherwise, that is, the data shows up in the app without the above code. Herer’s the whole thing as it currently stands:

[php]<?php

/**

  • README for sample service
  • This generated sample service contains functions that illustrate typical service operations.
  • Use these functions as a starting point for creating your own service implementation. Modify the
  • function signatures, references to the database, and implementation according to your needs.
  • Delete the functions that you do not use.
  • Save your changes and return to Flash Builder. In Flash Builder Data/Services View, refresh
  • the service. Then drag service operations onto user interface components in Design View. For
  • example, drag the getAllItems() operation onto a DataGrid.
  • This code is for prototyping only.
  • Authenticate the user prior to allowing them to call these methods. You can find more
  • information at http://www.adobe.com/go/flex_security

*/
class IpsService {

var $username = "root";
var $password = "";
var $server = "localhost";
var $port = "3306";
var $databasename = "ip_inventory";
var $tablename = "ips";

var $connection;

/**
 * The constructor initializes the connection to database. Everytime a request is 
 * received by Zend AMF, an instance of the service class is created and then the
 * requested method is invoked.
 */
public function __construct() {
  	$this->connection = mysqli_connect(
  							$this->server,  
  							$this->username,  
  							$this->password, 
  							$this->databasename,
  							$this->port
  						);

	$this->throwExceptionOnError($this->connection);
}

/**
 * Returns the string corresponding to the ipType int
 */
function ipTypeConvert($ipType){
	
	if($ipType == 0)
		return "IP";
	else if($ipType == 1)
		return "Mgmt";
	else if($ipType == 2)
		return "iSCSI";
	else if($ipType == 3)
		return "FCoE";
	else
		return null;
}


/**
 * Returns the string corresponding to the hostType int
 */
function hostTypeConvert($hostType){
	
	if($hostType == 0)
		return "Server";
	else if($hostType == 1)
		return "Array";
	else if($hostType == 2)
		return "VM";
	else if($hostType == 3)
		return "Switch";
	else if($hostType == 4)
		return "Router";
		
}



/**
 * Returns all the rows from the table.
 *
 * Add authroization or any logical checks for secure access to your data 
 *
 * @return array
 */
public function getAllIps() {

	$stmt = mysqli_prepare($this->connection, "SELECT * FROM $this->tablename");		
	$this->throwExceptionOnError();
	
	mysqli_stmt_execute($stmt);
	$this->throwExceptionOnError();
	
	$rows = array();
	
	mysqli_stmt_bind_result($stmt, $row->IP, $row->IP_type, $row->host_type, $row->system_name, $row->model, $row->lab, $row->rack, $row->MAC, $row->rack_sn, $row->asset_sn, $row->firmware, $row->username, $row->password, $row->notes, $row->port_number, $row->speed);
	
    while (mysqli_stmt_fetch($stmt)) {
	  $row[1]=$this->ipTypeConvert($row[1]);
	  $row[2]=$this->hostTypeConvert($row[2]);
      $rows[] = $row;
      $row = new stdClass();
      mysqli_stmt_bind_result($stmt, $row->IP, $row->IP_type, $row->host_type, $row->system_name, $row->model, $row->lab, $row->rack, $row->MAC, $row->rack_sn, $row->asset_sn, $row->firmware, $row->username, $row->password, $row->notes, $row->port_number, $row->speed);
    }
	
	mysqli_stmt_free_result($stmt);
    mysqli_close($this->connection);

    return $rows;
}

public function getIPsByName($search) {
		  
		  if ($search=="") {
		  	$stmt = mysqli_prepare($this->connection, "SELECT * FROM ips");
		  } else {
		  $stmt = mysqli_prepare($this->connection,'SELECT * FROM ips WHERE system_name LIKE \'%'.mysql_escape_string(		              $search).'%\'');
		  }
		  $this->throwExceptionOnError();
		  
		  mysqli_stmt_execute($stmt);
		  $this->throwExceptionOnError();
		  
		  $rows = array();
		  
		  mysqli_stmt_bind_result($stmt, $row->IP, $row->IP_type, $row->host_type, $row->system_name, $row->model, $row->lab, $row->rack, $row->MAC, $row->rack_sn, $row->asset_sn, $row->firmware, $row->username, $row->password, $row->notes, $row->port_number, $row->speed);
		  while (mysqli_stmt_fetch($stmt)) {
      $row[1]=ipTypeConvert($row[1]);
	  $row[2]=hostTypeConvert($row[2]);
	  $rows[] = $row;
      $row = new stdClass();
      mysqli_stmt_bind_result($stmt, $row->IP, $row->IP_type, $row->host_type, $row->system_name, $row->model, $row->lab, $row->rack, $row->MAC, $row->rack_sn, $row->asset_sn, $row->firmware, $row->username, $row->password, $row->notes, $row->port_number, $row->speed);
    }
	
	mysqli_stmt_free_result($stmt);
    mysqli_close($this->connection);

    return $rows;
}

/**
 * Returns the item corresponding to the value specified for the primary key.
 *
 * Add authorization or any logical checks for secure access to your data 
 *
 * 
 * @return stdClass
 */
public function getIpsByID($itemID) {
	
	$stmt = mysqli_prepare($this->connection, "SELECT * FROM $this->tablename where IP=?");
	$this->throwExceptionOnError();
	
	mysqli_stmt_bind_param($stmt, 'i', $itemID);		
	$this->throwExceptionOnError();
	
	mysqli_stmt_execute($stmt);
	$this->throwExceptionOnError();
	
	mysqli_stmt_bind_result($stmt, $row->IP, $row->IP_type, $row->host_type, $row->system_name, $row->model, $row->lab, $row->rack, $row->MAC, $row->rack_sn, $row->asset_sn, $row->firmware, $row->username, $row->password, $row->notes, $row->port_number, $row->speed);
	
	if(mysqli_stmt_fetch($stmt)) {
      return $row;
	} else {
      return null;
	}
}

/**
 * Returns the item corresponding to the value specified for the primary key.
 *
 * Add authorization or any logical checks for secure access to your data 
 *
 * 
 * @return stdClass
 */
public function createIps($item) {

	$stmt = mysqli_prepare($this->connection, "INSERT INTO $this->tablename (IP_type, host_type, system_name, model, lab, rack, MAC, rack_sn, asset_sn, firmware, username, password, notes, port_number, speed) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
	$this->throwExceptionOnError();

	mysqli_stmt_bind_param($stmt, 'iissssissssssis', $item->IP_type, $item->host_type, $item->system_name, $item->model, $item->lab, $item->rack, $item->MAC, $item->rack_sn, $item->asset_sn, $item->firmware, $item->username, $item->password, $item->notes, $item->port_number, $item->speed);
	$this->throwExceptionOnError();

	mysqli_stmt_execute($stmt);		
	$this->throwExceptionOnError();

	$autoid = mysqli_stmt_insert_id($stmt);

	mysqli_stmt_free_result($stmt);		
	mysqli_close($this->connection);

	return $autoid;
}

/**
 * Updates the passed item in the table.
 *
 * Add authorization or any logical checks for secure access to your data 
 *
 * @param stdClass $item
 * @return void
 */
public function updateIps($item) {

	$stmt = mysqli_prepare($this->connection, "UPDATE $this->tablename SET IP_type=?, host_type=?, system_name=?, model=?, lab=?, rack=?, MAC=?, rack_sn=?, asset_sn=?, firmware=?, username=?, password=?, notes=?, port_number=?, speed=? WHERE IP=?");		
	$this->throwExceptionOnError();
	
	mysqli_stmt_bind_param($stmt, 'iissssissssssisi', $item->IP_type, $item->host_type, $item->system_name, $item->model, $item->lab, $item->rack, $item->MAC, $item->rack_sn, $item->asset_sn, $item->firmware, $item->username, $item->password, $item->notes, $item->port_number, $item->speed, $item->IP);		
	$this->throwExceptionOnError();

	mysqli_stmt_execute($stmt);		
	$this->throwExceptionOnError();
	
	mysqli_stmt_free_result($stmt);		
	mysqli_close($this->connection);
}

/**
 * Deletes the item corresponding to the passed primary key value from 
 * the table.
 *
 * Add authorization or any logical checks for secure access to your data 
 *
 * 
 * @return void
 */
public function deleteIps($itemID) {
			
	$stmt = mysqli_prepare($this->connection, "DELETE FROM $this->tablename WHERE IP = ?");
	$this->throwExceptionOnError();
	
	mysqli_stmt_bind_param($stmt, 'i', $itemID);
	mysqli_stmt_execute($stmt);
	$this->throwExceptionOnError();
	
	mysqli_stmt_free_result($stmt);		
	mysqli_close($this->connection);
}


/**
 * Returns the number of rows in the table.
 *
 * Add authorization or any logical checks for secure access to your data 
 *
 * 
 */
public function count() {
	$stmt = mysqli_prepare($this->connection, "SELECT COUNT(*) AS COUNT FROM $this->tablename");
	$this->throwExceptionOnError();

	mysqli_stmt_execute($stmt);
	$this->throwExceptionOnError();
	
	mysqli_stmt_bind_result($stmt, $rec_count);
	$this->throwExceptionOnError();
	
	mysqli_stmt_fetch($stmt);
	$this->throwExceptionOnError();
	
	mysqli_stmt_free_result($stmt);
	mysqli_close($this->connection);
	
	return $rec_count;
}


/**
 * Returns $numItems rows starting from the $startIndex row from the 
 * table.
 *
 * Add authorization or any logical checks for secure access to your data 
 *
 * 
 * 
 * @return array
 */
public function getIps_paged($startIndex, $numItems) {
	
	$stmt = mysqli_prepare($this->connection, "SELECT * FROM $this->tablename LIMIT ?, ?");
	$this->throwExceptionOnError();
	
	mysqli_stmt_bind_param($stmt, 'ii', $startIndex, $numItems);
	mysqli_stmt_execute($stmt);
	$this->throwExceptionOnError();
	
	$rows = array();
	
	mysqli_stmt_bind_result($stmt, $row->IP, $row->IP_type, $row->host_type, $row->system_name, $row->model, $row->lab, $row->rack, $row->MAC, $row->rack_sn, $row->asset_sn, $row->firmware, $row->username, $row->password, $row->notes, $row->port_number, $row->speed);
	
    while (mysqli_stmt_fetch($stmt)) {
      $rows[] = $row;
      $row = new stdClass();
      mysqli_stmt_bind_result($stmt, $row->IP, $row->IP_type, $row->host_type, $row->system_name, $row->model, $row->lab, $row->rack, $row->MAC, $row->rack_sn, $row->asset_sn, $row->firmware, $row->username, $row->password, $row->notes, $row->port_number, $row->speed);
    }
	
	mysqli_stmt_free_result($stmt);		
	mysqli_close($this->connection);
	
	return $rows;
}


/**
 * Utility function to throw an exception if an error occurs 
 * while running a mysql command.
 */
private function throwExceptionOnError($link = null) {
	if($link == null) {
		$link = $this->connection;
	}
	if(mysqli_error($link)) {
		$msg = mysqli_errno($link) . ": " . mysqli_error($link);
		throw new Exception('MySQL Error - '. $msg);
	}		
}

}

?>[/php]

Again, thanks for your help, I really appreciate you taking the time to look at this :slight_smile:

[php]$row[1]=ipTypeConvert($row[1]);
$row[2]=hostTypeConvert($row[2]);[/php]

Are missing the $this prefix:

[php]$row[1] = $this->ipTypeConvert($row[1]);
$row[2] = $this->hostTypeConvert($row[2]);[/php]

Oh wow, I did put that in, but in getAllIps() instead of getIPsByName :P. So that’s sorted out, and the function currently looks like this:

[php]public function getIPsByName($search) {

	  if ($search=="") {
		  	$stmt = mysqli_prepare($this->connection, "SELECT * FROM ips");
	  } else {
	  $stmt = mysqli_prepare($this->connection,'SELECT * FROM ips WHERE system_name LIKE \'%'.mysql_escape_string(		              $search).'%\'');
	  }
	  $this->throwExceptionOnError();
		  
	  mysqli_stmt_execute($stmt);
	  $this->throwExceptionOnError();
		  
	  $rows = array();
		  
	  mysqli_stmt_bind_result($stmt, $row->IP, $row->IP_type, $row->host_type, $row->system_name, $row->model, $row->lab, $row->rack, $row->MAC, $row->rack_sn, $row->asset_sn, $row->firmware, $row->username, $row->password, $row->notes, $row->port_number, $row->speed);
		  while (mysqli_stmt_fetch($stmt)) {
	  $row = (array) $row;
          $row[1]= $this->ipTypeConvert($row[1]);
	  $row[2]= $this->hostTypeConvert($row[2]);
	  $rows[] = $row;
          $row = new stdClass();
          mysqli_stmt_bind_result($stmt, $row->IP, $row->IP_type, $row->host_type, $row->system_name, $row->model, $row->lab, $row->rack, $row->MAC, $row->rack_sn, $row->asset_sn, $row->firmware, $row->username, $row->password, $row->notes, $row->port_number, $row->speed);
    }[/php]

And throws a “Undefined offset: 1” error at the line: $row[1]= $this->ipTypeConvert($row[1]);

Does that mean it thinks the array does not have anything at [1]?

Thanks so much again for your help and prompt responses!!!

I’ve never used mysqli_stmt_bind_result but from the php.net manual, I would assume that you would have to at least define the array before using it - if not also define the keys that you are going to use.

Try adding:

[php]$row = array();[/php]
Before:
[php]mysqli_stmt_bind_result($stmt, $row->IP, $row->IP_type, $row->host_type, $row->system_name, $row->model, $row->lab, $row->rack, $row->MAC, $row->rack_sn, $row->asset_sn, $row->firmware, $row->username, $row->password, $row->notes, $row->port_number, $row->speed);[/php]

And if that doesn’t work, you could try defining the array with keys:

[php]$row = array(‘IP’ => ‘’, ‘IP_type’ => ‘’, etc etc);[/php]

Got it working!

This is how I ended up doing it:

[php]$row->IP_type= $this->ipTypeConvert($row->IP_type);
$row->host_type= $this->hostTypeConvert($row->host_type);[/php]

Whole function:
[php]public function getIPsByName($search) {

		  if ($search=="") {
		  	$stmt = mysqli_prepare($this->connection, "SELECT * FROM ips");
		  } else {
		  $stmt = mysqli_prepare($this->connection,'SELECT * FROM ips WHERE system_name LIKE \'%'.mysql_escape_string(		              $search).'%\'');
		  }
		  $this->throwExceptionOnError();
		  
		  mysqli_stmt_execute($stmt);
		  $this->throwExceptionOnError();
		  
		  $rows = array();
		  
		  mysqli_stmt_bind_result($stmt, $row->IP, $row->IP_type, $row->host_type, $row->system_name, $row->model, $row->lab, $row->rack, $row->MAC, $row->rack_sn, $row->asset_sn, $row->firmware, $row->username, $row->password, $row->notes, $row->port_number, $row->speed);
		  
		  while (mysqli_stmt_fetch($stmt)) {
			 $row->IP_type= $this->ipTypeConvert($row->IP_type);
	                 $row->host_type= $this->hostTypeConvert($row->host_type);
	                 $rows[] = $row;

                         $row = new stdClass();
	  
                         mysqli_stmt_bind_result($stmt, $row->IP, $row->IP_type, $row->host_type, $row->system_name, $row->model, $row->lab, $row->rack, $row->MAC, $row->rack_sn, $row->asset_sn, $row->firmware, $row->username, $row->password, $row->notes, $row->port_number, $row->speed);
	  
                  }
	
                  mysqli_stmt_free_result($stmt);
                  mysqli_close($this->connection);

                  return $rows;
}[/php]

I guess the msqli_stmt_bind_sesult function creates those variables inside of a row object, so I can just access them and change them. I just didn’t quite understand what that function did at first.

Thanks for your help!

Sponsor our Newsletter | Privacy Policy | Terms of Service