sql >> Base de Datos >  >> RDS >> Mysql

¿Diferencia entre mysql_fetch_array y mysql_fetch_row?

Muchos de los novatos en programación php se confunden con las funciones mysql_fetch_array(), mysql_fetch_row(), mysql_fetch_assoc() y mysql_fetch_object(), pero todas estas funciones realizan un proceso similar.

Vamos a crear una tabla "tb" para un ejemplo claro con tres campos "id", "nombre de usuario" y "contraseña"

Tabla:tb

Inserte una nueva fila en la tabla con valores 1 para id, tobby para nombre de usuario y tobby78$2 para contraseña

db.php

<?php
$query=mysql_connect("localhost","root","");
mysql_select_db("tobby",$query);
?>

mysql_fetch_row()

Obtener una fila de resultados como una matriz numérica

<html>
<?php
include('db.php');
$query=mysql_query("select * from tb");
$row=mysql_fetch_row($query);
echo $row[0];
echo $row[1];
echo $row[2];
?>
</html>

Resultado

1 tobby tobby78$2

mysql_fetch_object()

Obtener una fila de resultados como un objeto

<html>
<?php
include('db.php');
$query=mysql_query("select * from tb");
$row=mysql_fetch_object($query);
echo $row->id;
echo $row->username;
echo $row->password;
?>
</html>

Resultado

1 tobby tobby78$2

mysql_fetch_assoc()

Obtener una fila de resultados como una matriz asociativa

<html>
<?php
include('db.php');
$query=mysql_query("select * from tb");
$row=mysql_fetch_assoc($query);
echo $row['id'];
echo $row['username'];
echo $row['password'];
?>
</html> 

Resultado

1 tobby tobby78$2

mysql_fetch_array()

Obtenga una fila de resultados como una matriz asociativa, una matriz numérica y también obtiene por matriz asociativa y numérica.

<html>
<?php
include('db.php');
$query=mysql_query("select * from tb");
$row=mysql_fetch_array($query);
echo $row['id'];
echo $row['username'];
echo $row['password'];

<span style="color: #993300;">/* here both associative array and numeric array will work. */</span>

echo $row[0];
echo $row[1];
echo $row[2];

?>
</html>

Resultado

1 toby tobby78$2