search
Homephp教程php手册实例讲解php数据访问,实例讲解php数据

实例讲解php数据访问,实例讲解php数据

本文实例为大家分享了两种php数据访问方式,大家可以进行比较,分析两种方式的异同,最后为大家提供了一个小练习,具体内容如下

方式一:已过时,只做了解

1.造一个连接(建立通道)

$db=mysql_connect("localhost","root","123");     //括号内是“服务器地址”,“用户名”,“密码”

2.选择操作哪个数据库

mysql_select_db("mydb","$db");

3.写sql语句

$sql="select * from Info";

4.执行sql语句

$result=mysql_query($sql);      //query 有查询之意

5.从结果集($result)中取数据

$row=mysql_fetch_row($result);  //每执行一次读取一行数据

$row1=mysql_fentch_row($result);  //执行第二条数据

var_dump($row);

//读取全部数据用循环:

while($row=mysql_fetch_row($result))

{

  var_dump($row);  

}

方法二:面向对象

1.造一个连接对象:

$db=new MySQLi("localhost","root","123","mydb")  //括号内的内容依次为“服务器地址”,“用户名”,“密码”,“数据库名称”

2.判断连接是否出错:

2.1 mysqli_connect_error();  //代表连接出错

2.2

if(mysqli_connect_erroe())

       {

    echo "连接失败!";

    exit();  //退出程序

        }

  2.3 !mysqli_connect_error or die ("连接失败!"); //“or”前面代表连接正确,后面代表连接失败

3. 写sql语句:

$sql="select * from nation";

4. 执行sql语句:如果执行成功返回结果集对象,如果执行失败返回false

$result=$db->query($sql);

5.从结果集中读取数据,先判断是否有数据

if($result)

{

  //返回一行数据的索引数组,每次执行返回一条数据

   var_dump($result->fetch_row()); 

  while($row=$result->fetch_row)

  {

    var_dump($row);

  }

  //返回一行数据的关联数组,每次执行返回一条数据

  var_dump($result->fetch_row()); 

  //通过二维数组返回所有数据

  var_dump($result->fetch_all());

  //以对象的方式返回一行数据

  var_dump($result->fetch_object());

}

练习:

1.以下拉菜单的形式在页面显示nation表

$db=new MySQLi("localhost","root","","mydb");

!mysqli_connection_erroe() or die ("连接失败!");

$sql="select*from nation";

$result=$db->query($sql);

if($result)

{

  $att=$result->fetch_all();

  echo "<select>";

  foreach ($att as $value)

  {

    echo "<option value='{$value[0]}'>{$value[1]}</option>";

  }

  echo "</select>";

}

 

2. 把Info表查出来,以表格的形式显示

$db=new MySQLi("localhost","root","","mydb");

!mysqli_connecton_error() or die("连接失败!");

$sql="select * from info";

$result=$bd->query($sql);

if($result)

{

$att=$result->fetch_all();

echo "<table border='1' width='100%' cellpadding='0' cellspacing='0'>";

echo "<tr><td>代号</td><td>姓名</td><td>性别</td><td>民族</td><td>生日</td></tr>";

foreach ($att as $value)

{

 echo "<tr>

<td>{$value[0]}</td>

<td>{$value[1]}</td>

<td>{$value[2]}</td>

<td>{$value[3]}</td>

<td>{$value[4]}</td>

</tr>";

}

echo "</table>";

}

 

//也可以用for循环

if($result)
{
  $arr=$result->fetch_all();
  echo "<table border='1' width='100%' cellpadding='0' cellspacing='0'>";
  echo "<tr><td>Code</td><td>Name</td><td>Sex</td><td>Nation</td><td>Birthday</td></tr>";
  for($i=0;$i<count($arr);$i++)
  {
    echo "<tr>
    <td>{$arr[$i][0]}</td>
    <td>{$arr[$i][1]}</td>
    <td>{$arr[$i][2]}</td>
    <td>{$arr[$i][3]}</td>
    <td>{$arr[$i][4]}</td> 
    </tr>";
  }
  echo "</table>";
}

以上就是本文的全部内容,希望对大家学习php程序设计有所帮助。

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment