PHP MySQL PDO SQLSTATE [HY000]:一般错误

时间:2017-01-31 20:09:17

标签: php mysql pdo

请帮助修复" SQLSTATE [HY000]:一般错误:"在以下PHP脚本中。另外,请参考MySQL脚本。

<?php

# MySQL 
$host = 'localhost';
$username = 'root';
$password = '';
$dbname = 'procdb';

try {
    $pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);

    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    echo "Connected to MySQL Server successfully." . "\n";

    $sql = "CALL prepend('abcdefg', @inOutParam);";
    $stmt = $pdo->query($sql);
    do {
        $rows = $stmt->fetchAll(PDO::FETCH_NUM);
        if ($rows) {
        foreach($rows as $row) {
              print($row[0] . "\n");
            }  
        }
    } while ($stmt->nextRowset());

} catch (PDOException $e) {
    #die("Could not connect to the database $dbname :" . $e->getMessage());
    $error = $e->getMessage();
    echo $error . "\n";
} catch (Exception $e) {
    $error = $e->getMessage();
    echo $error . "\n";
} finally {
    $pdo = null;
    echo "Connection closed." . "\n";
}   

?>

这是MySQL脚本:

DROP DATABASE IF EXISTS procdb;
DELIMITER $$
CREATE DATABASE procdb;$$
DELIMITER ;

USE procdb;
DROP PROCEDURE IF EXISTS prepend;
DELIMITER $$
CREATE PROCEDURE prepend
(
 IN inParam VARCHAR(255), 
 INOUT inOutParam INT
)
BEGIN
 DECLARE z INT;
 SET z = inOutParam + 1;
 SET inOutParam = z;
 SELECT inParam;
 SELECT CONCAT('zyxw', inParam);
END;$$
DELIMITER ;

USE procdb;
CALL prepend('abcdefg', @inOutParam);

/*
# Output 
// (FieldName1 and its value)
inParam
'abcdefg'
// (FieldName2 and its value)
CONCAT('zyxw', inParam)
'zyxwabcdefg'    
*/

错误的原因是什么?请注意,添加&#34; $ stmt-&gt; close();&#34;或&#34; $ stmt-&gt; closeCursor();&#34;没有帮助。

请帮忙。

由于

1 个答案:

答案 0 :(得分:0)

试试这个,行

 $rows = $stmt->fetchAll(PDO::FETCH_NUM);

外部循环

try {
 $pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
 $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 echo "Connected to MySQL Server successfully." . "\n";
 $sql = "CALL prepend('abcdefg', @inOutParam);";
 $stmt = $pdo->query($sql);
 $rows = $stmt->fetchAll(\PDO::FETCH_ASSOC);
 foreach ($rows as $row) {
    foreach ($row as $col) {
        print $col . "\n";
    }
 }
 $stmt->nextRowset();
 $rows = $stmt->fetchAll(\PDO::FETCH_ASSOC);
 foreach ($rows as $row) {
     foreach ($row as $col) {
        print $col . "\n";
    }
  }
 } catch (PDOException $e) {
   #die("Could not connect to the database $dbname :" . $e->getMessage());
   $error = $e->getMessage();
   echo $error . "\n";
 } catch (Exception $e) {
   $error = $e->getMessage();
   echo $error . "\n";
 } finally {
   $pdo = null;
   echo "Connection closed." . "\n";
 }