Regarding functions with stored procedures in php code

Discussion in 'PHP' started by nanna_vijayalaxmi, Dec 26, 2007.

  1. #1
    Hi All ,
    I need small help regarding functions.I wrote a code for retrieving values from database using functions.We are having nearly 20 functions in our application.But when I called each function,I have given connection details as a parameter or mentioned in the each function .So that loading time of the page is so long i.e. the page is displaying nearly 5-6 minutes.But I have to display the page with 5-6 secs.

    Here is sample application what i was written


    Samplefun.php:


    <?php

    $db->connect("localhost","root","","vijaya");
    include('Functions.php');
    $url="www.sciencedaily.com";
    $rating=Getrating($url);
    echo $rating; echo"<br/>";
    $assignedval=GetAssignedvalue($url,$rating);
    echo $assignedval;
    ?>


    Functions.php:


    <?php

    function Getrating($url)
    {
    $db = new mysqli();
    $db->connect("localhost","root","vijaya","vijaya");

    $stmt=$db->prepare( "call rating('$url')") ;
    // mysqli_stmt_bind_param($stmt, "s", $url);
    mysqli_stmt_execute($stmt);
    $result=$stmt->bind_result($rating);

    mysqli_stmt_fetch($stmt);
    $stmt->close();
    return $rating;

    }
    function GetAssignedvalue($url,$rating)
    {

    $stmt1=$db->prepare( "call Get_Assigned_value('$url',$rating)") ;
    //mysqli_stmt_bind_param($stmt1, "sd", $url,$district);
    // mysqli_stmt_bind_param($stmt1, "d", $district);
    mysqli_stmt_execute($stmt1);
    $result=$stmt1->bind_result($values);
    mysqli_stmt_bind_result($stmt1, $values);
    mysqli_stmt_fetch($stmt1);
    //echo"<br/>";
    //echo $values;
    $stmt1->close();
    return $values;
    }

    ?>

    IF we wrote code like above we got following exeception:

    [Wed Dec 26 14:09:45 2007] [error] [client 127.0.0.1] PHP Notice: Undefined variable: db in F:\\Softwares\\php\\htdocs\\samples\\function_26.php on line 5
    [Wed Dec 26 14:09:45 2007] [error] [client 127.0.0.1] PHP Fatal error: Call to a member function connect() on a non-object in F:\\Softwares\\php\\htdocs\\samples\\function_26.php on line 5
    [Wed Dec 26 14:13:51 2007] [error] [client 127.0.0.1] PHP Notice: Undefined variable: db in F:\\Softwares\\php\\htdocs\\samples\\function_26.php on line 21
    [Wed Dec 26 14:13:51 2007] [error] [client 127.0.0.1] PHP Fatal error: Call to a member function prepare() on a non-object in F:\\Softwares\\php\\htdocs\\samples\\function_26.php on line 21



    Please help us regarding error problem

    thanks in advance
    vijaya
     
    nanna_vijayalaxmi, Dec 26, 2007 IP
  2. coches

    coches Peon

    Messages:
    41
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    It looks like you didnt include the connect class or u didnt instantiate the new class .

    Also if these are loose functions and not methods (which it looks like it)
    this for example is not going to work.
    because the $db object is not in the scope of the function
    function GetAssignedvalue($url,$rating)
    {
    // database stuff
    $db->some_method()
    }

    because you didnt pass the db object
    you can do this in 2 ways
    pass it as a argument or define it globally
    function GetAssignedvalue(&$db,$url,$rating)
    {
    or
    function GetAssignedvalue($url,$rating)
    {
    global $db;

    good luck
     
    coches, Dec 26, 2007 IP
  3. nanna_vijayalaxmi

    nanna_vijayalaxmi Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi,thank you very much.But I tried like that but i didn't get values
    please send a sample application with multiple functions means 3-4 functions.I have nearly 19 functions in our application.For each time open /close the connection it is taking to execute the application
     
    nanna_vijayalaxmi, Dec 28, 2007 IP