从php运行mysql存储过程

时间:2014-03-30 15:48:46

标签: 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,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);
?>

绝对没有任何反应......没有错误信息或任何其他问题迹象。它只是不运行程序。 (这是一个更新/插入例程)。

我正在使用此网址...

updateCodeTable.php?codeid=0&codetable=TABLE&desription=testing2%20entry&code=TEST1&groupcode=gcode&t1=t1&t2=t2&t3=t3

...但是,如果我在phpMyAdmin中运行此查询,它会完美运行....

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

我可以包含存储过程代码,但是它可以在我直接运行它时运行正常,但只是没有从我的php代码中成功运行。

2 个答案:

答案 0 :(得分:0)

每个mysqli *函数/方法都可能失败。测试返回值和/或将报告机制切换为异常,请参阅http://docs.php.net/mysqli-driver.report-mode

<?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');
    }
}

答案 1 :(得分:-1)

你可以尝试一下吗?

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