Fetch single row from database using `$_GET['MyVariable']; in PHP

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) . "'";
}

Maybe something like this:

//db connection
include('../test_db_pdo.php');
//set utf8 mode
$conn->exec("set names utf8");
//set error mode
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$tablename = 'xxxx';


//set $_GET data
$member_id = isset($_GET['member_id']) ? $_GET['member_id'] : '';

$employeeDetails_sql = "SELECT * FROM $tablename WHERE member_id = :member_id";
$employeeDetails_stmt = $conn->prepare($employeeDetails_sql);
$employeeDetails_stmt->bindValue(':member_id', $member_id);
$employeeDetails_stmt->execute();
$employeeDetails_stmt->setFetchMode(PDO::FETCH_ASSOC);	
$_employeeDetails = $employeeDetails_stmt->fetch(); //single row

$colcount = $employeeDetails_stmt->columnCount();
$rowcount = $employeeDetails_stmt->rowCount();	

if($rowcount <= 0){
	//no member record found
	return 'There was no member id found.<br>Please try again..<br>' . print_r($_employeeDetails);
}else{		
	$target_id = $_employeeDetails['member_id'];
	$target_first_name = $_employeeDetails['first_name'];
	$target_last_name = $_employeeDetails['last_name'];
	$target_email = $_employeeDetails['email'];
	
	echo 'EMPLOYEE ID: ' . $target_id . '<br>';
	echo 'FIRST NAME: ' . $target_first_name . '<br>';
	echo 'LAST NAME: ' . $target_last_name . '<br>';
	echo 'EMAIL: ' . $target_email . '<br>';
	
}	

Sponsor our Newsletter | Privacy Policy | Terms of Service