码迷,mamicode.com
首页 > 数据库 > 详细

PHP与MySql

时间:2014-07-10 16:09:37      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   使用   数据   width   

一:链接数据库

  mysql_connect("localhost","root","123456") or die("连接数据库失败");

  mysql_pconnect();//持久链接

二:选择数据库:

  mysql_select_db("mysqldb") or die("数据库选择失败");

三:拼接数据库

  $sql="select * from ...";

四:执行数据库:

  $result=$mysql_query($sql);

五:判断是否错误:

  //1.mysql_errno(); 错误号

  //2.mysql_error(); 错误信息

  if(!result){

    echo "1.错误号,2.错误信息";

    exit;

  }

六:从结果集中将记录取出:

  1.非select:

    a.获取自动增长的行数:

      mysql_insert_id(); //insert into (只有在有自动增长的表中使用)

    b.获取影响的行数:

      mysql_affected_rows();//可以判断记录是否改变,改变有值,不改变没有值

      if(mysql_affected_rows() > 0){

        ehco "执行成功<br>";

      }else{

        echo "没有改变记录<br>";

      }

  2.select:从结果集中将记录取出:

  mysql_fetch_row($result);      //返回索引数组

  mysql_fetch_assoc($result);      //返回关联数组(下标:就是列表)

  mysql_fetch_array($result);    //返回索引和关联两个数组

  mysql_fetch_object($result);    //将一条记录以对象的形式返回
  while($data=mysql_fetch_row($result)){

    print_r($data);

      echo "<br>";

  }

  while($data=mysql_fetch_assoc($result)){

    print_r($data);

    echo "<br>";

  }

  mysql_data_seek($result,3);  //移动指针,从第三个开始找;

  while($data=mysql_fetch_object($result)){

    print_r($data);

    echo "<br>";

  }
  echo "<table align=‘center‘ width=‘800‘ border=‘1‘>";

  echo "<caption><h1>演示表</h1></caption>";  

      while($row=mysql_fetch_assoc($result)){
          echo "<tr>";
          foreach($row as $val){
          echo "<td>".$val."</td>";
          echo "</tr>";
      }

   echo "</table>";

  3.获取字段信息:

    a.获取列的信息:

    mysql_num_fields($result);  //列数;

    mysql_num_rows($result);  //行数;

    mysql_field_name($result,$i); //获取数据字段的名称;

    $result:  结果集

    $i    : 第几列

 

八:释放结果集:

  mysql_free_resutl($result);
    $link=mysql_connect("localhost","root","") or die("没有值选择");
    mysql_select_db("sqldb") or die("没有你要选择的数据库");
    $sql="select * from products";
    $result=mysql_query($sql);
    if(!$result){
        echo mysql_errno()."<br>".mysql_error();
    }
    echo "<table align=‘center‘ border=‘1‘>";
    for($i=0;$i<mysql_num_rows($result);$i++){
        echo "<th>".@mysql_field_name($result,$i)."</th>";
    }
    while($row=mysql_fetch_assoc($result)){
        echo "<tr>";
        foreach($row as $val){
         echo "<td>".$val."</td>";
        }
        echo "</tr>";
    }
    echo "<td colspan=‘7‘ align=‘right‘>列:".mysql_num_fields($result)."行:".mysql_num_rows($result)."</td>";
    echo "<table>";

 

九:关闭连接:

  close()

PHP与MySql,布布扣,bubuko.com

PHP与MySql

标签:style   blog   color   使用   数据   width   

原文地址:http://www.cnblogs.com/subtract/p/3834928.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!