Inner Join question with Prepared statement

Asked

Viewed 33 times

0

Hello,

I was using select that way and working perfectly

$id = (int)$_GET["id"];
$banco = $mysqli->query("SELECT os.os_solicitado,os.os_status,cliente.cliente_emailfinanceiro FROM os left join cliente on os.os_razaosocial = cliente.cliente_razaosocial where os.os_id = $id"); 
while($dados = $banco->fetch_array()) { 
$status = $dados["os_status"];
$solicitado = $dados["os_solicitado"];
$emailfin = $dados["cliente_emailfinanceiro"];

But to improve security I’m changing to that way

$stmt = $mysqli->prepare("SELECT os.os_solicitado,os.os_status,cliente.cliente_emailfinanceiro FROM os left join cliente ON os.os_razaosocial=cliente.cliente_razaosocial WHERE os.os_id = ? ");
            $stmt->bind_param("s", $_GET['id']);
            $stmt->execute();
            $stmt->close();
while($dados = $stmt->fetch_array()) { 
$status = $dados["os_status"];
$solicitado = $dados["os_solicitado"];
$emailfin = $dados["cliente_emailfinanceiro"];

but this second way gives error, does not connect in the bank and can not identify what is

  • Post the error that is appearing

  • In my case appears Error Connecting to database

  • Then put the connection code where you pass the database data, remember to change the actual data to fictitious data.

  • mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
set_exception_handler(function($e) {
 error_log($e->getMessage());
 exit('Error connecting to database'); //Should be a message a typical user could understand
});
$mysqli = new mysqli("localhost", "root", "", "test"); $mysqli->set_charset("ISO-8859-1");

  • remembering that to do without preparing it, works just right

  • To facilitate reading, seeks to put additional information in the question itself, editing it.

  • replace the content of Exit with $e->getMessage(), to see which error is actually happening.

  • Call to Undefined method mysqli_stmt::fetch_array()

Show 4 more comments

1 answer

0


Add after the $stmt->execute();

$result = $stmt->get_result();

and in the while($dados = $stmt->fetch_array()) {

change to

while($dados = $result->fetch_array()) { 

Browser other questions tagged

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