范例
php 手册 | 脚本学堂 | 脚本编程 | 网站编程 | 系统管理 | 服务器配置 | 数据库管理 | Php教程 | python教程 | 正则表达式 | 批处理脚本 | Centos教程 | Linux基础教程

This simple example shows how to connect, execute a query, print resulting rows and disconnect from a MySQL database.

Example #1 MySQL extension overview example

<?php
// Connecting, selecting database
$link mysql_connect('mysql_host''mysql_user''mysql_password')
    or die(
'Could not connect: ' mysql_error());
echo 
'Connected successfully';
mysql_select_db('my_database') or die('Could not select database');

// Performing SQL query
$query 'SELECT * FROM my_table';
$result mysql_query($query) or die('Query failed: ' mysql_error());

// Printing results in HTML
echo "<table>\n";
while (
$line mysql_fetch_array($resultMYSQL_ASSOC)) {
    echo 
"\t<tr>\n";
    foreach (
$line as $col_value) {
        echo 
"\t\t<td>$col_value</td>\n";
    }
    echo 
"\t</tr>\n";
}
echo 
"</table>\n";

// Free resultset
mysql_free_result($result);

// Closing connection
mysql_close($link);
?>


范例
php 手册 | 脚本学堂 | 脚本编程 | 网站编程 | 系统管理 | 服务器配置 | 数据库管理 | Php教程 | python教程 | 正则表达式 | 批处理脚本 | Centos教程 | Linux基础教程