All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
基础知识之认识和使用,RSS+asp.net_[Asp.Net教程]
查询同一表内多字段同时重复记录的SQL语句_[SQL Server教程]
PHP判断搜索引擎机器人Robot_[PHP教程]
ASP.NET,2.0角色及成员管理_.net资料_编程技术
C#网络应用编程基础练习题与答案(八)_.net资料_编程技术
asp.net2.0母版页与内容页的创建
用ASP.NET,2.0,FormView控件控制显示_[Asp.Net教程]
技巧:Asp.net直接保存文件到客户端_.net资料_编程技术
Ajax基础必读-AJAX中的一些关键技术_[AJAX教程]
ASP.NET,MVC+LINQ开发一个图书销售站点(10)-作者管理_[Asp.Net教程]
Reading number is top 10 pictures
青涩甜美-王祖贤小时候的旧照片曝光
采访美女孙菲菲
壮丽的云彩1
西游四格漫画(五)
5 meters long centenarians python and melee was successfully capture king snake (figure)
A beautiful girl to bud2
The money of more than 100 countries and regions11
The terra-cotta warriors1
粉红蕾丝的美女
The money of more than 100 countries and regions15
Download software ranking
Unix video tutorial4
The king of fighters 97(Mobile phone games-apk)
Boxer vs Yellow4
Unix video tutorial14
超级战舰
Boxer's Top ten classic battle8
Unix video tutorial12
Boxer vs Yellow2
Professional killers2 data package
都市狐狸姑娘传
aaa published in(发表于) 2013/12/27 19:52:19 Edit(编辑)
PHP与MYSQL交互函数表学习笔记_php资料_编程技术

PHP与MYSQL交互函数表学习笔记_php资料_编程技术

PHP与MYSQL交互函数表学习笔记_php资料_编程技术-你的首页-uuhomepage.com
最近一直在研究PHP与MYSQL,感觉PHP与MYSQL交互的函数都是过程化的,当然也有mysqli扩展,面向对象,Java和C#写多了之后,再写PHP,有些不适应,感觉又回到了学C的年代。今天学习了一些函数,记录下来,以便日后忘记时,可以参考。





































说 明


函 数 说 明
建立数据库连接
mysql_connect()
resource mysql_connect([string hostname [:port] [:/path/to/socket] [, string username] [, string password]])
示例:$conn = @mysql_connect("localhost", "username", "password") or dir("不能连接到Mysql Server");

使用该连接必须显示的关闭连接
建立数据库连接
mysql_pconnect()
resource mysql_pconnect([string hostname [:port] [:/path/to/socket] [, string username] [, string password]])
示例:$conn = @mysql_pconnect("localhost", "username", "password") or dir("不能连接到Mysql Server");

使用该连接函数不需要显示的关闭连接,它相当于使用了连接池
关闭数据库连接
mysql_close()
$conn = @mysql_connect("localhost", "username", "password") or die("不能连接到Mysql Server");
@mysql_select_db("MyDatabase") or die("不能选择这个数据库,或数据库不存在");
echo "你已经连接到MyDatabase数据库";
mysql_close();

 
选择数据库
mysql_select_db()
boolean mysql_select_db(string db_name [, resource link_id])
$conn = @mysql_connect("localhost", "username", "password") or die("不能连接到Mysql Server");
@mysql_select_db("MyDatabase") or die("不能选择这个数据库,或数据库不存在");

 
查询MySQL
mysql_query()
resource mysql_query (string query, [resource link_id])
$linkId = @mysql_connect("localhost", "username", "password") or die("不能连接到Mysql Server");
@mysql_select_db("MyDatabase") or die("不能选择这个数据库,或者数据库不存在");
$query = "select * from MyTable";
$result = mysql_query($query);
mysql_close();

SQL查询执行成功,则返回资源标识符,失败时返回FALSE。若执行更新成功,则返回TRUE,否则返回FALSE
查询MySQL
mysql_db_query()
resource mysql_db_query(string database, string query [, resource link_id])
$linkId = @mysql_connect("localhost", "username", "password") or die("不能连接到MysqlServer");
$query = "select * from MyTable";
$result = mysql_db_query("MyDatabase", $query);
mysql_close();

为了使代码清晰,不推荐使用这个函数调用
获取和显示数据
mysql_result()
mixed mysql_result (resource result_set, int row [, mixed field])
$query = "select id, name from MyTable order by name";
$result = mysql_query($query);
$c_id = mysql_result($result, 0, "id");
$c_name = mysql_result($result, 0, "name");

最简单、也是效率最低的数据获取函数
获取和显示数据
mysql_fetch_row()
array mysql_fetch_row (resource result_set)
$query = "select id, name from MyTable order by name";
$result = mysql_query($query);
while (list($id, $name) = mysql_fetch_row($result)) {
echo("Name: $name ($id)
");
}

函数从result_set中获取整个数据行,将值放在一个索引数组中。通常会结使list()函数使用
获取和显示数据
mysql_fetch_array()
array mysql_fetch_array (resource result_set [, int result_type])
$query = "select id, name from MyTable order by name";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$id = $row["id"];
$name = $row["name"];
echo "Name: $name ($id)
";
}

