0

I have PHP code that calls stored procedures from a database.

<?php
include("DataBase.php");
$sql=$mysqli_query("CALL sp()");
if( $sql === FALSE ) {
trigger_error('Query failed returning error: '. $mysqli_error(),E_USER_ERROR);
} else {
while($row=$mysqli_fetch_array($sql))
{
echo $row['id'].'--'.$row['item'].'';
}
}
?>

DB Conection;

<?php
$mysqli_hostname = "localhost";
$mysqli_user = "root";
$mysqli_password = "";
$mysqli_database = "liveedit";
$bd = $mysqli_connect($mysqli_hostname, $mysqli_user, $mysqli_password) 
or die("Opps some thing went wrong");
$mysqli_select_db($mysqli_database, $bd) or die("Opps some thing went wrong");
?>

The code returns this error:

Fatal error: Function name must be a string in C:\xampp\htdocs\ambot\database.php on line 6

What's wrong with the code? I tried to fix this one but fixing this using stored procedure in my code adds difficulties.

  • Your mysql_query call is encountering an error, hence why it is returning `FALSE`. See php's [mysql_query](http://php.net/manual/en/function.mysql-query.php) documentation. Are you sure your query is correct? – Aiias Mar 10 '13 at 06:45
  • ive updated my post above. check the query and the error now is different. Check it out.. – Soon To Be Revealed Mar 10 '13 at 06:53
  • Are you logging in as `root` in `include("DataBase.php");`? You might want to set the `Definer` to be a little more leniant for your procedure. See the MySQL documentation on [Procedures syntax](http://dev.mysql.com/doc/refman/5.0/en/create-procedure.html). – Aiias Mar 10 '13 at 06:58
  • yup, the user is root. What do you mean by that? – Soon To Be Revealed Mar 10 '13 at 07:00
  • Is this a bug in your php version? See the possibly related post http://stackoverflow.com/questions/1200193/cant-return-a-result-set-in-the-given-context. – Aiias Mar 10 '13 at 07:02
  • Your code works fine on my machine. It might be a bug as @Aiias said. You can try to switch to `mysqli_*` – peterm Mar 10 '13 at 07:07
  • i think it's not. i'm using php 5.3.1 and it is latest version. everything is right here, right? My stored procedure created well, i just don't know why is this happening. – Soon To Be Revealed Mar 10 '13 at 07:08
  • @peterm:i changed the query to $mysqli_query() but it dd not work either. – Soon To Be Revealed Mar 10 '13 at 07:11
  • @SoonToBeRevealed I hope you changed connection code also. Did you? **All** `mysql_*` functions should be changed to their `mysqli_*` analogs. Does it give you the same error? – peterm Mar 10 '13 at 07:12
  • @peterm:here's the error: Fatal error: Function name must be a string in C:\xampp\htdocs\ambot\results.php on line 3. – Soon To Be Revealed Mar 10 '13 at 07:14
  • @SoonToBeRevealed See my updated prev comment. You can't just change one function call from one extention to another. What you can do is to comment out `include("DataBase.php");` temporarily, put `mysqli_*` connection code right in here for testing purpose and see what you get. You will have to also change `mysql_fetch_array` to `mysqli_fetch_array`. – peterm Mar 10 '13 at 07:18

1 Answers1

0

Your code works fine on my machine.

You can try to switch mysqli_* extension, at least for testing purposes. To that end change your code that way and make sure that your connection info is correct (host, username, password, database name).

<?php
//include("DataBase.php");

$db = mysqli_connect("localhost","user","password", "dbname");
if (!$db) {
    die('Could not connect: ' . mysqli_error());
}

$sql = mysqli_query($db, "CALL sp()");
if( $sql === FALSE ) {
   die('Query failed returning error: '. mysqli_error());
} else {
       while($row=mysqli_fetch_array($sql))
       {
        echo $row['id']. "<br>";
       }
   }
?>
peterm
  • 91,357
  • 15
  • 148
  • 157