PHP Basic Tutorial--5 Database Summary _php Tutorial

Source: Internet
Author: User

PHP Basic Tutorial--5 Database Summary


1. Connection to the database

$DBC = mysql_connect (hosetname, username, password);

2.Mysql Error Handling

Mysql_error (); Detailed reports showing errors

3. Create and select a database

Created: mysql_query (' Create DATABASE somedb ');

Checked: mysql_select_db (' somedb '); Select the database every time you run the query

4. Create a table

$query = ' Create TABLE my_table (id INT PRIMARY KEY, information TEXT); Assign create statement to a variable first

mysql_query ($query);//Add the variable into the mysql_query () function

5. Inserting data

With the CREATE table, each query is assigned a variable, and then the variable is passed to the mysql_query () function:

$query = "INSERT into entries (entry_id, title, entry, data_entered) VALUES (0, ' title ', ' $entry ', Now ())";

mysql_query ($query);

6. Secure query data

For a query entered by the user, use Mysql_real_escape_string ($var) to escape potentially dangerous characters such as single quotes (a backslash will be preceded)

7. Retrieving data from a database

You need to copy the results of the query to a variable:
$query = ' SELECT ' from the users WHERE (name = myname) ';

$result = MySQL ($query);

8. Delete Data

$query = ' DELETE from users WHERE name = myname LIMIT 1 ';

$result = MySQL ($query);

9. Update

$query = UPDATE tablename SET column1 = value, Colunmn2 = value where some_column = value ';

$result = MySQL ($query);

Coding test: ws.php

   
 
  
  <title>Test</title>   
 
Connect to the database and check if ($dbc = @mysql_connect (' localhost ', ' root ', ') ') {if (@mysql_select_db (' MyData ')) {print '

selected!

';} Else{print '

Can not select error: '. Mysql_error (). '

';}} Else{print '

Can not connect. Error: '. Mysql_error (). '

';}
CREATE TABLE/* $create = ' CREATE TABLE myTable (ID INT UNSIGNED NOT NULL auto_increment PRIMARY key,name VARCHAR (+) NOT null) ';p R int "

Create ...

"If (@mysql_query ($create)) {print '

created!

';} else {print '

Can not create error: '. Mysql_error (). '

';} Mysql_close (); */
Insert Data $insert = ' INSERT into myTable (ID, name) VALUES (12345, "Charles") '; if (@mysql_query ($insert)) {print '

inserted!

';} else {print '

Can not insert error: '. Mysql_error (). '

';} Mysql_close ();? >

This is the foot of the document


The results show:


http://www.bkjia.com/PHPjc/953325.html www.bkjia.com true http://www.bkjia.com/PHPjc/953325.html techarticle PHP Basic Tutorial--5 Database Summary 1. database connection $dbc = mysql_connect (hosetname, username, password); 2.Mysql error handling Mysql_error (); Detailed report showing error 3. Create ...

  • 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.