[英]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似乎是常识,但我只是想确定一下。
你为什么不只在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.