result_type的值有:
MYSQL_ASSOC: 字段名表示键,字段内容为值
MYSQL_NUM: 数值索引数组,操作与mysql_fetch_ros()函数一样
MYSQL_BOTH: 即作为关联数组又作为数值索引数组返回。result_type的默认值。

获取和显示数据
mysql_fetch_assoc()
array mysql_fetch_assoc (resource result_set)
相当于调用 mysql_fetch_array(resource, MYSQL_ASSOC);

 
获取和显示数据
mysql_fetch_object()
object mysql_fetch_object(resource result_set)
$query = "select id, name from MyTable order by name";
while ($row = mysql_fetch_object($result)) {
$id = $row->id;
$name = $row->name;
echo "Name: $name ($id)
";
}

在操作上与mysql_fetch_array()相同
所选择的记录
mysql_num_rows()
int mysql_num_rows(resource result_set)
#query = "select id, name from MyTable where id > 65";
$result = mysql_query($query);
echo "".mysql_num_rows($result)."条记录的ID大于65";

只在确定select查询所获取的记录数时才有用。
受影响的记录
mysql_affected_rows()
int mysql_affected_rows([resource link_id])
$query = "update MyTable set name='CheneyFu' where id>=5";
$result = mysql_query($query);
echo "ID大于等于5的名称被更新了的记录数:".mysql_affected_rows();

该函数获取受INSERT,UPDATEDELETE更新语句影响的行数
获取数据库列表信息
mysql_list_dbs()
resource mysql_list_dbs([resource link_id])
mysql_connect("localhost", "username", "password");
$dbs = mysql_list_dbs();
echo "Databases:
";
while (list($db) = mysql_fetch_rows($dbs)) {
echo "$db
";
}

 
获取数据库名
mysql_db_name()
string mysql_db_name(resource result_set, integer index)
该函数获取在mysql_list_dbs()所返回result_set中位于指定index索引的数据库名
获取数据库表列表
mysql_list_tables()
resource mysql_list_tables(string database [, resource link_id])
mysql_connect("localhost", "username", "password");
$tables = mysql_list_tables("MyDatabase");
while (list($table) = mysql_fetch_row($tables)) {
echo "$table
";
}

该函数获取database中所有表的表名
获取数据库表名
mysql_tablename()
string mysql_tablename(resource result_set, integer index)
mysql_connect("localhost", "username", "password");
$tables = mysql_list_tables("MyDatabase");
$count = -1;
while (++$count < mysql_numrows($tables)) {
echo mysql_tablename($tables, $count)."
";
}

该函数获取mysql_list_tables()所返回result_set中位于指定index索引的表名
获取字段信息
mysql_fetch_field()
object mysql_fetch_field(resource result [, int field_offset])
mysql_connect("localhost", "username", "password");
mysql_select_db("MyDatabase");
$query = "select * from MyTable";
$result = mysql_query($query);
$fields = mysql_num_fields($result);
for($count = 0; $count < $fieds; $count++) {
$field = mysql_fetch_field($result, $count);
echo "

$field->name $field->type ($field->max_length)

";
}

返回的对象共有12个对象属性:
name: 字段名
table: 字段所在的表
max_length: 字段的最大长度
not_null: 如果字段不能为null,则为1,否则0
primary_key: 如果字段为主键,则为1,否则0
unique_key: 如果字段是唯一键,则为1 否则0
multiple_key: 如果字段为非唯一,则为1,否则0
numeric: 如果字段为数值则为1,否则0
blob: 如果字段为BLOB则为1,否则为0
type: 字段的数据类型
unsigned: 如果字段为无符号数则为1,否则为0
zerofill: 如果字段为“零填充”则为1 否则为0

获取查询的字段数
mysql_num_fields()
integer mysql_num_fields (resource result_set)
$query = "select id, name from MyTable order by name";
$result = mysql_query($query);
echo "这个查询的字段数是:".mysql_num_fields($result)."
";

返回查询result_set中的字段数
获取指定表的所有字段的字段名
mysql_list_fields()
resource mysql_list_fields (string database_name, string table_name [, resource link_id])
$fields = mysql_list_fields("MyDatabase", "MyTable");
echo "数据库MyDatabase中表MyTable的字段数: ".mysql_num_fields($fields)."
";

 
获取指定的字段选项
mysql_field_flags()
string mysql_field_flags (resource result_set, integer field_offset)
 
获取指定的字段的最大长度
mysql_field_len()
integer mysql_field_len (resource result_set, integer field_offset)
$query = "select name from MyTable";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
echo mysql_field_len($result, 0)."
";

如果mysql_field_len($reseult, 0) = 16777215
那么numer_format(mysql_field_len($result))等于16,777,215

获取字段名
mysql_field_name()
string mysql_field_name (resource result_set, int field_offset)
$query = "select id as PKID, name from MyTable order by name";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
echo mysql_field_name($result, 0); // Result: PKID

 
获取字段类型
mysql_field_type()
string mysql_field_type (resource result_set, int field_offset)
$query = "select id, name from MyTable order by name";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
echo mysql_field_type($result, 0); // Result: int

 
获取字段所在表名
mysql_field_table()
string mysql_field_table (resource result_set, int field_offset)
$query = "select id as PKID, name from MyTable order by name";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
echo mysql_field_table($result, 0); //


添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.