Mysql query with undesirable items - PHP

Asked

Viewed 66 times

0

I need to compare two arrays that come back from Mysql, but one of them is coming back with several strange items, making it impossible to compare. I already trained in the search to return only the field that I want, but there is no way...

Take a look at the code to understand better:

public function listarNomes(){
$oConexao = new conexaoclass();
$oConexao -> abrirConexao();
$this -> sql = "SELECT Nome FROM Professores 
                ORDER BY Nome;";

$this -> resultado = mysql_query($this -> sql, $oConexao -> getConn());
}

Here comes the call:

include_once 'classes/professoresclass.php';

$oProfessor = new professoresclass();
$oProfessor ->listarNomes();          

while ($arrayProfessores = mysql_fetch_array($oProfessor->retorno())){
    $array2[] = $arrayProfessores['Nome'];
}

foreach ($array2 as $key => $value) {
    echo 'teste<br>';
}

I have only 3 names registered in the database but this 'test' appears several times... I did a var_dump() in array2 and the following appears:

array (size=11)

0 => string '1' (length=1)
'idSemestre' => string '1' (length=1)

1 => string '2014/2' (length=6)
'Nome' => string '2014/2' (length=6)

2 => string '2014-11-28' (length=10)
'DataDeInicio' => string '2014-11-28' (length=10)

3 => string '2014-12-05' (length=10)
'DataDeTermino' => string '2014-12-05' (length=10)

4 => string 'Administrador Máximo' (length=21)

5 => string 'Carlos Maltz' (length=12)

6 => string 'João Sem Braço' (length=16)

I really have these data that are coming to more registered in other sectors of the system, but I only need the last three in this array... Does anyone know what it can be?

1 answer

3


By default, the result of mysql-fetch-array comes with numeric indexes and also with indexes corresponding to the name of the bank. If you want only one of the two things, you need to use the second parameter:

$arrayProfessores = mysql_fetch_array( $oProfessor->retorno(), MYSQL_ASSOC )

Will return

'idSemestre' => '1'


On the other hand, if you only want numerical indexes:

$arrayProfessores = mysql_fetch_array( $oProfessor->retorno(), MYSQL_NUM )

And the result will be:

0 => '1'

Failing to specify the second parameter, as you are doing, is the same as setting it as MYSQL_BOTH: each result will return twice, once with numeric index, another with the name/alias of the column as an index.

More details on PHP manual about the mysql_fetch_array.

  • Yeah, but there’s something weird there... Because I do the same thing in another array, in array1 there in that code and it works, just comes the field I want... I will edit the question and put this array1 for you to take a look

Browser other questions tagged

You are not signed in. Login or sign up in order to post.