How to delete a specific row result obtained through mysql_query?

I have a mysql_query result which I repeat several times in different parts of the code, each time using mysql_data_seek ($ result, 0) to reset at the beginning of the result.

I am using mysql_fetch_array for these results and would like to remove a few specific rows from the $ result. Basically equivalent to unset ($ result [$ row]) if it was a normal array. Is there any way to do this?

Code example:

$result = mysql_query( $sql );
$num_rows = mysql_num_rows( $result );
if( $num_rows ){
    for( $a=0; $a < $num_rows; $a++ ){
    $row = mysql_fetch_array( $result );
    if( my_check_function( $row['test'] ){
                // do stuff
    } else {
            // remove this row from $result
    }
  }
}
mysql_data_seek( $result, 0 );

I know that I can just do unset ($ row [$ a]) to delete this specific row, but after the data is searched and I go through the results the next time, I get the same original rows of results.

. ps - , _ , , ..:)

+3
3

, . , .

$survivors = array();

while ($row = mysql_fetch_array($result)) { // while we have records
  if (do_something($row))                   // if this is a good record
    $survivors[] = $row;                    // save it for later
}

print_r($survivors);                        // who survived the cut?
+7
$result = mysql_query( $sql );
$new_rows = mysql_num_rows( $result );
$new_array = array();
if( $num_rows ){
    for( $a=0; $a < $num_rows; $a++ ){
    $row = mysql_fetch_array( $result );
    if( my_check_function( $row['test'] ){
                        // do stuff
         // populate new array with only validated data
         $new_array[$a] = $row;
    } else {
                // remove this row from $result
            // do not remove anything. 
    }
  }
}

PS. , sql?

+2

, , SQL- . PDO.

0

Source: https://habr.com/ru/post/1714345/


All Articles