<?php
//     include "connect.inc";

    $dbname = 'test';    
    $tmp    = NULL;   
    $link   = NULL;    
$user = "root";
$passwd = "root";
$dbname = "test";
$port = 3306;
$socket = null;
$host = "127.0.0.1";
$engine = "MyISAM";
    
       
    if (!$link = mysqli_connect($host, $user, $passwd, $dbname)) {
        printf("Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
            $host, $user, $dbname, $port, $socket);
        exit(1);
    }  
    $stmt = mysqli_stmt_init($link);
    
    if (!mysqli_stmt_prepare($stmt, 'DROP TABLE IF EXISTS test') ||
        !mysqli_stmt_execute($stmt)) {
        printf("[001] Failed to drop old test table: [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
        exit(1);
    }        
        
    if (!mysqli_stmt_prepare($stmt, 'CREATE TABLE test(id INT, label CHAR(1), PRIMARY KEY(id)) ENGINE = ' . $engine) ||
        !mysqli_stmt_execute($stmt)) {
        printf("[002] Failed to create test table: [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
        exit(1);
    }
    
    mysqli_stmt_close($stmt);
    $stmt = mysqli_stmt_init($link);

    if (!mysqli_stmt_prepare($stmt, 'INSERT INTO test(id, label) VALUES (1, "a")') || 
        !mysqli_stmt_execute($stmt))
        printf("[003] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
           
    mysqli_stmt_close($stmt);
    $stmt = mysqli_stmt_init($link);      
        
    if (!mysqli_stmt_prepare($stmt, 'INSERT INTO test(id, label) VALUES (1, "a")') || 
        !mysqli_stmt_execute($stmt))
        printf("[004] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));        

    if (-1 !== ($tmp = mysqli_stmt_affected_rows($stmt)))
        printf("[005] Expecting int/-1, got %s/%s\n", gettype($tmp), $tmp);
        
          
    mysqli_stmt_close($stmt);    

    mysqli_query($link, 'DROP TABLE IF EXISTS test');
    mysqli_query($link, 'CREATE TABLE test(id INT, label CHAR(1), PRIMARY KEY(id)) ENGINE = ' . $engine);
    mysqli_query($link, "INSERT INTO test(id, label) VALUES (1, 'a')");

    if (1 !== ($tmp = mysqli_affected_rows($link)))
        printf("[006] Expecting int/1, got %s/%s\n", gettype($tmp), $tmp);

    mysqli_query($link, "INSERT INTO test(id, label) VALUES (1, 'a')");
    if (-1 !== ($tmp = mysqli_affected_rows($link)))
        printf("[007] Expecting int/-1, got %s/%s\n", gettype($tmp), $tmp);

    mysqli_close($link);
    
    print "done!";
?> 