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
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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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; 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.