Query in MySQL with undesirable items - PHP

0

I need to compare two arrays that come back from MySQL, but one of them is coming back with several strange items, making my comparison impossible. I have already filtered the search to return only the field I want, but there is no way ...

Take a look at the code to understand it 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's 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 made 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 this data that is being registered in other sectors of the system, but I only need the last three in this array ... Does anyone know what it can be?

    
asked by anonymous 25.11.2014 / 04:07

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 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 just want numeric 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 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 on mysql_fetch_array .

    
25.11.2014 / 06:36