数据库操作

来源:互联网 发布:js判断是否离开页面 编辑:程序博客网 时间:2024/06/06 02:16

链接数据库

header("Content-type: text/html; charset=utf-8"); $conn=@mysql_connect("localhost","root","") or die ("链接失败");mysql_select_db("dllo",$conn);mysql_query("set names utf8");

常用API
$_SERVER[‘PHP_SELF’] 本页
file_get_contents 抓取网页数据

mysql_num_rows 取得结果集中行的数目
mysql_num_fields 取得结果集中列的数目

//print_r(mysql_fetch_row($res));      //索引数组//print_r(mysql_fetch_array($res));    //都有//print_r(mysql_fetch_assoc($res));    //关联数组

mysql_affected_rows 前一次 MySQL 操作所影响的记录行数

mysql_query('DELETE FROM mytable WHERE id < 10');printf("Records deleted: %d\n", mysql_affected_rows());

mysql_close($dbc); 关闭数据库

操作数据库

include"conn.php";$sql="select * from message order by id desc";$res=mysql_query($sql,$conn);$colums=mysql_num_fields($res);//获取列数     //mysql_fetch_array 得到“关联数组”和“索引数组”    while($row = mysql_fetch_array($result))    {        echo "<tr>";        echo "<td>" . $row['id'] . "</td>";        echo "<td>" . $row['name'] . "</td>";        echo "<td>" . $row['url'] . "</td>";        echo "<td>" . $row['alexa'] . "</td>";        echo "<td>" . $row['country'] . "</td>";        echo "</tr>";    }    while($row=mysql_fetch_array($result)){                   for($i=0; $i<$colums; $i++){            echo "<td>$row[$i]</td>";        }        }        ----------//从结果集中取得一行作为关联数组    while ($row = mysql_fetch_assoc($result)){ //索引数组        echo "<tr>";          foreach ($row as $value){ //遍历            echo "<td>$value</td>";        }        echo "</tr>";     }----------echo "<table class='table_message'>;";     for($i=1; $i < $colums; $i++){       $field_name=mysql_field_name($res,$i);//数据库的标题:      echo "<th>$field_name</th>";    }    echo "</tr>";        while($row=mysql_fetch_row($res)){                  for($i=1; $i<$colums; $i++){             echo "<td>$row[$i]</td>";         }              echo "</tr>";}              echo "</table>";

封装数据库操作 见文章
php_08_链接数据库(封装)

1 0
原创粉丝点击