简体   繁体   English

使用PHP在MySQL中选择一行的最佳方法是什么?

[英]What is the best way to select one row in MySQL using PHP?

I have a mysql statement which grabs just one record based on an id. 我有一条mysql语句,它仅根据ID捕获一条记录。 I want to know if using mysql_fetch_row or something along the lines of that is better then using something such as mysql_fetch_array, for just grabbing one row in a database. 我想知道是否使用mysql_fetch_row或类似的方法比使用诸如mysql_fetch_array之类的方法更好,以便仅捕获数据库中的一行。

It may seem like common sense to go with the mysql_fetch_row but I just want to make sure. 使用mysql_fetch_row似乎是常识,但我只是想确定一下。

An important thing to note is that using mysql_fetch_array() is not significantly slower than using mysql_fetch_row() , while it provides a significant added value. 需要注意的重要一点是,使用mysql_fetch_array() 不会比使用mysql_fetch_row() 显着慢,尽管它提供了可观的附加值。

Source 资源

你为什么不只在mysql语句中使用LIMIT 1 ,而无论你将拥有一行。

The two functions are almost identical, the name is just making you think it's different: 这两个函数几乎完全相同,其名称只是让您认为它与众不同:

$r=mysql_query('SELECT name,email FROM tbl LIMIT 1');
var_dump(mysql_fetch_assoc($r));
    // array('name'=>[dbName], 'email'=>[dbEmail]);
var_dump(mysql_fetch_row($r));
    // array([dbName], [dbEmail]);
    // It's a normal incremental integer index array
var_dump(mysql_fetch_array($r,MYSQL_ASSOC));
    // same as mysql_fetch_assoc();
var_dump(mysql_fetch_array($r,MYSQL_NUM));
    // same as mysql_fetch_row();
var_dump(mysql_fetch_array($r,MYSQL_BOTH));
    // array(0=>[dbName], 1=>[dbEmail], 'name'=>[dbName], 'email'=>[dbEmail]);

The manual also suggests performance wise the two functions fared well. 该手册还建议明智地使用这两个功能。 Local tests hint in the same direction. 本地测试表明方向相同。 I'd suggest you not worry about the .0001 time/memory saved and find true bottlenecks in your applications. 我建议您不要担心保存的.0001时间/内存,并在应用程序中找到真正的瓶颈。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 PHP MySQL 动态表中更改行颜色的最佳方法是什么? - What is the best way to change row colors in PHP MySQL dynamic table? 使用MySQL,不选择存在于不同表中的用户的最佳方法是什么? - Using MySQL, What is the best way to not to select users that exist in a different table? 使用PHP连接和查询MySQL数据库的最佳方法是什么? - What is the best way to connect to and query a MySQL database using PHP? 使用PHP和MySQL存储用户图像的最佳方法是什么? - What is the best way to store users images using PHP and MySQL? 使用PHP将iOS客户端连接到MySql数据库的最佳方法是什么? - what is the best way to connect an iOS client to a MySql database using PHP? 使用PHP创建随机mysql ID的最佳方法是什么? - what is the best way to create random mysql ID using PHP 什么是使用php递归更新mysql的最佳方法 - what is best way to recursive update mysql using php 使用PHP从MySQL检索表的最佳方法是什么? - What is the best way to retrieve table from MySQL using PHP? 基于PHP / MYSQL中DATE的SELECT的最佳方式 - Best way of SELECT based on DATE in PHP/MYSQL 在不使用 mysql 服务器资源的情况下加密数据的最佳方法是什么? 使用 CFB 模式的 mcrypt 是使用 php 的最佳方式吗? - What is the best way to encrypt data without using the resource of mysql server ? Is mcrypt with CFB mode the best way with php?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM