php的mysqlmysqliPDO(一)mysql,mysqlipdo
php的mysql\mysqli\PDO(一)mysql,mysqlipdo
原文链接:http://www.orlion.ga/1140/
工作中数据库的操作都被封装好了,这些怎么用的都快忘了干脆写篇博客重新复习下,以后要是再忘记了可以看这篇文章。
PHP 5.5.0 起已废弃
1、mysql_connect()
resource mysql_connect([ string $server [, string $username [, string $password [, bool$new_link [, int $client_flags ]]]]] )
$server:服务器地址可以包括端口号,如果 PHP 指令 mysql.default_host 未定义(默认情况),则默认值是 'localhost:3306'。 在 SQL 安全模式 时,参数被忽略,总是使用 'localhost:3306'。
$username:用户名。默认值由 mysql.default_user 定义。 在 SQL 安全模式 时,参数被忽略,总是使用服务器进程所有者的用户名。
$password:密码。默认值由mysql.default_password定义。在 SQL 安全模式 时,参数被忽略,总是使用空密码。
$new_link:如果用同样的参数第二次调用 mysql_connect(),将不会建立新连接,而将返回已经打开的连接标识。参数new_link
改变此行为并使 mysql_connect() 总是打开新的连接,甚至当 mysql_connect() 曾在前面被用同样的参数调用过。
$client_flags:使用以下常量:
常量 | 说明 |
---|---|
MYSQL_CLIENT_COMPRESS |
使用压缩的通讯协议 |
MYSQL_CLIENT_IGNORE_SPACE |
允许在函数名后留空格位 |
MYSQL_CLIENT_INTERACTIVE |
允许设置断开连接之前所空闲等候的 interactive_timeout 时间(代替 wait_timeout)。 |
MYSQL_CLIENT_SSL |
使用 SSL 加密。本标志仅在 MySQL 客户端库版本为 4.x 或更高版本时可用。在 PHP 4 和 Windows 版的 PHP 5 安装包中绑定的都是 3.23.x。
|
2、mysql_close()
bool mysql_close ([ resource $link_identifier = NULL ] )
mysql_close() 关闭指定的连接标识所关联的到 MySQL 服务器的非持久连接。如果没有指定 link_identifier
,则关闭上一个打开的连接.通常不需要使用 mysql_close(),因为已打开的非持久连接会在脚本执行完毕后自动关闭。PHP 4 Zend 引擎引进了引用计数系统,可以自动检测到一个资源不再被引用了(和 Java 一样)。这种情况下此资源使用的所有外部资源都会被垃圾回收系统释放。因此,很少需要手工释放内存。
3、mysql_select_db()
bool mysql_select_db ( string $database_name [, resource $ link_identifier ] )
如果没有指定$link_identifier则使用上一个打开的数据库连接,如果没有打开的连接则将无参数调用mysql_connet()取得一个连接并使用。如果没有连接则E_WARNING错误
4、mysql_query()
resource mysql_query ( string $query [, resource $link_identifier = NULL ] )
$query查询字符串不应该以分号结束
$link_identifier如果不指定处理方式与mysql_select_db()相同。
返回值:
mysql_query() 仅对 SELECT,SHOW,DESCRIBE, EXPLAIN 和其他语句 语句返回一个 resource,如果查询出现错误则返回 FALSE
。对于其它类型的 SQL 语句,比如INSERT, UPDATE, DELETE, DROP 之类, mysql_query() 在执行成功时返回 TRUE
,出错时返回 FALSE
。返回的结果资源应该传递给 mysql_fetch_array() 和其他函数来处理结果表,取出返回的数据。假定查询成功,可以调用 mysql_num_rows() 来查看对应于 SELECT 语句返回了多少行,或者调用mysql_affected_rows() 来查看对应于 DELETE,INSERT,REPLACE 或 UPDATE 语句影响到了多少行。如果没有权限访问查询语句中引用的表时,mysql_query() 也会返回 FALSE
。
对于包含二进制数据的查询,你必须使用mysql_real_query()而不是mysql_query(),因为二进制代码数据可能包含“\0”字符,而且,mysql_real_query()比mysql_query()更快,因为它不会在查询字符串上调用strlen()。如果查询成功,函数返回零。如果发生一个错误,函数返回非零
5、mysql_affected_rows()
int mysql_affected_rows ([ resource $link_identifier = NULL ] )
取得最近一次与 link_identifier
关联的 INSERT,UPDATE 或 DELETE 查询所影响的记录行数。
6、mysql_fetch_array()
array mysql_fetch_array ( resource $result [, int $ result_type ] )
将结果以数组方式返回,一次只返回一行,然后指向下一行(用while循环取出)如果没有更多结果返回false。如果结果中有两个或两个以上的列有相同的字段名,最后一列将优先。如果sql中指定了别名则使用别名。PHP手册中指出mysql_fetch_array并不明显比mysql_fetch_row慢。
$result_type:可选:MYSQL_ASSOC(关联数组),MYSQL_NUM(枚举数组) 和 MYSQL_BOTH,默认值MYSQL_BOTH。
// MYSQL_NUM $result = mysql_query("SELECT id, name FROM mytable"); while ($row = mysql_fetch_array($result, MYSQL_NUM)) { printf ("ID: %s Name: %s", $row[0], $row[1]); } // MYSQL_ASSOC $result = mysql_query("SELECT id, name FROM mytable"); while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { printf ("ID: %s Name: %s", $row["id"], $row["name"]); } // MYSQL_BOTH $result = mysql_query("SELECT id, name FROM mytable"); while ($row = mysql_fetch_array($result, MYSQL_BOTH)) { printf ("ID: %s Name: %s", $row[0], $row["name"]); }
7、其他函数:
string mysql_error ([ resource $link_identifier ] )
如果没有指定数据库连接则使用上一个连接,只返回最近一次mysql函数的错误文本。
bool mysql_set_charset ( string $charset [, resource $link_identifier = NULL ] )
设置字符集,数据库连接的选择与mysql_select_db()一样。
string mysql_real_escape_string ( string $unescaped_string [, resource $link_identifier ] )
转义 SQL 语句中使用的字符串中的特殊字符,并考虑到连接的当前字符集(这是与mysql_escape_string()的不同)

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



