php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #80740 Possible "in_array" bug
Submitted: 2021-02-12 16:07 UTC Modified: 2021-02-12 22:51 UTC
From: celestinoxp at hotmail dot com Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: 8.0Git-2021-02-12 (snap) OS: Windows 10
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
49 - 29 = ?
Subscribe to this entry?

 
 [2021-02-12 16:07 UTC] celestinoxp at hotmail dot com
Description:
------------
I have a code to insert delays of numbers in a mysql table, however values are not correct.

I think my code is correct, so, assuming that, i think problem is in php or mysql, it´s for that i need help to check if is a bug in php or not...


I have a mysql table with lotto numbers (n1, n2, n3...) and delays (n1_ausencias, n2_ausencias...)

my script is to update values (initial 0) to delay values in (n1_ausencias, n2_ausencias, n3_ausencias...) 

printscreen: https://i.stack.imgur.com/GBZQx.png 

Test script:
---------------
<?php

	set_time_limit(0);

	include "../ligar_bd.php";
	
	$i = 0;
	
	$result = mysqli_query($db, "SELECT `id`, `data`, `n1`, `n2`, `n3`, `n4`, `n5`, `ns` FROM `tbl_todos` WHERE `nome_do_jogo` = 'totoloto' ORDER BY `tbl_todos`.`data` DESC;");
	
	$f = mysqli_num_rows($result);
	
	while ($dados = mysqli_fetch_array($result)){
		
		$chave_bd = array($dados['n1'], $dados['n2'], $dados['n3'], $dados['n4'], $dados['n5']);
		
		$conta = -1;
		
		++$i;
		
		$n1_encontrou = 0;
		$n2_encontrou = 0;
		$n3_encontrou = 0;
		$n4_encontrou = 0;
		$n5_encontrou = 0;
		$ns_encontrou = 0;
		
		$result2 = mysqli_query($db, "SELECT `id`, `data`, `n1`, `n2`, `n3`, `n4`, `n5`, `ns` FROM `tbl_todos` WHERE `nome_do_jogo` = 'totoloto' ORDER BY `tbl_todos`.`data` DESC LIMIT $i, $f;");
		
		while ($dados2 = mysqli_fetch_array($result2)){	
			
			++$conta;
			
			$n1 = $dados2['n1'];
			$n2 = $dados2['n2'];
			$n3 = $dados2['n3'];
			$n4 = $dados2['n4'];
			$n5 = $dados2['n5'];
			$ns = $dados2['ns'];			
			
			if (in_array($n1, $chave_bd) and ($n1_encontrou == 0)){
				
				$n1_encontrou = 1;
				$n1_pos = $conta;
			}
			
			if (in_array($n2, $chave_bd) and ($n2_encontrou == 0)){
				
				$n2_encontrou = 1;
				$n2_pos = $conta;
			}
			
			if (in_array($n3, $chave_bd) and ($n3_encontrou == 0)){
			
				$n3_encontrou = 1;
				$n3_pos = $conta;
			}
			
			if (in_array($n4, $chave_bd) and ($n4_encontrou == 0)){
				
				$n4_encontrou = 1;
				$n4_pos = $conta;
			}
			
			if (in_array($n5, $chave_bd) and ($n5_encontrou == 0)){
				
				$n5_encontrou = 1;
				$n5_pos = $conta;
			}
			
			if ($ns == $dados['ns'] and $ns_encontrou == 0){
				
				$ns_encontrou = 1;
				$ns_pos = $conta;
			}
			
			//Se encontrou ausencias de todos, não precisa perder mais tempo a percorrer toda a tabela
			if ($n1_encontrou == 1 and $n2_encontrou == 1 and $n3_encontrou == 1 and $n4_encontrou == 1 and $n5_encontrou == 1 and $ns_encontrou == 1){
				
				break;
			}
			
		} //fecha while dados2
		
		$atualiza = mysqli_query($db, "UPDATE `tbl_todos` SET `n1_ausencias` = $n1_pos, `n2_ausencias` = $n2_pos, `n3_ausencias` = $n3_pos, `n4_ausencias` = $n4_pos, `n5_ausencias` = $n5_pos, `ns_ausencias` = $ns_pos WHERE `tbl_todos`.`id` = $dados[id];");			
		
		
	} //fecha while dados
	
?>

Expected result:
----------------
correct delay values

