Below image contain my code.
here, only one procedure is invoked at a time.Both procedure are not working .
here if state procedure is commented then city procedure called.
and if city in commented then State procedure is working.
for example , you can call multiple stored procedure like this below code
$db = mysqli_connect([...]);
$r = mysqli_query($db, " CALL getSomething(2); ");
while($row = mysqli_fetch_assoc($r)) {
print_r($row);
}
mysqli_free_result($r);
mysqli_next_result($db);
$r = mysqli_query($db, " CALL getSomethingElse(); ");
while($row = mysqli_fetch_assoc($r)) {
print_r($row);
}
mysqli_free_result($r);
mysqli_next_result($db);
mysqli_close($db);
it is important to use the functions mysqli_free_result() and mysqli_next_result() after the MySQL stored procedure is called, otherwise, the code will not work and you may see an error like "Commands out of sync; you can't run this command now".
Related
I am using procedure in php. There are two procedure. First procedure is calling but second procedure is not working. When I comment the first procedure then second is working.
I don't know the that two procedure are called at a time or not. please tell me the way. I am new for procedure. Please help me.
if(isset($_REQUEST['customer'])){
$customer_name = $_POST['customer_name'];
$lang = 'en';
$qry = mysql_query("call customer (NULL,'$customer_name','$lang')")
or mysql_error();//first procedure
$data = mysql_num_rows($qry);
if($data > 0){
$msg ="Customer already exists.";
}
else{
mysql_query("call insertCustomer(NULL,'$customer_name','','','',1,'','','','',#ret,#err_code)")
or mysql_error();//second procedure
$rs = mysql_query('SELECT #p_ret,#p_err_code')or mysql_error();
$data = mysql_fetch_array($rs);
echo $data['#p_err_code'];
echo $data['#p_ret'];
}
}
First of all you should not be using mysql but mysqli or PDO - mysql is deprecated and support for this will be withdrawn.
Secondly your question is not specific: the second procedure might not be called at all because if ($data > 0) returns true and in this case that would be ok.
Try to improve your code for debugging purposes - your calls to mysql_error() are pointless as the function returns error as a string or empty string if no error but you don't echo it out or assign it to any variables. Also would be good to save the result of mysql_query (mysqli_query rather) to a variable and then test if it worked or not.
I am using the mysqli functions (mysqli_connect, mysqli_select_db, mysqli_query) to call 1 select query and 2 stored procedures.
It seems that when I am using the same $connection (returned by mysqli_connect) multiple times, I am getting the following error message: "Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in..."
Below is my code:
<?php
$server="localhost";
$user="user";
$pass="pass";
$db="db";
$connection=mysqli_connect("$server","$user","$pass");
mysqli_select_db($connection, "$db") or die('Unable to select database.');
//First SELECT using $connection
$query=mysqli_query($connection, "SELECT item_name FROM items ORDER BY item_name DESC");
While ($result=mysqli_fetch_array($query,MYSQL_NUM))
{
$complete_result[] = $result[0];
$total_rows = $total_rows + 1;
}
//CALL to first sp using $connection
$query2 = mysqli_query($connection, "CALL sp_check_edits_remaining()");
while ($row2 = mysqli_fetch_array($query2, MYSQL_ASSOC)) {
$edits_remaining = $row2['edits_remaining'];
} // End while
//CALL to second sp using $connection
$query3 = mysqli_query($connection, "CALL sp_edit_data");
while ($row3 = mysqli_fetch_array($query3, MYSQL_ASSOC)) {);
$edits_id = $row3['id'];
} // End while
?>
Like I described, when I call the second sp, the above code gives me the error message mentioned above. (Please note that the connection is never closed.)
However, when I create another connection and provide it to the second sp call, this error disappears. This is shown in the code below
$connection2=mysqli_connect("$server","$user","$pass");
mysqli_select_db($connection2, "$db") or die('Unable to select database.');
//CALL to second sp using $connection
$query3 = mysqli_query($connection2, "CALL sp_edit_data");
while ($row3 = mysqli_fetch_array($query3, MYSQL_ASSOC)) {
$edits_id = $row3['id'];
} // End while
Can anyone please help me why this unexpected behavior?
Thanks and in advance!
It seems I have found a solution, which might be specific to my scenario.
My stored procs return only one row in the resultset.
So, after the CALL to the first sp and the corresponding while loop, I have simply added:
mysqli_next_result($connection);
This has removed the error message/warning I was receiving.
Anyone wants to comment whether this is the 'professional' approach?
You have an error somewhere, causing one of the mysql functions (probably the query call(s)) to return a boolean false, which you then blindly use in a fetch call. You need to add extra error handling, e.g.
$query = mysqli_query($connection, "...") or die(mysqli_error($connection));
never assume a query has succeeded.
enter code hereafter the query is finished, you must close the $connection then for another query, connect and assign the $connection again.
mysqli_close($connection);
$connection=mysqli_connect(bla,bla,bla,bla).
I am new to php but not as a programmer...
I am having difficulty calling and displaying the content when I call a procedure more than once in a page. I am trying to display two separate record sets from two different SP calls for MYSQL. I can display the first call but the second fails. I'm not sure what I am doing wrong but perhaps someone can kind help?
I keep getting the error when I call the second procedure:
Error calling SPCommands out of sync; you can't run this command now
I'm running on windows btw
Code below... PHP
// First call to SP
$page = 2;
$section = 1;
include("DatabaseConnection.php"); //general connection - works fine
$sql = 'CALL GetPageContent("'.$page.'", "'.$section.'")';
$result = mysqli_query($conn, $sql) or die('Error calling SP' .mysqli_error($conn));
while($row=mysqli_fetch_assoc($result))
{
// DO STUFF< REMOVED TO MAKE READING CLEARER
}
mysqli_free_result($result);
//SECOND CALL BELOW
$section = 2; // change parameter for different results
$sql = 'CALL GetPageContent("'.$page.'", "'.$section.'")';
$result = mysqli_query($conn, $sql) or die('Error calling SP' .mysqli_error($conn));
while($row=mysql_fetch_assoc($result))
{
// DO STUFF< REMOVED TO MAKE READING CLEARER
}
mysqli_free_result($result);
The trouble is that your SP is giving you multiple results.
Use the mysqli_multi_query, see http://us2.php.net/mysqli_multi_query
i have written a basic stored procedure using mysql
DELIMITER //
CREATE PROCEDURE `sp_sel_test`()
BEGIN
SELECT * FROM category c;
END//
DELIMITER ;
now i m calling it from php
the php code is:
<?php
$txt = $_GET['id'];
$name = $_GET['name'];
$con = mysql_connect("localhost","four","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("fourthes_a", $con);
//$result = mysql_query("select * from new_c where name like %". $name ."% or c_name like %" . $name . "% order by name asc;");
$result = mysql_query("call sp_sel_test()");
if ($result === FALSE) {
die(mysql_error());
}
while($row = mysql_fetch_array($result))
{
echo $row['category_id'] . " " . $row['c_name'];
?>
<br />
<?php
}
mysql_close($con);
echo $txt;
?>
now its giving the error
PROCEDURE fourthes_a.sp_sel_test can't return a result set in the given context
mysql_query() returns false when the query fails. You didn't check if your sproc query succeeded, so most likely you're passing that boolean FALSE to the fetch function, which is rightfully complaining.
Rewrite your code like this, as a bare mininum, for proper error handling:
$res = mysql_query('call sp_sel_test()');
if ($res === FALSE) {
die(mysql_error());
}
Never ever assume a query succeeded. Even if the SQL syntax is perfect, there's far too many other reasons for a query to fail to NOT check if it worked.
You need to set client flags while connecting for using stored procedures with php. Use this:
mysql_connect($this->h,$this->u,$this->p,false,65536);
See MySQL Client Flags for more details. PHP MySQL does not allow you to run multiple statements in single query. To overcome this you must tell PHP to allow such queries by setting CLIENT_MULTI_STATEMENTS flag in your connection.
I know last answer was a year ago, but...
CREATE PROCEDURE sp_sel_test(OUT yourscalarvariable INT/TEXT...)
Statements that return a result set cannot be used within a stored function. This includes SELECT statements that do not use INTO to fetch column values into variables, SHOW statements, and other statements such as EXPLAIN. For statements that can be determined at function definition time to return a result set, a Not allowed to return a result set from a function error occurs (ER_SP_NO_RETSET_IN_FUNC). For statements that can be determined only at runtime to return a result set, a PROCEDURE %s can't return a result set in the given context error occurs (ER_SP_BADSELECT).
So, your select shoould be like this:
SELECT * FROM category **INTO** c;
http://www.cs.duke.edu/csl/docs/mysql-refman/stored-procedures.html
Got the following simple query which works fine through phpmyadmin but when I add it to my php website no results are returned and no error/warning messages either. If I remove "SET #N=-1;" then it works fine.
<?php
$db_connect = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD, true);
mysql_select_db(DB_NAME, $db_connect);
$test_query = mysql_query("SET #N=-1;SELECT `id`, (#N:=#N+1) AS `mycount` FROM `mydb`;");
for ($i = 0; $i <= mysql_num_rows($test_query)-1; $i++) {
echo mysql_result($db_directorymap, $i, 0) . " " . mysql_result($db_directorymap, $i, 1) . "<br />";
}
?>
UPDATE: I just moved to mysqli but of course I'm still having a problem with the mysql statement and mysqli_multi_query. It seems when it runs the first part of the query the results returned are empty thus a boolean error is given. I'm guessing I have to skip the first set of results but I don't know how to do that?
It's because the mysql_query function will only accept one query, but you've given it two, separated by a semicolon. Try either:
Running each query separately (don't know if this will work):
mysql_query( "SET #N=-1" );
mysql_query( "SELECT `id`, (#N:=#N+1) AS `mycount` FROM `mydb`" );
Using mysqli with the multi_query function (or a PDO equivalent if there is one).
To answer your updated question: check the PHP manual page for multi_query. I think you'll want to use mysqli::next_result. Something like this, using procedural style:
mysqli_multi_query($link, $query);
mysqli_next_result($link);
if ($result = mysqli_store_result($link)) {
while ($row = mysqli_fetch_row($result)) {
printf("%s\n", $row[0]);
}
mysqli_free_result($result);
}
Multiple queries via mysql_query aren't supported, so I'd guess that it's only executing the SET command, and not the subsequent SELECT command.