PHP MySQL Database link Simple example

Source: Internet
Author: User
Tags php mysql

Here is the process-oriented MySQL link, two files, config.php and conn.php, just record, do not like to spray, but also want to have good suggestions, the code is as follows:

config.php

<?php
/*
* The configuration variables related to database links are stored in this file, which facilitates later development of the changes
* This is only part of the configuration
* **/
$hostname = ' localhost ';
$username = ' root ';
$password = ";
$database = ' AA ';
?>

conn.php

<?php
/* This is mysql_connect process-oriented link, just write a box, the details will be added later **/
Require ("config.php");//path to correct

[Email Protected]_connect ($hostname, $username, $password) or Die ("link error");
mysql_query ("Set names ' UTF8 '"); Set encoding
mysql_select_db ($database); Select Database
$sql = "SELECT * from user";
$res = mysql_query ($sql, $conn); Execute SQL statement

/* Loop through the data **/
while ($row = Mysql_fetch_array ($res)) {
echo $row [' name '];
}

/* Write Data **/
$sql 1 = "INSERT into user (name) values (' Longo ')";
mysql_query ($sql 1);

/* Update data name and age**/
$sql 2= "Update user set Name= ' Longo ', age= ' 100 '";
$result =mysql_query ($sql 2);
if ($result) {echo "succeeded";} else {echo "failed";}

Mysql_freeresult ($res);
Mysql_close ();
?>

PHP MySQL Database link Simple example

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.