"reference: what is a perfect code sample using the mysql extension? [closed]" Code Answer

1

my stab at it. tried to keep it as simple as possible, while still maintaining some real-world conveniences.

handles unicode and uses loose comparison for readability. be nice ;-)

<?php

header('content-type: text/html; charset=utf-8');
error_reporting(e_all | e_strict);
ini_set('display_errors', 1);
// display_errors can be changed to 0 in production mode to
// suppress php's error messages

/*
can be used for testing
$_post['id'] = 1;
$_post['name'] = 'markus';
*/

$config = array(
    'host' => '127.0.0.1', 
    'user' => 'my_user', 
    'pass' => 'my_pass', 
    'db' => 'my_database'
);

# connect and disable mysql error output
$connection = @mysql_connect($config['host'], 
    $config['user'], $config['pass']);

if (!$connection) {
    trigger_error('unable to connect to database: ' 
        . mysql_error(), e_user_error);
}

if (!mysql_select_db($config['db'])) {
    trigger_error('unable to select db: ' . mysql_error(), 
        e_user_error);
}

if (!mysql_set_charset('utf8')) {
    trigger_error('unable to set charset for db connection: ' 
        . mysql_error(), e_user_error);
}

$result = mysql_query(
    'update tablename set name = "' 
    . mysql_real_escape_string($_post['name']) 
    . '" where id = "' 
    . mysql_real_escape_string($_post['id']) . '"'
);

if ($result) {
    echo htmlentities($_post['name'], ent_compat, 'utf-8') 
        . ' updated.';
} else {
    trigger_error('unable to update db: ' 
        . mysql_error(), e_user_error);
}
By Bakepi on May 30 2022

Answers related to “reference: what is a perfect code sample using the mysql extension? [closed]”

Only authorized users can answer the Search term. Please sign in first, or register a free account.