如何从PHP代码中调用MySQL存储过程?

时间:2010-10-19 09:00:47

标签: php mysql stored-procedures mysqli user-defined-functions

我有我在MySQL中创建的存储过程,并希望PHP调用该存储过程。这样做的最佳方式是什么?

-MySQL客户端版本:4.1.11
-MySQL Server版本:5.0.45

这是我的存储过程:

DELIMITER $$

DROP FUNCTION IF EXISTS `getNodeName` $$
CREATE FUNCTION `getTreeNodeName`(`nid` int) RETURNS varchar(25) CHARSET utf8
BEGIN
 DECLARE nodeName varchar(25);
 SELECT name into nodeName FROM tree
 WHERE id = nid;
 RETURN nodeName;
END $$

DELIMITER ;

调用getTreeNodeName过程的PHP代码是什么?

7 个答案:

答案 0 :(得分:47)

我现在使用mysqli代替mysql找到解决方案。

<?php 

  //connect to database
  $connection = mysqli_connect("hostname", "user", "password", "db", "port");

  //run the store proc
  $result = mysqli_query($connection, 
     "CALL StoreProcName") or die("Query fail: " . mysqli_error());

  //loop the result set
  while ($row = mysqli_fetch_array($result)){   
      echo $row[0] . " - " . + $row[1]; 
  }

?>

我发现许多人似乎在使用mysql_connect, mysql_query and mysql_fetch_array时遇到了问题。

答案 1 :(得分:14)

您可以使用以下语法调用存储过程:

$result = mysql_query('CALL getNodeChildren(2)');

答案 2 :(得分:4)

<?php
    $res = mysql_query('SELECT getTreeNodeName(1) AS result');
    if ($res === false) {
        echo mysql_errno().': '.mysql_error();
    }
    while ($obj = mysql_fetch_object($res)) {
        echo $obj->result;
    }

答案 3 :(得分:2)

答案 4 :(得分:1)

这是我使用预处理语句的解决方案,存储过程返回多行,而不仅仅是一个值。

<?php

require 'config.php';
header('Content-type:application/json');
$connection->set_charset('utf8');

$mIds = $_GET['ids'];

$stmt = $connection->prepare("CALL sp_takes_string_returns_table(?)");
$stmt->bind_param("s", $mIds);

$stmt->execute();

$result = $stmt->get_result();
$response = $result->fetch_all(MYSQLI_ASSOC);
echo json_encode($response);

$stmt->close();
$connection->close();

答案 5 :(得分:-1)

以下代码显示,如何使用示例

调用存储过程
$con = mysql_connect('localhost','root','');  
mysql_select_db('books'); 
//Call the proc() procedure follow
$result= mysql_query("CALL proc();") or die(mysql_error()); 

while($row = mysql_fetch_row($result))
{
for($i=0;$i<=6;$i++)
{ 
echo  $row[$i]."<br>"; 
}  
}  
mysql_close($con); 

答案 6 :(得分:-2)

我从php.net获得解决方案

<?php
$mysqli = new mysqli("localhost", "root", "", "joomla2.5");
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

/*if (!$mysqli->query("DROP TABLE IF EXISTS test") ||
    !$mysqli->query("CREATE TABLE test(id INT)") ||
    !$mysqli->query("INSERT INTO test(id) VALUES (1), (2), (3)")) {
    echo "Table creation failed: (" . $mysqli->errno . ") " . $mysqli->error;
}

if (!$mysqli->query("DROP PROCEDURE IF EXISTS p") ||
    !$mysqli->query('CREATE PROCEDURE p() READS SQL DATA BEGIN SELECT id FROM test; SELECT id + 1 FROM test; END;')) {
    echo "Stored procedure creation failed: (" . $mysqli->errno . ") " . $mysqli->error;
}

if (!$mysqli->multi_query("CALL p()")) {
    echo "CALL failed: (" . $mysqli->errno . ") " . $mysqli->error;
}

do {
    if ($res = $mysqli->store_result()) {
        printf("---\n");
        var_dump($res->fetch_all());
        $res->free();
    } else {
        if ($mysqli->errno) {
            echo "Store failed: (" . $mysqli->errno . ") " . $mysqli->error;
        }
    }
} while ($mysqli->more_results() && $mysqli->next_result());

&GT;