Jump to content

Call to undefined method mysqli_stmt::get_result()


Hall of Famer

Recommended Posts

Well I have a library file class_database.php which defines a class called 'database' and a method called 'get' to retrieve database info. It was originally designed to return a two-dimensional array with the format of $array['index']['associative']. Right now I want to change this so as to return a result, which can then be used in other script files to retrieve data by using the fetch_array() method after getting this result. It doesnt work though, and I got this error 'Call to undefined method mysqli_stmt::get_result()', which confuses me...

 

The two methods get() and _buildQuery are shown below, please help me figure out how to return an Mysql result so that I can use while($row = $db->get()->fetch_array()) on it with flexibility...

 

The method get()

public function get($tableName, $numRows = NULL) 
{

	$this->_query = "SELECT * FROM $tableName";
	$stmt = $this->_buildQuery($numRows);
	$stmt->execute();
                $results = $stmt->get_result();

	$this->reset();
	return $results;
}

 

The method  _buildQuery is shown below:

protected function _buildQuery($numRows = NULL, $tableData = NULL) 
{
	(gettype($tableData) === 'array') ? $hasTableData = true : $hasTableData = false;	
	(!empty($this->_where )) ? $hasConditional = true : $hasConditional = false;

	// Did the user call the "where" method?
	if (!empty($this->_where)) {

		// if update data was passed, filter through and create the SQL query, accordingly.
		if ($hasTableData) {
			$i = 1;
			$pos = strpos($this->_query, 'UPDATE');
			if ( $pos !== false) {
				foreach ($tableData as $prop => $value) {
					// determines what data type the item is, for binding purposes.
					$this->_paramTypeList .= $this->_determineType($value);

					// prepares the reset of the SQL query.
					($i === count($tableData)) ?
						$this->_query .= $prop . ' = ?':
						$this->_query .= $prop . ' = ?, ';

					$i++;
				}
			}
		}

		//Prepair the where portion of the query
		$this->_query .= ' WHERE ';	
		$i = 1;
		foreach ($this->_where as $column => $value) {
			// Determines what data type the where column is, for binding purposes.
			$this->_whereTypeList .= $this->_determineType($value);

			// Prepares the reset of the SQL query.
			($i === count($this->_where)) ?
				$this->_query .= $column . ' = ?':
				$this->_query .= $column . ' = ? AND ';

			$i++;
		}

	}

	// Determine if is INSERT query
	if ($hasTableData) {
		$pos = strpos($this->_query, 'INSERT');

		if ($pos !== false) {
			//is insert statement
			$keys = array_keys($tableData);
			$values = array_values($tableData);
			$num = count($keys);

			// wrap values in quotes
			foreach ($values as $key => $val) {
				$values[$key] = "'{$val}'";
				$this->_paramTypeList .= $this->_determineType($val);
			}

			$this->_query .= '(' . implode($keys, ', ') . ')';
			$this->_query .= ' VALUES(';
			while ($num !== 0) {
				($num !== 1) ? $this->_query .= '?, ' : $this->_query .= '?)';
				$num--;
			}
		}
	}

	// Did the user set a limit
	if (isset($numRows)) {
		$this->_query .= " LIMIT " . (int) $numRows;
	}

	// Prepare query
	$stmt = $this->_prepareQuery();

	// Prepare table data bind parameters
	if ($hasTableData) {
		$this->_bindParams[0] = $this->_paramTypeList;
		foreach ($tableData as $prop => $val) {
			array_push($this->_bindParams, &$tableData[$prop]);
		}
	}
	// Prepare where condition bind parameters
	if($hasConditional) {
		if ($this->_where) {
			$this->_bindParams[0] .= $this->_whereTypeList;
			foreach ($this->_where as $prop => $val) {
				array_push($this->_bindParams, &$this->_where[$prop]);
			}
		}	
	}
	// Bind parameters to statment
	if ($hasTableData || $hasConditional){
		call_user_func_array(array($stmt, 'bind_param'), $this->_bindParams);
	}

	return $stmt;
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.