mysql_query($sql)到底返回boolen值还是resource id?该怎么解决
mysql_query($sql)到底返回boolen值还是resource id???
- PHP code
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> <?php $sql = "SELECT * FROM UserInfo WHERE id=222"; $sql2 = "SELECT TOP 1 * FROM UserInfo WHERE id=222"; mysql_connect("localhost", "root", ""); mysql_select_db("MYDB"); $result = mysql_query($sql); if (!$result){ echo( "the user not exist" .mysql_query($sql)); }else{ echo( "ok ,the user exist".mysql_query($sql) ); } ?>
如果用sql,输出为ok ,the user existResource id #4
如果用sql2, 输出为the user not exist
声明一下,id = 222 的数据在数据库中是不存在的,为什么会有这样的结果?
用sql查询,返回的是资源ID,用sql2查询,返回的是布尔值,为什么?
------解决方案--------------------
查询成功就返回资源id,这与结果有多少没有关系
获取查询结果的行数用 mysql_num_rows
------解决方案--------------------
$result=mysql_query($sql);
if (!$mysql_num_rows($result)){
echo( "the user not exist" );
}else{
echo( "ok ,login sucsuss);
}
------解决方案--------------------
- PHP code
mysql_query ― 发送一条 MySQL 查询 说明 resource mysql_query ( string $query [, resource $link_identifier ] ) mysql_query() 向与指定的连接标识符关联的服务器中的当前活动数据库发送一条查询。如果没有指定 link_identifier,则使用上一个打开的连接。如果没有打开的连接,本函数会尝试无参数调用 mysql_connect() 函数来建立一个连接并使用之。查询结果会被缓存。 Note: 查询字符串不应以分号结束。 mysql_query() 仅对 SELECT,SHOW,EXPLAIN 或 DESCRIBE 语句返回一个资源标识符,如果查询执行不正确则返回 FALSE。对于其它类型的 SQL 语句,mysql_query() 在执行成功时返回 TRUE,出错时返回 FALSE。非 FALSE 的返回值意味着查询是合法的并能够被服务器执行。这并不说明任何有关影响到的或返回的行数。 很有可能一条查询执行成功了但并未影响到或并未返回任何行。 以下查询语法上有错,因此 mysql_query() 失败并返回 FALSE: Example #1 mysql_query() 例子 <php mysql_query where or die query: . mysql_error> 以下查询当 my_col 并不是表 my_tbl 中的列时语义上有错,因此 mysql_query() 失败并返回 FALSE: Example #2 mysql_query() <?php $result = mysql_query("SELECT my_col FROM my_tbl") or die("Invalid query: " . mysql_error()); ?> 如果没有权限访问查询语句中引用的表时,mysql_query() 也会返回 FALSE。 假定查询成功,可以调用 mysql_num_rows() 来查看对应于 SELECT 语句返回了多少行,或者调用 mysql_affected_rows() 来查看对应于 DELETE,INSERT,REPLACE 或 UPDATE 语句影响到了多少行。 仅对 SELECT,SHOW,DESCRIBE 或 EXPLAIN 语句 mysql_query() 才会返回一个新的结果标识符,可以将其传递给 mysql_fetch_array() 和其它处理结果表的函数。处理完结果集后可以通过调用 mysql_free_result() 来释放与之关联的资源,尽管脚本执行完毕后会自动释放内存。 参见 mysql_num_rows(),mysql_affected_rows(),mysql_unbuffered_query(),mysql_free_result(),mysql_fetch_array(),mysql_fetch_row(),mysql_fetch_assoc(),mysql_result(),mysql_select_db() 和 mysql_connect()。 <div class="clear"> </div></php>

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





You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

Installing MySQL on CentOS involves the following steps: Adding the appropriate MySQL yum source. Execute the yum install mysql-server command to install the MySQL server. Use the mysql_secure_installation command to make security settings, such as setting the root user password. Customize the MySQL configuration file as needed. Tune MySQL parameters and optimize databases for performance.
