How to use array_diff with iteration using 2 querys

0

I'm trying to find with array_diff the values (strings) that are not present between two resulting array of mysql queries using mysql_fetch_assoc to create each array.

Query 1

<?php
mysql_select_db($database_conn, $conn);
$query_est_lista = "SELECT localidades.estacion FROM localidades ORDER BY 
localidades.codigo ASC";
$est_lista = mysql_query($query_est_lista, $conn) or die(mysql_error());
$row_est_lista = mysql_fetch_assoc($est_lista);
$totalRows_est_lista = mysql_num_rows($est_lista);?>

Query 2

<?php
mysql_select_db($database_conn, $conn);
$query_est_entrada = sprintf("SELECT lluvias.estacion FROM lluvias WHERE 
lluvias.fecha LIKE %s", GetSQLValueString($today_est_entrada, "date"));
$est_entrada = mysql_query($query_est_entrada, $conn) or die(mysql_error());
$row_est_entrada = mysql_fetch_assoc($est_entrada);
$totalRows_est_entrada = mysql_num_rows($est_entrada);?>

When I try to perform the check like this ...

  <?php 
 $arr= array_diff($row_est_lista,$row_est_entrada);
 foreach ($arr as $value) {
 echo $value.' '.'<br>';}?>

returns the expected result but only the first value in each array and I have tried to iterate both arrays (using do {} while ()) but I can not get it to work correctly. Please suggest the correct way to iterate both arrays to find all the results.

    
asked by Alejandro Martinez Alvaro 31.05.2018 в 21:41
source

2 answers

0

The mysql functions are considered obsolete, use mysqli better. The problem is that mysqli_fetch_assoc only returns a row. What you need is mysqli_fetch_all like this:

<?php
mysqli_select_db($database_conn, $conn);
$query_est_lista = "SELECT localidades.estacion FROM localidades ORDER BY 
localidades.codigo ASC";
$est_lista = mysqli_query($query_est_lista, $conn) or die(mysql_error());
$row_est_lista = array();
while ($row = mysql_fetch_assoc($est_lista)) {
    $row_est_lista[] = $row;
}
$totalRows_est_lista = mysqli_num_rows($est_lista);?>
    
answered by 31.05.2018 в 22:04
0

I found another simpler solution to achieve what I wanted, I did it through MySQL through a query, here it goes in case someone needs some day. SELECT station FROM localities WHERE localidades.estacion NOT IN (SELECT station from rains WHERE rains.date LIKE today) eureka!

    
answered by 04.06.2018 в 15:00