Home php教程 php手册 PHP3 入门教程---要注意的地方

PHP3 入门教程---要注意的地方

Jun 21, 2016 am 09:01 AM
mysql name nbsp query

 

1. 脚本开头部分定义的变量是 MYSQL_CONNECT() 函数的参数,当然我们也可以直接把这些字符串插入到函数中,但是,如果在一个大的 Web 应用中,这些值很可能被放在几个不同的文件中然后被包含进来(用 include 语句),如果一开始定义了这些字符串变量,要修改的时候就很容易了。
  
  
   2. 函数 @mysql_select_db() 用来选择一个数据库。这样做可以节省一些时间,能够在执行查询语句的时候不用给出数据库名。
  
  
   语法 : int mysql_select_db(string database_name, int link_identifier);
  
  
   * database_name 必须是服务器上的一个数据库名。
  
  
   * link_identifier (可选)指明建立的数据库连接号,如果省略,那么就会使用最后打开的连接。
  
  
   * 根据执行成功与否,返回真 / 假值。
  
  
   3. 函数 MYSQL_QUERY() 用来向 MySQL 数据库发送查询:
  
  
   语法 : int mysql_query(string query, int link_identifier);
  
  
   * query - 查询用的 SQL 字符串。
  
  
   * link_identifier - 数据库名(可选,如果省略,则使用最后打开的数据库连接),如果不想使用函数 @mysql_select_db() 选择的数据库,那么就必须给出数据库名。
  
  
   * 根据执行成功与否,返回正 / 负值,如果执行的是 SELECT 查询,那么返回的是结果号,否则返回值可以不用理会。
  
  
   4. MYSQL_CLOSE 函数关闭到 MySQL 数据库的连接。
  
  
   语法 : int mysql_close(int link_identifier);
  
  
   * link_identifier - 同上。
  
  
   * 同样的,根据执行成功与否,返回正 / 负值。
  
  
   如果设置的正确,你会看到数据真的被添加到了 information 表中。在下一部分中,我们将学会如何从 MySQL 数据库中提取数据,再把它显示出来。
  
  
   MySQL 提取数据
  
  
   我们已经成功的得到了足够多的用户信息,并且都储存在了数据库中。但是,怎样才能浏览这些数据,并从中得到有用的结论呢?
  
  
   下面,我们想把所有喜欢苹果的用户的姓名和邮件地址列出来:
  
  
  
  /*
这段脚本用来显示出所有喜欢苹果的用户的姓名和邮件地址 */
  
  /*
定义一些相关变量 */
  $hostname = "devshed";
  $username = "myusername";
  $password = "mypassword";
  $userstable = "information";
  $dbName = "mydbname";
  
  /*
建立连接 */
  MYSQL_CONNECT($hostname, $username, $password) OR DIE("Unable to connect to database");
  
  @mysql_select_db( "$dbName") or die( "Unable to select database");
  
  /*
选者所有喜欢苹果的用户 */
  $query = "SELECT * FROM $userstable WHERE choice = 'Apples'";
  
  $result = MYSQL_QUERY($query);
  
  /*
计算有多少这样的用户 */
  $number = MYSQL_NUMROWS($result);
  
  /*
把结果显示在屏幕上 */
  $i = 0;
  
  IF ($number == 0) :
   PRINT "
没有人喜欢吃苹果 ";
  ELSEIF ($number > 0) :
   PRINT "
