Wu Yuxiong - natural born PHP Development Learning: Connecting MySQL, create a table

? < PHP
 $ ServerName = "localhost" ;
 $ username = "root" ;
 $ password = "ADMIN" ; 

// create a connection 
$ conn = new new mysqli ( $ ServerName , $ username , $ password ); 

// detect the connection 
IF ( conn $ -> connect_error) {
     Die ( "connection failed:." $ conn -> connect_error); 
} 
echo "successfully connected" ;
 >?

<?php
$servername = "localhost";
$username = "root";
$password = "admin";

try {
    $conn = new PDO("mysql:host=$servername;", $username, $password);
    echo "连接成功";
}
catch(PDOException $e)
{
    echo $e->getMessage();
}
?>

? < PHP
 $ ServerName = "localhost" ;
 $ username = "root" ;
 $ password = "ADMIN" ; 

// create a connection 
$ conn = new new mysqli ( $ ServerName , $ username , $ password );
 // detect the connection 
IF ( Conn $ -> connect_error) {
     Die ( "connection failed:." $ Conn -> connect_error); 
} 

// Create database 
$ SQL = "myDB the CREATE dATABASE" ;
 IF ( $ Conn ->query(SQL $ ) === TRUE ) {
     echo "create database successfully" ; 
} the else {
     echo "Error Creating Database:." $ conn -> error; 
} 

$ conn -> use Close ();
 >?

// create a connection 
$ Conn = mysqli_connect ( $ ServerName , $ username , $ password );
 // detect a connection 
IF (! $ Conn ) {
     Die ( "connection failed:." Mysqli_connect_error ()); 
} 
 
// Create database 
$ sql = "myDB the CREATE dATABASE" ;
 IF ( the mysqli_query ( $ Conn , $ SQL )) {
     echo "database successfully created" ; 
} the else {
     echo "Error creating database:."mysqli_error($conn);
}
 
mysqli_close($conn);
?>
the try { 
     $ Conn = new new PDO ( "MySQL: Host = $ ServerName ", $ username , $ password ); 

    // set the mode to an abnormal error PDO 
    $ Conn -> the setAttribute (PDO :: ATTR_ERRMODE, PDO :: ERRMODE_EXCEPTION); 
     $ SQL = "myDBPDO the CREATE dATABASE" ; 

    // use exec (), returns no result because 
    $ Conn -> Exec ( $ SQL ); 

    echo "database successfully created <br>" ; 
} 
the catch (PDOException $ E ) 
{ 
    echo  $ SQL . "<br>".$e->getMessage(); 
} 

$conn = null; 
?>
? < PHP
 $ ServerName = "localhost" ;
 $ username = "root" ;
 $ password = "ADMIN" ;
 $ dbname = "myDB" ; 

// create a connection 
$ conn = new new mysqli ( $ ServerName , $ username , $ password , dbname $ );
 // detect a connection 
IF ( $ Conn -> connect_error) {
     Die ( "connection failed:." $ Conn -> connect_error); 
} 

// used to create a data table sql 
$ sql = "CREATE TABLE MyGuests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)";

if ($conn->query($sql) === TRUE) {
    echo "Table MyGuests created successfully";
} else {
    echo "创建数据表错误: " . $conn->error;
}

$conn->close();
?>

// create a connection 
$ Conn = mysqli_connect ( $ ServerName , $ username , $ password , $ dbname );
 // detect a connection 
IF (! $ Conn ) {
     Die ( "connection failed:." Mysqli_connect_error ()); 
} 
 
// use sql create a data table 
$ sql = " the CREATE tABLE MyGuests ( 
ID the INT (. 6) UNSIGNED the AUTO_INCREMENT a PRIMARY KEY, 
FirstName VARCHAR (30) the NOT NULL, 
LastName VARCHAR (30) the NOT NULL, 
In Email VARCHAR (50), 
reg_date TIMESTAMP 
) " ; 
 
IF( Mysqli_query ( $ conn , $ SQL )) {
     echo "Data table MyGuests successfully created" ; 
} the else {
     echo "Create a data table error:." Mysqli_error ( $ conn ); 
} 
 
mysqli_close ( $ conn );
 >?
? < PHP
 $ ServerName = "localhost" ;
 $ username = "username" ;
 $ password = "password" ;
 $ dbname = "myDBPDO" ; 
 
the try {
     $ conn = new new PDO ( "MySQL: Host = $ ServerName ; dbname = $ dbname ", $ username , $ password );
     // set the PDO error mode, an exception is thrown for 
    $ conn -> setAttribute (PDO :: ATTR_ERRMODE, PDO :: ERRMODE_EXCEPTION); 
 
    // use sql create a data table 
    $ sql =" CREATE TABLE MyGuests (
    the INT ID (. 6) UNSIGNED the AUTO_INCREMENT a PRIMARY KEY, 
    FirstName VARCHAR (30) the NOT NULL, 
    LastName VARCHAR (30) the NOT NULL, 
    In Email VARCHAR (50), 
    reg_date TIMESTAMP 
    ) " ; 
 
    // use exec (), returns no result 
    $ Conn - > Exec ( $ SQL );
     echo "data table MyGuests successfully created" ; 
} 
the catch (PDOException $ E ) 
{ 
    echo  $ SQL "<br>.". $ E -> getMessage (); 
} 
 
$ conn = null ;
 >?

 

Guess you like

Origin www.cnblogs.com/tszr/p/10955464.html