Call count result from stored procedure MySQL in PHP - php

I'm trying to get a value from a stored procedure in php but I can't do it.
My stored procedure:
DROP PROCEDURE IF EXISTS sp_table;
DELIMITER $$
CREATE PROCEDURE sp_table()
BEGIN
SELECT COUNT(*) FROM table;
END$$
DELIMITER ;
My PHP code:
$recordSet_table = $conn->query("CALL sp_table()");
print_r($recordSet_table)."<br><br>";

Please try following code.
$sql = mysqli_query($connectionVariable,"CALL sp_table(#count)");
$result = mysqli_fetch_array($sql);

Related

get output value from mysql stored-procedure to php?

how i can get output value from stored procedure to php variable ..
example Stored procedure :
DROP PROCEDURE `test`; CREATE DEFINER=`mydbinstance`#`%` PROCEDURE `test`(OUT `max` INT(11)) NOT DETERMINISTIC MODIFIES SQL DATA SQL SECURITY DEFINER
BEGIN
SELECT MAX(slno)+1 INTO #max FROM table1;
SELECT #max;
END
php:
$stmt=mysqli_query($conn,"CALL test();");
You should be able to get the result(s) of a stored procedure the same way you would any other query:
$stmt = mysqli_query($conn,"CALL test();");
if ($row = mysqli_fetch_row($stmt)) {
$value = $row[0];
}

concatenation in stored procedure mysql

I want to create concatenation for store procedure mysql.
in php I can use (.) for concatinating the variable
example :
$table = "table_".$_GET["number"];
$sql = mysqli_query($conn,select * from `".$table."` where `field`='$field');
in stored procedure mysql, I don't know how to concatinate the variable.
this is my stored procedure script.
CREATE DEFINER=`user`#`localhost` PROCEDURE `testprocedure`(v_number int(5))
BEGIN
declare v_table varchar(20);
set v_table = 'table_'.v_number;
update `v_table` set `field`=v_number where `field2`='test';
END $$
DELIMITER ;
so how can I add v_number to set v_table then add v_table to update table query?
Use CONCAT : set v_table = CONCAT('table_',v_number);

Stored procedure does not return values

Here is my stored procedure returned in MySQL
DELIMITER //
CREATE PROCEDURE get_content(content1)
BEGIN
SELECT content into content1
FROM page_content
WHERE id= 1;
/*END ;*/
END //
DELIMITER ;
I am using following code to get values using stored procedure but its not returning anything, I just want to know if in case I am missing something. Here is my PHP code
$rs = mysql_query('CALL get_content(#content1)' );
$rs = mysql_query('SELECT #content1' );
while($row = mysql_fetch_object($rs))
{
print_r($row);
}
Please help me, thanks in advance
Sneha
You need to use output parameter with SP.
DELIMITER //
CREATE PROCEDURE get_content(out content1 varchar(500))
BEGIN
SELECT content into content1 FROM page_content WHERE id= 1;
END//
DELIMITER ;
Call it by below method.
SET #content1 =NULL;
CALL `get_content`(#content1)
SELECT #content1;

How to create a stored procedure in mysql

I have created the following stored procedure in mysql...
DELIMITER //
CREATE PROCEDURE GetMember(IN in_memberID int)
BEGIN
SELECT *
FROM Members
WHERE MemberID = in_memberID;
END//
$result = mysql_query("CALL GetMember(".$memberID.")") or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
echo $row['Name'] . "</br>";
}
But when I call it from php it returns all records in the Members table, what am I doing wrong?
EDIT:
When I try to call the query within phpmyadmin I get this error
CALL GetMember(1);
#1312 - PROCEDURE myDb.GetMember can't return a result set in the given context
what version of PHP?
PHP 5.2.3 and PHP 5.2.4 have a bug with procedures:
https://bugs.php.net/bug.php?id=42548
use Database_Name;
DELIMETER $$
DROP PROCEDURE IF EXISTS Proc$$
CREATE PROCEDURE Proc()
BEGIN
DECLARE x INT;
SET x = 1;
WHILE x <= 110000 DO
INSERT INTO Table_Name (word, mean) VALUES ('a', 'a mean');
SET x = x + 1;
END WHILE;
END$$
DELIMITER ;

Mysql Stored procedures

How stored procedures are fast in Mysql .
Where it is suitable to use stored procedures (insert,update,select)
How can we make stored procedure of a complex query returning 100000 rows and how can we get these records on php form.
any example of such stored procedure using select query returning multiple rows ?
well for example we will work with the following mysql procedure:
DELIMITER $$
DROP PROCEDURE IF EXISTS `ejemplos`.`queryEmployees` $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `queryEmployees`(IN param VARCHAR(1))
BEGIN
IF param='1' THEN
SELECT * FROM empleados;
END IF;
END $$
DELIMITER ;
And we get the records in php as the following :
<?php
$mysqli = new mysqli('localhost', 'root', '1234', 'ejemplos');
$resultado_consulta = $mysqli->query("CALL queryEmployees('1')");
?>
<table>
<?php
while ($registro = $resultado_consulta->fetch_object())
{
?><tr><td><?php echo $registro->first_name;?> </td></tr><?php
}
?>
</table>
I hope this can help u...

Categories