从php运行mysql存储过程

从php运行mysql存储过程,php,mysql,prepared-statement,stored-functions,Php,Mysql,Prepared Statement,Stored Functions,我一定是在这个代码中做错了什么 <? $codeid=$_GET["codeid"]; $tablecode=$_GET["tablecode"]; $description=$_GET["description"]; $code=$_GET["code"]; $groupcode=$_GET["groupcode"]; $t1=$_GET["t1"]; $t2=$_GET["t2"]; $t3=$_GET["t3"]; $mysqli = new mysqli(dbhost,dbus

我一定是在这个代码中做错了什么

<? 
$codeid=$_GET["codeid"];
$tablecode=$_GET["tablecode"];
$description=$_GET["description"];
$code=$_GET["code"];
$groupcode=$_GET["groupcode"];
$t1=$_GET["t1"];
$t2=$_GET["t2"];
$t3=$_GET["t3"];

$mysqli = new mysqli(dbhost,dbuser,dbpass,dbc);
if ($mysqli->connect_error) {
    die('Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error);
    }

$q="call spUpdateCodeTable(?,?,?,?,?,?,?,?)";
$stmt = $mysqli->prepare($q);
$stmt->bind_param($codeid,$tablecode,$description,$code,$groupcode,$t1,$t2,$t3);
$stmt->execute();

mysql_close($mysqli);
?>
…但是,如果我在phpMyAdmin中运行this查询,它将运行得非常完美

call spUpdateCodeTable(0,'TABLE','testing2','TEST1','group','','','');

我可以包含存储过程代码,但只要我直接运行它,它就可以正常运行,但不能从我的php代码成功运行。

您可以尝试一下吗

mysqli->query("call spUpdateCodeTable($codeid,'$tablecode',
 '$description','$code','$groupcode','$t1','$t2','$t3')");

每个mysqli*函数/方法都可能失败。测试返回值和/或将报告机制切换为异常,请参阅


使用
mysqli\u报告重试(mysqli\u报告严格)$q=“..
-使错过sql错误变得更加困难。请看,我可以问一下,为什么要使用过程?因为我是一个长期从事ms SQL server的人,在这里存储过程可以完成大部分工作。我仍然认为,应该尽可能多地在服务器上进行处理。但是对于php和MySQL来说是新手,所以谁知道一年后我会怎么想呢。你不觉得这太过分了吗?你的常识是:我预感
太过分了
你不是指代码的质量;-)但是“做别人的工作”部分。坦率地说,我已经放弃了参考好的实践,希望每个人都能广泛地学习它们,永远不会发生。所以我对mabye很满意,只是可能在某些方面提高了基准,不让太多人带着非常本地化的答案离开,他们仍然不知道他们的脚本中有大量的基本缺陷。希望有一个“阅读后燃烧”投票的问题和答案等。我的大部分答案都是这样的。非常感谢。我将进一步调查返回的错误信息。问题出在调用URL中。第二个参数应该是“tablecode”,但我有“codetable”。修正了这个问题,它就这样运行了。我确实学到了很多关于验证和错误捕获的知识,对此我真的非常感谢。在这种情况下,我只需要再观察一次:您使用GET作为传输方法,而您存储的产品名为
spUpdateCodeTable
,因此显然来自客户机的数据用于更改模型/数据库中的数据。在这种情况下,你应该使用POST而不是GET,这会破坏使用准备语句的全部目的Amit:是的,这是我以前会做的,但我正在努力学习使用准备语句。我甚至不能告诉你现在使用它们的好处,除非我已经读到,这是现在似乎更喜欢的。不过,在这种情况下,问题在于拼写错误,而不是逻辑……我应该说,问题在于拼写错误和我缺乏错误检查。
mysqli->query("call spUpdateCodeTable($codeid,'$tablecode',
 '$description','$code','$groupcode','$t1','$t2','$t3')");
<?php
// probably better done with http://docs.php.net/filter but anyway ...just check whether all those parameters are really there
// you are positive that GET is the correct method for this action?
if ( !isset($_GET["codeid"], $_GET["tablecode"], $_GET["description"], $_GET["code"], $_GET["groupcode"], $_GET["t1"], $_GET["t2"], $_GET["t3"]) ) {
    // die() is such a crude method
    // but bare me, it's just an example....
    // see e.g. http://docs.php.net/trigger_error
    die('missing parameter');
}
else {
    $mysqli = new mysqli(dbhost,dbuser,dbpass,dbc); 
    if ($mysqli->connect_error) {
        die('Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error);
    }

    $q="call spUpdateCodeTable(?,?,?,?,?,?,?,?)";
    $stmt = $mysqli->prepare($q);
    if ( !$stmt ) {
        // $mysqli->error has more info
        die('prepare failed');
    }
    // you have to provide the format of each parameter in the first parameter to bind_param
    // I just set them all to strings, better check that
    if ( !$stmt->bind_param('ssssssss', $_GET['codeid'], $_GET['tablecode'], $_GET['description'], $_GET['code'], $_GET['groupcode'], $_GET['t1'], $_GET['t2'], $_GET['t3']) ) {
        // $stmt->error has more info
        die('bind failed');
    }

    if ( !$stmt->execute() ) {
        // $stmt->error has more info
        die('execute failed');
    }
}