PHP与Mysql的交互

 

一 连接数据库与断开数据库

1 连接:int mysql_connect("hostname[:port][:/path/to/socket]","username","password")
还可以用[:/path/to/socket]形式来连接
        int mysql_pconnect("hostname","username","password")

2 断开:int mysql_close(int[link_identifier])
如果操作成功,则这个函数返回true,否则返回flase,因为用mysql_connect()建立的连接在脚本结

束之前,被系统自动关闭,所以这个函数用于中途关闭连接。mysql_pconnect()建立的连接,这个函

数是不起作用的。
<?php
$link=mysql_connect('localhost','root','');
if(!$link) echo "失败!";
else echo "成功!";
@mysql_select_db( "phpfirst") or die("Unable to select data");
$close = mysql_close($link);
echo $close;
?>

二 库级操作

int mysql_create_db(string database_name, int [link_identifier])
int mysql_select_db(string database_name, int [link_identifier])
int mysql_drop_db(string database_name, int [link_identifier])
在参数列表中,database_name是数据库的名字,link_identifier为由mysql_connect()返回的连接

标示号,如果不使用这个参数,系统会试图使用上次连接的连接号,如果根本没有打开过任何连接,

系统还会先建立一个连接(使用空参数列表的mysql_connect())。

<?
//连接到数据库服务器
$connection = mysql_connect("localhost","root","") or die("fail");
//建立一个连接
$create_db = mysql_create_db("testdb",$connection);
if(!$create_db)
{
   echo "Unable to create the database <br>";
}
else
{
   echo "Create the database succeded <br>";
   //选择一个数据库
   $select_db = mysql_select_db("testdb",$connection);
   if($select_db)
   {
      echo "select database succeded <br>";
   }
   else
   {
      echo "Unable to select the database <br>";
   }
   //删除一个数据库
   $drop_db = mysql_drop_db("testdb",$connection);
   if($drop_db)
   {
       echo "Drop database succeded! <br>";
   }
   else
   {
      echo "Unable to drop the database!<br>";
   }
}
?>

 

三 SQL查询请求

int mysql_query(string query,int[link_identifier]);
int mysql_db_query(string database,string query,int[link_identifier]);

mysql_query()需要先打开一个数据库,在对该数据库进行操作。
mysql_db_query()必须在参数表中指定操作的数据库,系统会以该数据库作为操作对象。
这2个函数只返回查询是否成功,要查看返回结果,则需要使用其它函数。

四 获取查询结果信息

1  string mysql_result(integer result,integer row,mixed[field]);
说明:先是用mysql_query()取得result 0或者1,row是行号,mixed是列名

<?
$connection = mysql_connect('localhost','root','password');
mysql_select_db('phpfirst');
$query = "select * from friend";
$result = mysql_query($query,$connection);
for($count = 0;$count < 4; $count++)
{
  echo mysql_result($result,$count,"name")."<br>";//取第count行的name列的数据
  echo mysql_result($result,$count,"address")."<br>";//取第count行的address列的数据
  echo mysql_result($result,$count,"email")."<br>";//取第count行的email列的数据
}
?>

2 array mysql_fetch_row(integer result);
返回一个数组,该数组保存了结果数据中当前行的数据信息。

<?
$connection = mysql_connect('localhost','root','password');
mysql_select_db('phpfirst');
$query = "select * from friend";
$result = mysql_query($query,$connection);
for($row = mysql_fetch_row($result))
{
  echo $row[0]."<br>";
  echo $row[1]."<br>";
  echo $row[2]."<br>";

}
?>


3 array mysql_fetch_array(integer result,integer[result_type]);
返回一个数组,
<?
$connection = mysql_connect('localhost','root','password');
mysql_select_db('phpfirst');
$query = "select * from friend";
$result = mysql_query($query,$connection);
for($row = mysql_fetch_array($result))
{
  echo $row["name"]."<br>";
  echo $row["address"]."<br>";
  echo $row["email"]."<br>";
}
?>


4 array mysql_fetch_object(integer result,integer[result_type]);
返回一个对象,该对象中的成员变量就是各个列的信息。访问每个列的数据就像访问对象属性
一样访问
<?
$connection = mysql_connect('localhost','root','password');
mysql_select_db('phpfirst');
$query = "select * from friend";
$result = mysql_query($query,$connection);
for($row = mysql_fetch_object($result))
{
  echo $row->name."<br>";
  echo $row->address."<br>";
  echo $row->email."<br>";
}
?>

五 获取结果中的字段信息

1 integer mysql_field_len(integer result, integer field_offset)
  返回指定字段中的最大长度

2 string mysql_field_name(integer result, integer field_index)
  返回指定字段的名字

3 string mysql_field_table(integer result, integer field_offset)
  返回指定字段所在的表名

4 string mysql_field_type(integer result, integer field_offset)
  返回mysql中定义的类型,字段的类型

<?
$connection = mysql_connect('localhost','root','password');
mysql_select_db('phpfirst');
$query = "select * from friend";
$result = mysql_query($query,$connection);
$fields = mysql_num_fields($result);//获取结果中的字段数
$i = 0;
$table = mysql_field_table($result,$i);
echo "The $table table has the following fields<br>";
while($i<$fields)
{
  //获取字段的名称
  $name = mysql_field_name($result,$i);
  //获取字段的类型
  $type = mysql_field_type($result,$i);
  //获取字段的长度
  $len = mysql_field_len($result, $i);
  //获取字段的标志 主键
  $flags = mysql_field_flags($result,$i);
  //输出结果数据
  echo $name." ".$type." ".$len." ".$flags."<br>";
  $i++;
}
?>

5 array mysql_fetch_lengths(integer result)
这个函数取回输出结果时,每个字段的长度。mysql_fetch_row(), mysql_fetch_array()

mysql_fetch_object()取回行中的字段。
<?
$connection = mysql_connect('localhost','root','password');
mysql_select_db('phpfirst',$connection);
$query = "select * from friend";
$result = mysql_query($query,$connection);
while($row = mysql_fetch_array($result))
{
  $lengths = mysql_fetch_lengths($result);
  echo "[".$row[id]"]";
  echo $lengths[0]." ";
  echo $lengths[1]." ";
  echo $lengths[2]." ";
  echo $lengths[3]."<br>";
}
?>

6 object mysql_fetch_field(integer result, integer[field_offset])
  该函数返回一个对象,详细地描述了指定字段的属性。

六 获取结果中的数目信息
1 integer mysql_num_fields(integer result)
查出来的结果中有几个列
2 integer mysql_num_rows(integer result)
查出来的结果中有几个行
3 integer mysql_affected_rows(integer[link_identifier])
上一次对数据库操作行的总数

七 查看数据库信息

1 int mysql_list_field(string database_name,string table_name,int[link_identifier])
指定数据库,数据表的字段列表 返回标示号给mysql_tablename()处理
2 int mysql_list_table(string database, integer[link_identifier])
指定数据库的数据表列表 返回标示号给mysql_tablename()处理
3 integer mysql_list_dbs(integer[link_identifier])
指定连接下的数据库列表 返回标示号给mysql_tablename()处理
4 string mysql_tablename(integer result,integer i)
接受上面3个函数的返回值,返回相应的字段名,数据库名,数据表名

八 对出错的处理

1 integer mysql_errno(integer[link_identifier])
//获得错误的代号
2 string mysql_error(integer[link_identifier])
//获得错误的文本提示
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值