喜欢吃苹果的用户数: $number";
   WHILE ($i    $name = mysql_result($result,$i,"name");
   $email = mysql_result($result,$i,"email");
   PRINT "$name
喜欢苹果 ";
   PRINT "
邮件地址: $email.";
   PRINT "";
   $i++;
   ENDWHILE;
   PRINT "";
  ENDIF;
  ?>
  
  
   把结果保存为 apples.php3.
  
  
   下面解释一下用到的函数:
  
  
   $number = MYSQL_NUMROWS($result);
  
  
   语法 : int mysql_num_rows(string result);
  
  
   * result - MYSQL_QUERY 函数返回结果号。
  
  
   * 函数返回值是纪录组中纪录的个数。
  
  
   还有一个与之相近的函数: mysql_num_fields(string result) ,它的返回是纪录集字段的个数。
  
  
   在输出的过程中,如果数据库中记录显示没有喜欢苹果的人,那么就显示字符串没有人喜欢吃苹果,否者,输出没有搜索到的用户的名字和邮件地址。这用到了一个 WHILE 循环,输出所有符合条件的数据。
  
  
   $name = MYSQL_RESULT($result,$i,"name");
  
  
   语法 : int mysql_result(int result, int i, column);
  
  
   mysql_result() 是用来提取一个纪录中某个字段的值:
  
  
   * $result 指明要操作的纪录集。
  
  
   * $i 指明要操作纪录集中的第几号纪录
  
  
   * column MySQL 表结构中一个字段名。
  
  
   这样,用一个简单的 WHILE 循环,我们就可以输出所有的数据了。
  
  
   SQL 函数:
  
  
   使用 MYSQL_QUERY() 函数能够执行一些 SQL 的函数来对数据库进行操作,其中就包括了 DELETE UPDATE 函数:
  
  
   Delete
  
  
   假设我们想删除名字为 "Bunny" 的纪录,那么可以这样做: :
  
  
   $query = "DELETE FROM $userstable WHERE name = "Bunny";
  
  
   MYSQL_QUERY($query);
  
  
   Update
  
  
   或者我们想修改所有名字是 "Bunny" 的纪录,并把 "Bunny" 改为“”
  
  
   $query = "UPDATE $userstable SET name = "Bugs Bunny" WHERE name = "Bunny"; MYSQL_QUERY($query);
  
  
   看完这篇文章,大家对 PHP3.0 应该有个大概的认识了。我们看到了如何用 PHP3.0 创建动态网页,还有如何通过 PHP3.0 MySQL 的结合,把数据库发布到网上。但是,这些只是冰山一角, PHP3.0 还有许多强大的功能。由于这只是一入门介绍性文章,在这儿就不多说了。
  
  
   我认为学习 PHP 的最好的方法,莫过于读 PHP3.0 的文档,这些文档都是由开发 PHP 的大师们写的,可能没有什么资料比这个文档更为详细的了。你可以不用去背记,只要读懂、理解了就好了。这个文档以及一些关于 PHP 的最新消息都可以在 http://www.php.net 找到,这是 PHP 的老巢,也是学习 PHP 所必须到的的方。 MySQL 的文档和相关资源可以在 http://www.mysql.com 找到。
  
  



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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 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's big data structure processing skills PHP's big data structure processing skills May 08, 2024 am 10:24 AM

Big data structure processing skills: Chunking: Break down the data set and process it in chunks to reduce memory consumption. Generator: Generate data items one by one without loading the entire data set, suitable for unlimited data sets. Streaming: Read files or query results line by line, suitable for large files or remote data. External storage: For very large data sets, store the data in a database or NoSQL.

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

How to insert data into a MySQL table using PHP? How to insert data into a MySQL table using PHP? Jun 02, 2024 pm 02:26 PM

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values ​​to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

How to use MySQL stored procedures in PHP? How to use MySQL stored procedures in PHP? Jun 02, 2024 pm 02:13 PM

To use MySQL stored procedures in PHP: Use PDO or the MySQLi extension to connect to a MySQL database. Prepare the statement to call the stored procedure. Execute the stored procedure. Process the result set (if the stored procedure returns results). Close the database connection.

How to create a MySQL table using PHP? How to create a MySQL table using PHP? Jun 04, 2024 pm 01:57 PM

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

The difference between oracle database and mysql The difference between oracle database and mysql May 10, 2024 am 01:54 AM

Oracle database and MySQL are both databases based on the relational model, but Oracle is superior in terms of compatibility, scalability, data types and security; while MySQL focuses on speed and flexibility and is more suitable for small to medium-sized data sets. . ① Oracle provides a wide range of data types, ② provides advanced security features, ③ is suitable for enterprise-level applications; ① MySQL supports NoSQL data types, ② has fewer security measures, and ③ is suitable for small to medium-sized applications.

See all articles