How do you translate 'mysqli_fetch_array' in the code and screen, what are you doing? [duplicate]

1

<?php
	include 'conexion.php';

	$consulta = mysqli_query ($conn, "SELECT * FROM Usuario");

	

	// error mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in ----->  
	// se arreglo viendo el error asi 'if (!$consulta){ echo mysqli_error ($conn);}' resulta que escribi mal la tabla, usarlo para ver mas errores


	// https://www.w3schools.com/php/func_mysqli_query.asp
	// mysqli_query(connection,query,resultmode);
	// https://www.w3schools.com/sql/ 

	if (mysqli_num_rows ($consulta)> 0)
		//  mysqli_num_rows ($consulta) va a ser igual  al numero de columnas, de la misma manera que  "Mysqli_query ($conn, "SELECT * FROM Usuarios")"  sera igual al valor que lleva dentro del parentesis. Por lo que si es mayor a 0, osea si hay columnas en la consulta, viendo asi que fue satisfactoria, se hace lo siguiente.

//array es fila (<-- -->), no columna 
	{

		echo "<p>";
		// mysqli_fetch_array(result,resulttype);
		// [result ]= Mysqli_query(), mysqli_store_result() or mysqli_use_result()
		// [resulttype ] = MYSQLI_ASSOC, MYSQLI_NUM, MYSQLI_BOTH
		while ($row = mysqli_fetch_array($consulta, MYSQLI_ASSOC)  )
			// otro error: Use of undefined constant MYSQL_ASSOC - assumed 'MYSQL_ASSOC'
			// use esto "if (!$row){ echo mysqli_error ($MYSQLI_ASSOC);}" resulta que coloque MYSQL_ASOC, cuando tuvo que ser MYSQLI_ASSOC
			// https://www.phpclasses.org/discuss/package/9199/thread/4/

		{
			echo "<tr>";
			echo "< <td>".$row['nombre']."</td>";
			echo "< <td>".$row['fecha_creacion']."</td>";
			echo "< <td>".$row['nick']."</td>";
			echo "</tr>";

			echo "
			<table>
				<thead>
					<tr>
						<th>Nombre</th>
						<th>Fecha creacion</th>
						<th>Nick</th>

				</thead>
			</table>
			";


		}



	}

?>

I still can not get a clear idea of what 'mysqli_fetch_array' does, I still do not have a mental image about it, and that is that when I run the code in the browser I get this:

< Jenny < 2018-08-16 < Wick Name Date creation Nick < danphan < 2018-08-16 < danphan29 Name Date creation Nick < mmtuts < 2018-08-16 < hood Name Creation date Nick

Each time I add a new user I update the browser and each one comes out with "Name Creation Date Nick" below, I do not know why, when I only wrote it once, that you are allowing this format to appear on the whole list.

As for the format of the Mysqli_fetch_array, in the second parameter, 'resulttype' I do not see what is the difference between 'MYSQLI_ASSOC, MYSQLI_NUM, MYSQLI_BOTH' or what advantages it has from each other, I did not find much about it. Thanks.

    
asked by Hoozuki 16.08.2018 в 18:01
source

1 answer

3
mysqli_fetch_array();

Transform the results of your query to an array; let's see that you can return it in various ways such as:

  • a numeric array
  • $data = ["pedro", "perez", "[email protected]"]

  • an associative type array
  • $data = ["nombre" => "pedro", "apellido" => "perez", "email" => "[email protected]"]

    The difference is how you plan to return and operate the data; that is, how it suits you best;

    To operate the data of a numeric array

    $row=mysqli_fetch_array($consulta,MYSQLI_NUM);
    
      

    The detail is that with a numerical array, when you try to obtain the   data, you will have to indicate the index or position   numerical where the value in the array is found; so

    printf ($row[0],$row[1]);
    

    To operate the data with an associative array

    $row=mysqli_fetch_array($result,MYSQLI_ASSOC);
    
      

    The detail is that with an associative array, instead of indicating the   position of the element in the array, only notes in the form of a string   text the name of the column in the database that you are trying to   get

    printf ($row["nombre"],$row["apellido"]);
    

    In addition to the above:

  • mysqli_fetch_assoc = > Returns a multidimensional arrangement of the consulted values
  •   

    I would recommend you to use mysqli_fetch_assoc() that returns a row   of the query made in the form of a key = > value

        
    answered by 16.08.2018 / 18:10
    source