Spaz
Spaz

Reputation: 3

mysql syntax for a beginner

Ok trying to get into mysql a little bit and I need something explained because the tutorial (w3schools) I'm reading doesn't explain nor does googling it turn up anything.

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

// sql to create table
$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 "Error creating table: " . $conn->error;
}

$conn->close();
?>

Upvotes: 0

Views: 39

Answers (2)

kasijus
kasijus

Reputation: 334

Actually, w3 does explain. Here are all Mysql types:

http://www.w3schools.com/sql/sql_datatypes.asp

Where you can see that int(6) means integer type with max 6 digits, and varchar(30) is string type with max 30 characters.

$conn->query is creating the table. For more information on what 'query' function does, you can just simply google 'mysqli query' and the first result will be this:

http://php.net/manual/en/mysqli.query.php

As you can see, the return value can be FALSE on failure, or a truthy value, depending on the query type:

Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.

Upvotes: 1

The 6 and the 30, are the "length" of that variable, int(6) have a range from 0 to 2^6 since its unsigned, in the varchar, is the max number of characteres it can have. Conn-Query its the command to execute the Query.

Upvotes: 0

Related Questions