在mysql中新建了如下存储过程
mysql>delimiter //
->CREATE PROCEDURE `myProc`()
->begin
->select * from test_table ;
->end;
->//
mysql>delimiter ;
mysql>call myProc();
以上代码在mysql中执行一切正常;
我想在php中调用该存储过程用下面语句(数据库的连接都正常)
$rs = mysql_query("call myProc()");
while($rs1=mysql_fetch_array($rs))
{
echo "id=".$rs1[id]." \n ";
echo "studentno=".$rs1[studentno]."\n";
echo "score=".$rs1[score]."\n";
echo "time=".$rs1[time]."<br>";
}
却总是报如下错误:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Program Files\Apache Group\Apache2\htdocs\test_0813.php on line 159
不知道从php中该如何调用这个存储过程?谢谢高手能给予指点!
mysql>delimiter //
->CREATE PROCEDURE `myProc`()
->begin
->select * from test_table ;
->end;
->//
mysql>delimiter ;
mysql>call myProc();
以上代码在mysql中执行一切正常;
我想在php中调用该存储过程用下面语句(数据库的连接都正常)
$rs = mysql_query("call myProc()");
while($rs1=mysql_fetch_array($rs))
{
echo "id=".$rs1[id]." \n ";
echo "studentno=".$rs1[studentno]."\n";
echo "score=".$rs1[score]."\n";
echo "time=".$rs1[time]."<br>";
}
却总是报如下错误:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Program Files\Apache Group\Apache2\htdocs\test_0813.php on line 159
不知道从php中该如何调用这个存储过程?谢谢高手能给予指点!
这个是在命令行执行的结果:
mysql > delimiter ¦ ¦
mysql > create procedure myProc()
- > begin
- > select * from auto_t;
- > end ¦ ¦
Query OK, 0 rows affected (0.00 sec)
mysql > delimiter ;
mysql > call myProc();
+----+---------+
¦ id ¦ name ¦
+----+---------+
¦ 1 ¦ 3 ¦
¦ 2 ¦ 5 ¦
¦ 3 ¦ sdflsjf ¦
+----+---------+
3 rows in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
mysql >
<?php
$DOC_ROOT = $_SERVER['DOCUMENT_ROOT'];
//require_once($DOC_ROOT. "/config/config.php ");
define ('MY_HOST','localhost');
define ('MY_USER','webuser');
define ('MY_PASS','test');
define ('MY_DB','test');
define ('MY_PORT','3307');
//define (MY_SOCKET,'/tmp/mysql_sock');
class My_Mysqli{
var $mysqli;
var $row;
var $count;
var $affected_row_sql;
var $affected_row_sp;
function My_Mysqli(){
$this- >mysqli = new mysqli(MY_HOST, MY_USER, MY_PASS, MY_DB, MY_PORT);
// $this- >mysqli = new mysqli(MY_HOST, MY_USER, MY_PASS, MY_DB, MY_PORT,MY_SOCKET);
if (mysqli_connect_errno()){
die( "Connect failed: ".mysqli_connect_error());
}
$this- >row = null;
$this- >count = 0;
//$this- >next_row = array();
}
function call_sql($sql){
$this- >mysqli- >query($sql);
$this- >affected_row_sql = $this- >mysqli- >affected_rows;
}
function call_sp($query){
$this- >mysqli- >multi_query($query);
do {
if ($result = $this- >mysqli- >use_result()){
while ($row = $result- >fetch_row()){
$this- >row[$this- >count++] = $row;
}
$result- >close();
}
} while($this- >mysqli- >next_result());
$this- >affected_row_sp = $this- >mysqli- >affected_rows;
}
function close_sp(){
$this- >mysqli- >close();
}
}
$mysqli = new My_Mysqli();
$query = 'call myProc();';
$mysqli- >call_sp($query);
$mysqli- >close_sp();
print ' <pre >';
print_r($mysqli- >row);
print ' </pre >';
? >
这个是在WEB页面上执行的结果:
Array
(
[0] = > Array
(
[0] = > 1
[1] = > 3
)
[1] = > Array
(
[0] = > 2
[1] = > 5
)
[2] = > Array
(
[0] = > 3
[1] = > sdflsjf
)
)
得好好研究下 呵呵 怎么给分的啊 第一次来还不是很清楚的哦