Home Database Mysql Tutorial PHP4 与 MySQL 数据库操作函数详解(2)_MySQL

PHP4 与 MySQL 数据库操作函数详解(2)_MySQL

Jun 01, 2016 pm 02:05 PM
echo in query function operate database Detailed explanation return

.数据库记录操作函数(5个):

1、mysql_fetch_array()
格式:array mysql_fetch_array(int query);

执行成功,返回1个数组,该数组保存有下一条记录的值,如执行失败,则返回False值。
返回的数组既可以用下标来表示,也可以用字段名来表示。

例子:
$query = mysql_query($sql , $connect);
while($arrary = mysql_fetch_array($query))
{

echo $array[column1]." | ".$array[column2];
//echo $array[0]." | ".$array[1];

}
?>
Note : 数组的下标是从0开始的!

2、mysql_fetch_row()
格式:array = mysql_fetch_row(int query);

与1的mysql_fetch_array()函数功能基本相同。区别在于mysql_fetch_row()只能以数组下标来表示。
成功返回1个数组,失败返回False值。

例子:
$query = mysql_query($sql , $connect);
while($row = mysql_fetch_row($query))
{
echo $row[0] . " | " . $row[1] . "
";
}
?>
Note : mysql_fetch_row()函数只能用数组下标来表示,并从0开始。
另:mysql_fetch_row()比mysql_fetch_array()执行速度快,并且都是对下一行数据进行读取。

3、mysql_result()
格式:int mysql_result(int query , int row , string filedname);

在mysql_result()中,参数row必须从0开始,参数filedname必须是真实的字段名,不能用下标来表示。
执行成功,返回由数据库中取出的字段的数值,失败则返回False值。

例子:
$query = mysql_query($sql , $connect);
echo mysql_result($query , 0 , "column1")."
";
echo mysql_result($query , 1, "column1")."
";
echo mysql_result($query , 2, "column1")."
";
?>

Note : 此函数功能少,但使用方便。

4、mysql_fetch_object()
格式: object mysql_fetch_object(int query)

能对指定的字段进行循环读取,执行成功,将以对象object的形式返回数值,失败则返回False值。

例子:
$query = mysql_query($sql , $connect);
while($object = mysql_fetch_object($query))
{
echo $object->column1 . "
";
echo $object->column2 . "
";
echo $object->column3 . "
";
}
?>


Note : mysql_fetch_object()函数执行成功后返回的是1个对象object !
操作如下:
$object->字段名

5、mysql_data_seek()
格式:int mysql_data_seek(int row , int query);
移动游标到指定的行(row_number)
执行成功,返回真值,失败,返回False值。
此函数可以与mysql_fetch_array()或mysql_fetch_row()配合使用,即在使用mysql_data_seek()函数之后,就可以用mysql_fetch_array()或mysql_fetch_row()函数来显示指定的行了。

例子:
$query = mysql_query($sql , $connect);
$seek = mysql_data_seek($query , 2);
$arrary = mysql_fetch_array($query);
echo $array[column1]."
";
echo $array[column2]."
";
?>

  .数据库级database的操作函数(2个):

1、mysql_create_db()
格式:int mysql_create_db(string database name , int link_identifier);

通过程序来建立1个数据库database,当然你也可以用mysql_query()或者mysql_db_query()函数来建立或删除database

但我们可以用这个函数更为方便地建立1个database。
成功返回1个真值,失败,则返回1个false。

例子:


$connect = mysql_connect("$hostname","$user","$pwd");
$create = mysql_create_db("dbtest" , $connect);
if($create) echo "create database dbtest successed!";
else echo "create database dbtest failed!";

?>


2、mysql_drop_db()
格式:int mysql_drop_db(string database name , int link_identifier);

通过程序来删除1个数据库database。

但我们可以用这个函数更为方便地删除1个database。
成功返回1个真值,失败,则返回1个false。

例子:


$connect = mysql_connect("$hostname","$user","$pwd");
$create = mysql_drop_db("dbtest" , $connect);
if($create) echo "drop datebase dbtest successed!";
else echo "drop datebase dbtest failed!";

?>

Note : 如用mysql_query()或mysql_db_query(),则SQL语句应为:
(1)create database dbtest
(2)drop datebase dbtest

转载:www.chinaphp.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

Video Face Swap

Video Face Swap

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

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)

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

Complete collection of excel function formulas Complete collection of excel function formulas May 07, 2024 pm 12:04 PM

1. The SUM function is used to sum the numbers in a column or a group of cells, for example: =SUM(A1:J10). 2. The AVERAGE function is used to calculate the average of the numbers in a column or a group of cells, for example: =AVERAGE(A1:A10). 3. COUNT function, used to count the number of numbers or text in a column or a group of cells, for example: =COUNT(A1:A10) 4. IF function, used to make logical judgments based on specified conditions and return the corresponding result.

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

How to handle database connection errors in PHP How to handle database connection errors in PHP Jun 05, 2024 pm 02:16 PM

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

C++ Function Exception Advanced: Customized Error Handling C++ Function Exception Advanced: Customized Error Handling May 01, 2024 pm 06:39 PM

Exception handling in C++ can be enhanced through custom exception classes that provide specific error messages, contextual information, and perform custom actions based on the error type. Define an exception class inherited from std::exception to provide specific error information. Use the throw keyword to throw a custom exception. Use dynamic_cast in a try-catch block to convert the caught exception to a custom exception type. In the actual case, the open_file function throws a FileNotFoundException exception. Catching and handling the exception can provide a more specific error message.

Astar staking principle, income dismantling, airdrop projects and strategies & operation nanny-level strategy Astar staking principle, income dismantling, airdrop projects and strategies & operation nanny-level strategy Jun 25, 2024 pm 07:09 PM

Table of Contents Astar Dapp Staking Principle Staking Revenue Dismantling of Potential Airdrop Projects: AlgemNeurolancheHealthreeAstar Degens DAOVeryLongSwap Staking Strategy & Operation "AstarDapp Staking" has been upgraded to the V3 version at the beginning of this year, and many adjustments have been made to the staking revenue rules. At present, the first staking cycle has ended, and the "voting" sub-cycle of the second staking cycle has just begun. To obtain the "extra reward" benefits, you need to grasp this critical stage (expected to last until June 26, with less than 5 days remaining). I will break down the Astar staking income in detail,

How to connect to remote database using Golang? How to connect to remote database using Golang? Jun 01, 2024 pm 08:31 PM

Through the Go standard library database/sql package, you can connect to remote databases such as MySQL, PostgreSQL or SQLite: create a connection string containing database connection information. Use the sql.Open() function to open a database connection. Perform database operations such as SQL queries and insert operations. Use defer to close the database connection to release resources.

How to use database callback functions in Golang? How to use database callback functions in Golang? Jun 03, 2024 pm 02:20 PM

Using the database callback function in Golang can achieve: executing custom code after the specified database operation is completed. Add custom behavior through separate functions without writing additional code. Callback functions are available for insert, update, delete, and query operations. You must use the sql.Exec, sql.QueryRow, or sql.Query function to use the callback function.

See all articles