Home Backend Development PHP Tutorial 实例讲解php数据访问_PHP

实例讲解php数据访问_PHP

May 27, 2016 am 10:18 AM
php data access

本文实例为大家分享了两种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);  

}

Copy after login

方法二:面向对象

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());

}
Copy after login

练习:

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>";

}

Copy after login

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>";
}
Copy after login

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

Statement of this Website
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

See all articles