www.webdeveloper.com
Results 1 to 3 of 3

Thread: prepare() function problem

  1. #1
    Join Date
    Aug 2007
    Posts
    94

    prepare() function problem

    Hi

    I am dipping my toe in the water of prepared statements, and my function below is not working:

    PHP Code:
    function getPageText($url) {
        global 
    $dblink;
        
    $qry "SELECT content FROM tblsections WHERE url = ?";
        
        if (
    $stmt $dblink->prepare($qry)) {
            
    $stmt->bind_param("s",$url);
            
    $stmt->execute();
            
    $stmt->bind_result($content);
            
    $stmt->close();
            return 
    str_replace("/documents/editorfiles/","/repository/documents/editorfiles/",$content);
        }


    I am not sure how to debug prepared statments, normallly with a mysqli_query() I would just put a die statement on the end which would tell me the problem.

    Thank you for any advice.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,225
    Stick something after the if(){} block (can be an else{} for clarity if you want) to log some debug output if the prepare fails, e.g.:
    PHP Code:
    <?php

    function getPageText($url) {
        global 
    $dblink;
        
    $qry "SELECT content FROM tblsections WHERE url = ?";
        
        if (
    $stmt $dblink->prepare($qry)) {
            
    $stmt->bind_param("s",$url);
            
    $stmt->execute();
            
    $stmt->bind_result($content);
            
    $stmt->close();
            return 
    str_replace("/documents/editorfiles/","/repository/documents/editorfiles/",$content);
        }
        else {
            
    error_log("Prepare failed:".PHP_EOL.$dblink->error.PHP_EOL.$qry);
            return 
    false;
        }
    }
    PS: I would recommend passing $dblink in as a function parameter instead of using the global command, making the function more re-usable and easier to debug.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Aug 2007
    Posts
    94
    Hi NogDog

    Thanks for that. It looks like my query is actually being prepared, I have no erros however $content is coming back as NULL.

    PHP Code:
    function getPageText($url) {
        global 
    $dblink;
        
    $qry "SELECT content FROM tblsections WHERE url = 'about'";

        if (
    $stmt $dblink->prepare($qry)) {
            
    //$stmt->bind_param("s",$url);
            
    $stmt->execute();
            
    $stmt->bind_result($content);
            
    var_dump($content);
            
    $stmt->close();
            return 
    str_replace("/documents/editorfiles/","/repository/documents/editorfiles/",$content);
        } else {
            
    error_log("Prepare failed:".PHP_EOL.$dblink->error.PHP_EOL.$qry,1,"kris@wesayhowhigh.com");
            return 
    false;
        } 

    I have hard coded the query and commented out the bind_param. When I run this query against the server directly it returns the content as expected.

    Really stumped on this one.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles