[ create a new paste ] login | about

Link: http://codepad.org/bncGpPJQ    [ raw code | fork ]

PHP, pasted on Mar 11:
<?php
$host = "localhost";
$username = "user";
$password = "pass";
$dbname = "test";
$table = "so_15343688_test";

function connect_db($host, $user, $pass, $dbname) {
    $dbh = mysql_connect($host, $user, $pass)
        or die("mysql_connect failed: " . mysql_error());

    mysql_select_db($dbname, $dbh)
        or die("mysql select $dbname failed: " . mysql_error());

    return $dbh;
}

function initialize($table, $dbh) {
    mysql_query("CREATE TABLE IF NOT EXISTS $table (
        id INT PRIMARY KEY AUTO_INCREMENT,
        name VARCHAR(30) NOT NULL DEFAULT '')", $dbh)
        or die("could not create table: " . mysql_error());

    $res = mysql_query("SELECT COUNT(*) FROM $table");
    if($res && $row = mysql_fetch_row($res) && $row[0] == 0) {
        for($i = 0; $i < 10; ++$i) {
            mysql_query("INSERT INTO $table (name) values ('"
                . mysql_real_escape_string("test $i", $dbh)
                . "')",
                $dbh);
        }
    }
}


function test_function($query_function, $table, $dbh) {
    $res = $query_function("SELECT * FROM $table");

    $first = true;
    while($row = mysql_fetch_row($res)) {
        if($first) {
            $first = false;
            mysql_close($dbh);
        }
        echo $row[0], " - ", $row[1], PHP_EOL;
    }
    mysql_free_result($res);
}

// connect to the db and create some test data.
$dbh = connect_db($host, $username, $password, $dbname);
initialize($table, $dbh);

// everything is peachy
test_function("mysql_query", $table, $dbh);

// reconnect
$dbh = connect_db($host, $username, $password, $dbname);

//oops
test_function("mysql_unbuffered_query", $table, $dbh);


Create a new paste based on this one


Comments: