PHP的bind_param无法正常工作 [英] php bind_param is not working

查看:122
本文介绍了PHP的bind_param无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从php $ stmt-> bind_param得到了奇怪的结果,这是发生了什么...如果我这样做 $ status ="something"; $ var = 5;

i'm having weird results from php $stmt->bind_param, here is what happens... if i do this $status="something"; $var=5;

$consulta="UPDATE carrito SET status='$status' WHERE id_carrito='$var'";

并准备并执行它可以工作...但是只要我这样做:

and prepare and execute it works... but as soon as i do this:

$consulta="UPDATE carrito SET status=? WHERE id_carrito=?";
if($stmt=$mysqli->prepare($consulta))
{
    $stmt->bind_param("si",$status,$var);
  .
  .
  .

它停止工作,vars,可以,我在执行后将它们打印出来,它们实际上具有正确的值,我没有从php中得到任何错误,查询执行,它只是也没有在mysql db上保存值,我想要提到这不是我第一次使用准备好的语句,这已经是一段时间了,我还不是专家,但这在我不知道bind_param出问题之前从未发生过,但是我找不到任何信息,谢谢.

it stops working, vars, are ok i print them after the execute and they have the correct value actually, i don't get any error from php, query executes, it just dont save values on mysql db also, i want to mention this is not the first time i work with prepared statements i've doing this for a while now, i'm not an expert yet, but this has never happened to me before i know there is something wrong with bind_param, but i don't find any information thanks.

这是完整的代码

/*_____________________ DATOS DE CONEXION _________________________*/

$mysqli = new mysqli('localhost', 'root', '', 'ambarb');

if(mysqli_connect_errno()) 
    {
        echo "Connection Failed: " . mysqli_connect_errno();
        exit();
    }
/*_____________________ fin de los datos de conexion _______________*/
 $idcarrito=$_POST["id"];
 $status="cancelado";
 $resultado=array();
  $consulta="UPDATE carrito SET status='$status' WHERE id_carrito=$idcarrito";
   if($stmt=$mysqli->prepare($consulta))
{
    //$stmt->bind_param("si",$status,$idcarrito);
    $stmt->execute();
    if($stmt->errno==true)
        {
            $resultado['status']='error';
        }
    else
        {
            $resultado['status']='ok';
        }
    $stmt->close();
}
else
{
    $resultado['status']='error al preparar la consulta PHP ERROR CODE ';
}
 echo json_encode($resultado);
//echo "el ide del carrito ".$idcarrito;
$mysqli->close();
?>
that code actually works, prints $resultado['status']=ok, but i think is not the point, because as soon as i change for this $consulta="UPDATE carrito SET status=? WHERE id_carrito=?"; with respective bind_param it stops working, i mean prints $resultado['status']=ok, but doesn't make anychange at database

推荐答案

我写了一个扩展mysqli的类.它会自动处理所有bind_params并使得使用mysqli变得容易.您可以在此处下载该类: better_mysqli.php

I wrote a class that extends mysqli. It handles all the bind_params automatically and makes using mysqli easy. You can download the class here: better_mysqli.php

此页面显示其基本用法: basic_usage.php

This page shows it's basic usage: basic_usage.php

此页面显示详细用法: detailed_usage.php

This page shows detailed usage: detailed_usage.php

这篇关于PHP的bind_param无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