When writing web applications using PHP, a MySQL database is often used to store data. PHP provides a way to interact with the MySQL database called MySQLi. However, sometimes when using MySQLi, you will encounter an error message, as shown below: PHPFatalerror:Calltoundefinedfunctionmysqli_connect() This error message means that PHP cannot find my

PDOPDO is an object-oriented database access abstraction layer that provides a unified interface for PHP, allowing you to use the same code to interact with different databases (such as Mysql, postgresql, oracle). PDO hides the complexity of underlying database connections and simplifies database operations. Advantages and Disadvantages Advantages: Unified interface, supports multiple databases, simplifies database operations, reduces development difficulty, provides prepared statements, improves security, supports transaction processing Disadvantages: performance may be slightly lower than native extensions, relies on external libraries, may increase overhead, demo code uses PDO Connect to mysql database: $db=newPDO("mysql:host=localhost;dbnam

Solution to php unable to connect to mysqli: 1. Open the "php.ini" file; 2. Find "mysqli.reconnect"; 3. Change "mysqli.reconnect = OFF" to "mysqli.reconnect = on".

If you encounter the following error message when using PHP to connect to a MySQL database: PHPWarning:mysqli_connect():(HY000/2002):Connectionrefused, then you can try to solve this problem by following the steps below. To confirm whether the MySQL service is running normally, you should first check whether the MySQL service is running normally. If the service is not running or fails to start, it may cause a connection refused error. you can

The running file of mysql is mysqld; mysqld is an executable file, which represents the Mysql server program. Executing this file can directly start a server process; and mysqld_safe is a startup script, which will indirectly call mysqld and also start a monitor. process.

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

When using the mysqli extension to connect and operate a MySQL database, you sometimes encounter the error PHPFatalerror:Calltoundefinedmethodmysqli::prepare(). This error is usually caused by the following reasons: PHP has insufficient support for the mysqli extension; the mysqli extension is not loaded or configured correctly; there are syntax errors in the PHP code; the MySQL server is not correctly configured or running

When developing websites using PHP, database operations are very common. MySQLi is an extension commonly used in PHP to operate MySQL databases. It provides a relatively complete object-oriented interface, procedural interface, and supports operations of prepared statements. But sometimes when we use mysqli's prepared statements, we will encounter such an error: PHPFatalerror:Calltoundefinedfunctionmysqli_stmt_bin
