felito Posted April 14, 2011 Share Posted April 14, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/233653-convert-mysql-code-to-mysqli/ Share on other sites More sharing options...
mikosiko Posted April 14, 2011 Share Posted April 14, 2011 "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 Quote Link to comment https://forums.phpfreaks.com/topic/233653-convert-mysql-code-to-mysqli/#findComment-1201358 Share on other sites More sharing options...
requinix Posted April 14, 2011 Share Posted April 14, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/233653-convert-mysql-code-to-mysqli/#findComment-1201359 Share on other sites More sharing options...
gizmola Posted April 14, 2011 Share Posted April 14, 2011 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; Quote Link to comment https://forums.phpfreaks.com/topic/233653-convert-mysql-code-to-mysqli/#findComment-1201361 Share on other sites More sharing options...
felito Posted April 14, 2011 Author Share Posted April 14, 2011 yes, now i understand. stupid mistake, sorry. one more question, i have problems with the accents, in mysql i can put mysql_query("set NAMES utf8;"); and solve the problem. but in mysqli? Quote Link to comment https://forums.phpfreaks.com/topic/233653-convert-mysql-code-to-mysqli/#findComment-1201363 Share on other sites More sharing options...
felito Posted April 14, 2011 Author Share Posted April 14, 2011 $db->query("SET NAMES 'utf8'"); solved, and thanks Quote Link to comment https://forums.phpfreaks.com/topic/233653-convert-mysql-code-to-mysqli/#findComment-1201374 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.