Actual result:
--------------
incorrect delay values

printscreen: https://i.stack.imgur.com/GBZQx.png

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-02-12 16:09 UTC] danack@php.net
-Status: Open +Status: Feedback
 [2021-02-12 16:09 UTC] danack@php.net
Thank you for this bug report. To properly diagnose the problem, we
need a short but complete example script to be able to reproduce
this bug ourselves.

A proper reproducing script starts with <?php and ends with ?>,
is max. 10-20 lines long and does not require any external
resources such as databases, etc. If the script requires a
database to demonstrate the issue, please make sure it creates
all necessary tables, stored procedures etc.

Please avoid embedding huge scripts into the report.


 [2021-02-12 16:12 UTC] danack@php.net
If nothing else, please make it clear what you think 'correct' values are. It takes too long to figure out what you are implying the issue is from screenshots. But this will almost certainly a bug in your code, rather than PHP.
 [2021-02-12 16:32 UTC] celestinoxp at hotmail dot com
It's so hard the code? If you see, it should insert delays of numbers.
Its a lottery numbers (n1, n2, n3, n4, n5, ns) and delays of this numbers (n1_ausencias, n2_ausencias)...
 [2021-02-12 20:27 UTC] celestinoxp at hotmail dot com
I checked my code and I'm 99.9% sure that it's a PHP bug!
 [2021-02-12 21:09 UTC] requinix@php.net
-Status: Feedback +Status: Not a bug
 [2021-02-12 21:09 UTC] requinix@php.net
You are calculating the ausencias values according to how many rows it takes for n1-n5 *from $dados2* to appear *in $dados*.

Ex:      n1 n2 n3 n4 n5
348010:   3 10 24 25 41 - $chave_bd = 3,10,24,25,41
348011:  18 19 23 27 47 - $conta=0, in_array(18, $chave_bd) == false
348012:  11 20 22 30 42 - $conta=1, in_array(11, $chave_bd) == false
348013:  10 26 29 31 42 - $conta=2, in_array(10, $chave_bd) == true, n1_ausencias=2
 [2021-02-12 22:20 UTC] celestinoxp at hotmail dot com
requinix@php.net why values n1_ausencias, n2_ausencias... are not correct???
You find any error in code? So i think php have a bug!
 [2021-02-12 22:23 UTC] requinix@php.net
Yes, there is an error in your code.

This is a bug tracker, not a PHP support group. See
http://www.php.net/support.php
for other places where you can get a more detailed explanation of the problem.
 [2021-02-12 22:46 UTC] celestinoxp at hotmail dot com
I don't find any error in the code, so I come here to ask for help to check a possible bug in php. can we work together?
 [2021-02-12 22:50 UTC] celestinoxp at hotmail dot com
Ex:      n1 n2 n3 n4 n5
348010:   3 10 24 25 41 - $chave_bd = 3,10,24,25,41
348011:  18 19 23 27 47 - $conta=0, in_array(18, $chave_bd) == false
348012:  11 20 22 30 42 - $conta=1, in_array(11, $chave_bd) == false
348013:  10 26 29 31 42 - $conta=2, in_array(10, $chave_bd) == true

problem is n1_ausencias, n2_ausencias... n5_ausencias, have not correct values.
Can you check this? Please?
 [2021-02-12 22:51 UTC] requinix@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.


 [2021-02-13 12:49 UTC] celestinoxp at hotmail dot com
Problem solved.
You can delete this post.
Thaks.
 [2021-02-13 13:09 UTC] rtrtrtrtrt at dfdfdfdf dot dfd35
> Problem solved

and would you mind describe your mistake so that others finding your wrong bugreport can look if they amde the same?

> You can delete this post

this is a bugtracker and there won't be something deleted
 [2021-02-13 14:05 UTC] celestinoxp at hotmail dot com
This line shlould be in second loop instead first loop:

$chave_bd = array($dados2['n1'], $dados2['n2'], $dados2['n3'], $dados2['n4'], $dados2['n5']);

...and lines down should be in first loop

	$n1 = $dados['n1'];
	$n2 = $dados['n2'];
	$n3 = $dados['n3'];
	$n4 = $dados['n4'];
	$n5 = $dados['n5'];
	$ns = $dados['ns'];

In resume, is not a php bug, but bug in code...
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Sun Jul 03 19:04:05 2022 UTC