Use PHP's mysql_query () function to create a tutorial on Mysql database _mysql

Source: Internet
Author: User
Tags php mysql create database

Taking the mysql_query () function as the basic prerequisite for the tutorial, let's take a look at the use of mysql_query ():
mysql_query () function
PHP MySQL function library, the mysql_query () function is used to send and execute SQL statements to MySQL.
Returns TRUE for SQL that has no data returning result sets, such as UPDATE, DELETE, or FALSE when execution succeeds, returns a resource identifier for a select,show,explain or DESCRIBE statement, and returns F if the query executes incorrectly Alse.
Grammar:

Resource mysql_query (string query [, resource connection])

Parameter description:

Tips
If there is no open connection, this function attempts to call the mysql_connect () function without arguments to establish a connection
For queries that return datasets, returns a resource identifier instead of FALSE, even if the result is 0 (that is, records that do not match the query criteria).
Example 1:

<php
$conn = @mysql_connect ("localhost", "root", "root123");
if (! $conn) {
 die ("Connection Database failed:". Mysql_error ());
}
mysql_select_db ("Test", $conn);
$result = mysql_query ("SELECT * WHERE 1=1")
  or Die ("Invalid query:".) Mysql_error ());
? > 
The example query statement has an error on the SQL syntax, so mysql_query () execution fails and returns FALSE.
Example 2:
<php
$conn = @mysql_connect ("localhost", "root", "root123");
if (! $conn) {
  die ("Connection Database failed:". Mysql_error ());
}

mysql_select_db ("Test", $conn);
mysql_query ("Set names ' GBK '"); In order to avoid the Chinese garbled code to do the conversion
$password = MD5 ("123456")//////////////////////////password
$regdate = time ()  after encryption Get timestamp
$sql = "INSERT into user (username, password, email, regdate) VALUES (' Xiao Wang ', ' $password ', '
 12345@163. com ', $regdate) ';

if (!mysql_query ($sql, $conn)) {
  echo failed to add data:. Mysql_error ();
} else {
  echo "Add Data Success!" ";
}
? > 

This example writes data to the user table, returns TRUE successfully, or returns FALSE (with!). Symbolic judgment).

Create DATABASE creating databases
Creating a Database
The CREATE DATABASE syntax is used for creating a data base.
Grammar:

CREATE DATABASE db_name

PHP MySQL function library, the mysql_query () function is used to send and execute SQL statements to MySQL.
Create a database named TestDB:

<?php
$conn = @mysql_connect ("localhost", "root", "root1234");
if (! $conn) {
  die ("Connection Database failed:". Mysql_error ());
}
if (@mysql_query ("CREATE Database TestDB", $conn)) {
  echo to create a success! ";
} else {
  echo failed to create database:. Mysql_error ();
>

Tips
Creating a database requires corresponding user rights, such as the root user
In the actual virtual host space, the virtual host business has usually created the corresponding database, so the above example does not necessarily run successfully
Select Database
To perform an operation on a database or table, you need to select a database. mysql_select_db () is used to select a database, and if successful, the function returns True and False if it fails.
Grammar:

BOOL mysql_select_db (String db_name [, resource connection])

Parameter description:

See examples of creating data tables below.
Creating data Tables
The SQL syntax for creating data tables is as follows:

CREATE TABLE table_name
(
  column1 data_type,
  column2 data_type,
  column3 data_type,
  ...
)

In the syntax above, column is the field name followed by the data type.
Create a table named User:

<?php
$conn = @mysql_connect ("localhost", "root", "root1234");
if (! $conn) {
  die ("Connection Database failed:". Mysql_error ());
}

Select Database
mysql_select_db ("test", $conn);

Create the datasheet SQL
$sql = "CREATE TABLE User (
uid mediumint (8),
username varchar (),
password char (32)," C20/>email varchar (),
regdate int
) ";

if (!mysql_query ($sql, $conn)) {
  echo created the data table failed:. mysql_error ();
} else {
  echo "create the datasheet successfully!" ";
}
? >

In this example, there are 3 steps to perform:

    1. Create a link to a database
    2. Use the mysql_select_db () function to select the database that holds the table
    3. Create a datasheet using the mysql_query () function

The table created in this example has 4 fields and specifies the corresponding data object type.

Table-Building principles
in general, creating a datasheet has the following considerations:
The corresponding relationship between the original data and the table
table name and field name should follow the naming syntax and should be clear meaning
Specify the data type of the field
Specify other properties such as whether the field is Non-null or has a default value The
Define table properties such as primary foreign keys, constraints, indexes, and so on
relationships with other tables are limited to space and are difficult to control, and there is no discussion here.
Hint
This example is not perfect just to demonstrate the basic syntax for building a table. In actual production, we also need to specify more properties for tables and fields.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.