Jump to content

convert mysql code to mysqli


felito

Recommended Posts

hi

 

i have a script that works perfectly, but i want to change to mysqli

 

so, the mysql is

<?php

include("includes/banco.php");
$theclass->conecta();


$consulta= "design";  

$rs = mysql_query('select profissao  from empregos where profissao like "'.$consulta .'%"');

$data = array();
if ( $rs && mysql_num_rows($rs) )
{
    while( $row = mysql_fetch_array($rs, MYSQL_ASSOC) )
    {
        $data[] = array(
            'label' => $row['profissao']  
        );
    }
}

echo json_encode($data);
flush();


?>

 

and the result is:

[{"label":"Design"},{"label":"Design Gráfico"},{"label":"Design"},{"label":"Design"}]

 

the mysqli at the moment is

 

<?php

include ('includes/includesMy.php');


$consulta= "design";


($sql = $db->prepare('select profissao from empregos where profissao like ?'));

$sql->bind_param('s', $consulta);
$sql->execute();
$sql->bind_result($profissao);

$data = array();

while ($sql->fetch()) {
$data[] = array(
            'label' => $row['profissao']  
);

}
echo json_encode($data);
$sql -> close();
$db -> close();


?>

 

ant the result is

[{"label":null},{"label":null},{"label":null}]

 

 

the mysqli is not working correctly, the number of outputs are different and label are null (only matches the specific word design and not design gráfico )

 

any help?

Link to comment
https://forums.phpfreaks.com/topic/233653-convert-mysql-code-to-mysqli/
Share on other sites

"the mysqli is not working correctly"  ..... you think so?

 

mysql

('select profissao  from empregos where profissao like "'.$consulta .'%"')

 

mysqli

'select profissao from empregos where profissao like ?'

 

see the difference?

 

and what the next post say too

The mysqli is working just fine. You can tell because you got the right number of results.

 

There's no $row anymore. If you try to use it you'll get nulls for everything.

You bound the first column of the results to the $profissao variable. Try using that.

This is a big reason why people don't use mysqli -- it's tedious, because you have to bind a variable to every column in your result set.  In your case, you are only selecting one column and returning one so it's no big deal, but this is why a lot of people use PDO instead, when they want the advantages of bind variables, but desire the flexibility of the old mysql api.

 

As requinix stated, you made the "bind" to the $profissao variable.  What that "binding" does, is make a sort of contract between mysql for the fetch. 

 

Whenever you fetch a row, the value for the column will be put into the $profissao variable.

 

All you really need to change is this line:

 

'label' => $row['profissao'] 

 

And make it:

 

'label' => $profissao; 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.