Home  >  Article  >  Backend Development  >  php连接数据库代码应用分析_php基础

php连接数据库代码应用分析_php基础

WBOY
WBOYOriginal
2016-05-17 09:18:571065browse

复制代码 代码如下:

$db_host='hostname is database server ';
$db_database='database name';
$db_username='username';
$db_password='password';
$connection=mysql_connect($db_host,$db_username,$db_password);//连接到数据库
mysql_query("set names 'utf8'");//编码转化
if(!$connection){
die("could not connect to the database:".mysql_error());//诊断连接错误
}
$db_selecct=mysql_select_db($db_database);//选择数据库
if(!$db_selecct)
{
die("could not to the database".mysql_error());
}
$query="select * from msu ";//构建查询语句
$result=mysql_query($query);//执行查询
if(!$result)
{
die("could not to the database".mysql_error());
}
// array mysql_fetch_row(resource $result);
while($result_row=mysql_fetch_row(($result)))//取出结果并显示
{
$num=$result_row[0];
$age=$result_row[1];
$name=$result_row[2];
echo "";
echo "$num";
echo "$age";
echo "$name";
echo "";
}
mysql_close($connection);//关闭连接
?>

下面是带详细说明的代码
复制代码 代码如下:

$connect = mysql_connect("127.0.0.1","root","") or die ("链接错误");//开启链接到mysql
$select_db = mysql_select_db("数据库名",$connect);//如果不特别声明连接标示符,则默认上一次打开的链接
//执行SQL语句!
$sql = "SELECT * FROM test"
$query = mysql_query($sql,$connect) or die(mysql_error());

//两种查询函数array/row区别
$row1 = mysql_fetch_row($query);
print_r($row1);//只能保存数组中的标号
$row2 = mysql_fetch_array($query);
print_r($row2);//可以保存数组中的标号,和字段名
//循环输出while输出到空位置
while($row1){
print_r($row1);
}

mysql_query("SET NAMES 'UTF-8'");

//用于计算查询结果的数目
mysql_num_rows($query);
//传回最后一次使用INSERT指令的IP
mysql_insert_id($query);
//取得数据库名
mysql_tablename($query);
//返回错误信息
mysql_error();
//关闭链接
mysql_close();

PHP连接mySQL常用函数
Mysql可通过两种方式通过PHP与web相连,一种通过php的Mysql相关函数,另一种通过php的ODBC相关函数。
相关函数如下:
MYSQL函数
mysql_affected_rows: 得到 MySQL 最后操作影响的列数目。
mysql_close: 关闭 MySQL 伺服器连线。
mysql_connect: 开启 MySQL 伺服器连线。
mysql_create_db: 建立一个 MySQL 新资料库。
mysql_data_seek: 移动内部传回指标。
mysql_db_query: 送查询字串 (query) 到 MySQL 资料库。
mysql_drop_db: 移除资料库。
mysql_errno: 传回错误讯息代码。
mysql_error: 传回错误讯息。
mysql_fetch_array: 传回阵列资料。
mysql_fetch_field: 取得栏位资讯。
mysql_fetch_lengths: 传回单列各栏资料最大长度。
mysql_fetch_object: 传回物件资料。
mysql_fetch_row: 传回单列的各栏位。
mysql_field_name: 传回指定栏位的名称。
mysql_field_seek: 设定指标到传回值的某栏位。
mysql_field_table: 获得目前栏位的资料表 (table) 名称。
mysql_field_type: 获得目前栏位的型态。
mysql_field_flags: 获得目前栏位的旗标。
mysql_field_len: 获得目前栏位的长度。
mysql_free_result: 释放传回占用记忆体。
mysql_insert_id: 传回最后一次使用 INSERT 指令的 ID。
mysql_list_fields: 列出指定资料表的栏位 (field)。
mysql_list_dbs: 列出 MySQL 伺服器可用的资料库 (database)。
mysql_list_tables: 列出指定资料库的资料表 (table)。
mysql_num_fields: 取得传回栏位的数目。
mysql_num_rows: 取得传回列的数目。
mysql_pconnect: 开启 MySQL 伺服器长期连线。
mysql_query: 送出一个 query 字串。
mysql_result: 取得查询 (query) 的结果。
mysql_select_db: 选择一个资料库。
mysql_tablename: 取得资料表名称。
ODBC函数
使用ODBC函数需安装MYSQL ODBC
odbc_autocommit: 开关自动更动功能。
odbc_binmode: 设定二进位资料处理方式。
odbc_close: 关闭 ODBC 连结。
odbc_close_all: 关闭所有 ODBC 连结。
odbc_commit: 更动 ODBC 资料库。
odbc_connect: 连结至 ODBC 资料库。
odbc_cursor: 取得游标名。
odbc_do: 执行 SQL 指令。
odbc_exec: 执行 SQL 指令。
odbc_execute: 执行预置 SQL 指令。
odbc_fetch_into: 取得传回的指定列。
odbc_fetch_row: 取得传回一列。
odbc_field_name: 取得栏位名称。
odbc_field_type: 取得栏位资料形态。
odbc_field_len: 取得栏位资料长度。
odbc_free_result: 释出传回资料的记忆体。
odbc_longreadlen: 设定传回栏的最大值。
odbc_num_fields: 取得栏位数目。
odbc_pconnect: 长期连结至 ODBC 资料库。
odbc_prepare: 预置 SQL 指令。
odbc_num_rows: 取得传回列数目。
odbc_result: 取得传回资料。
odbc_result_all: 传回 HTML 表格资料。
odbc_rollback: 撤消当前交易。
odbc_setoption: 调整 ODBC 设定。
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn