One option that you have is extending mysqli into your own class and then add methods in there to do what you want. Here is an example without any real error checking (untested):
<?php
class MyDbClass extends mysqli
{
/**
* Initializes the class. Passes given parameters to mysqli
*
* @return void
*/
public function __construct()
{
// Get all of the arguments that were passed
$args = func_get_args();
// Clean up the arguments
$cleaned_args = join(',', array_map('addSingleQuotes', $args));
// Pass all arguments to the parent's constructor
parent::init();
eval("parent::__construct(" . $cleaned_args . ");");
}
/**
* Runs given query and returns the first field of the first row returned.
* Ignores all other data returned by the query.
*
* @param string $query_string A valid MySQL query string.
*
* @return string
*/
function queryToSingleField($query_string) {
$result = $this->query($query_string);
$row = $result->fetch_array(MYSQLI_NUM);
return $row[0];
}
}
/**
* Escapes Single Quotes and Backslashes from the given string
*
* @param string $arg The string that we want to clean up.
*
* @return string
*/
function addSingleQuotes($arg)
{
// single quote and escape single quotes and backslashes
return "'" . addcslashes($arg, "'\\") . "'";
}
?>
Usage:
<?php
/**
* Usage
*/
$db_conn = new MyDbClass("localhost", "my_user", "my_password", "my_db");
$name = $db_conn->queryToSingleField("SELECT `name` FROM `table` WHERE id='1'");
?>
Here is the page that explains this better. http://devzone.zend.com/node/view/id/687
For an example of what laserlight was talking about take a look here: mysqli_stmt::fetch