Search

 
 
 
 
Previous article Next article Go to back
       

PHP. MYSQLi metodai

1. sukuriam DB:
CREATE DABABASE `mano`

Naudojam duomenų bazės duomenis apie darbuotojus. Juos importuojam: Mysql. Sudėtinės užklausos, sub užklausos 

PHP:
<?php

$mysqli = new mysqli('localhost', 'root', '', 'mano');

if ($mysqli->connect_error) {
    die('Connect Error (' . $mysqli->connect_errno . ') '
            . $mysqli->connect_error);
}

echo 'Success... ' . $mysqli->host_info . "\n";

if ($result = $mysqli->query("SELECT * FROM `darbai` LIMIT 10")) {
    printf("<br />Select returned %d rows.\n", $result->num_rows);

    while ($row = $result->fetch_assoc()) {
        printf ("<br />%s %s ", $row["id_parent"], $row["id"]);
    }
    

    $finfo = $result->fetch_field_direct(1);

    printf("<br /><br />Name:     %s\n", $finfo->name);
    printf("<br />Table:    %s\n", $finfo->table);
    printf("<br />max. Len: %d\n", $finfo->max_length);
    printf("<br />Flags:    %d\n", $finfo->flags);
    printf("<br />Type:     %d\n", $finfo->type);

    $field_cnt = $result->field_count;

    printf("<br /><br />Result set has %d fields.\n", $field_cnt);
    
    
    /* free result set */
    
    $result->close();
} // SELECT

$sql = "DELETE FROM MyGuests WHERE id=3";

if ($mysqli->query($sql) === TRUE) {

    echo "<br />Record deleted successfully";

} else {

    echo "<br />Error deleting record: " . $mysqli->error;

}// DELETE


$mysqli->close();

?>

2.
i -  integer
d - double
s - string
b - blob and will be sent in packets
<?php
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'world');

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$stmt = $mysqli->prepare("INSERT INTO CountryLanguage VALUES (?, ?, ?, ?)");
$stmt->bind_param('sssd', $code, $language, $official, $percent);

$code = 'DEU';
$language = 'Bavarian';
$official = "F";
$percent = 11.2;

/* execute prepared statement */
$stmt->execute();

printf("%d Row inserted.\n", $stmt->affected_rows);

/* close statement and connection */
$stmt->close();

/* Clean up table CountryLanguage */
$mysqli->query("DELETE FROM CountryLanguage WHERE Language='Bavarian'");
printf("%d Row deleted.\n", $mysqli->affected_rows);

/* close connection */
$mysqli->close();
?>

3.
<?php

$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$query  = "SELECT CURRENT_USER();";
$query .= "SELECT Name FROM City ORDER BY ID LIMIT 20, 5";

/* execute multi query */
if ($mysqli->multi_query($query)) {
    do {
        /* store first result set */
        if ($result = $mysqli->store_result()) {
            while ($row = $result->fetch_row()) {
                printf("%s\n", $row[0]);
            }
            $result->free();
        }
        /* print divider */
        if ($mysqli->more_results()) {
            printf("-----------------\n");
        }
    } while ($mysqli->next_result());
}

/* close connection */
$mysqli->close();

?>

--
Previous article Next article Go to back
.
To top
Facebook
Share