I have this code from Github which fetches multiple rows from the database and returns Arrays of rows upon success.
I don’t want to fetch multiple rows nor return arrays of rows. I only want to fetch a single row using $_GET['MyVariable'];
and return data from a specific column matching that row. (There will be at least 3 columns in the database).
I will be greateful if anyone could help me with that. Thanks in advance.
<?php
/**
* Database functions for a MySQL with PHP tutorial
*
* @copyright Eran Galperin
* @license MIT License
* @see http://www.binpress.com/tutorial/using-php-with-mysql-the-right-way/17
*/
/**
* Connect to the database
*
* @return bool false on failure / mysqli MySQLi object instance on success
*/
function db_connect() {
// Define connection as a static variable, to avoid connecting more than once
static $connection;
// Try and connect to the database, if a connection has not been established yet
if(!isset($connection)) {
// Load configuration as an array. Use the actual location of your configuration file
// Put the configuration file outside of the document root
$config = parse_ini_file('./config.ini');
$connection = mysqli_connect('localhost',$config['username'],$config['password'],$config['dbname']);
}
// If connection was not successful, handle the error
if($connection === false) {
// Handle error - notify administrator, log to a file, show an error screen, etc.
return mysqli_connect_error();
}
return $connection;
}
/**
* Query the database
*
* @param $query The query string
* @return mixed The result of the mysqli::query() function
*/
function db_query($query) {
// Connect to the database
$connection = db_connect();
// Query the database
$result = mysqli_query($connection,$query);
return $result;
}
/**
* Fetch rows from the database (SELECT query)
*
* @param $query The query string
* @return bool False on failure / array Database rows on success
*/
function db_select($query) {
$rows = array();
$result = db_query($query);
// If query failed, return `false`
if($result === false) {
return false;
}
// If query was successful, retrieve all the rows into an array
while ($row = mysqli_fetch_assoc($result)) {
$rows[] = $row;
}
return $rows;
}
/**
* Fetch the last error from the database
*
* @return string Database error message
*/
function db_error() {
$connection = db_connect();
return mysqli_error($connection);
}
/**
* Quote and escape value for use in a database query
*
* @param string $value The value to be quoted and escaped
* @return string The quoted and escaped string
*/
function db_quote($value) {
$connection = db_connect();
return "'" . mysqli_real_escape_string($connection,$value) . "'";
}