SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Addict darkwater23's Avatar
    Join Date
    Nov 2005
    Location
    Omaha, NE
    Posts
    335
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Can't make a prepared statement work

    Hello!

    Here's my code snippet. The $query object is always false. I threw some debugging statements in there.

    PHP Code:
    function db_connect()
    {
       
    $handle = new mysqli('localhost''username''password''database_name'); 
       if (!
    $handle)
       {
         return 
    false;
       }
       return 
    $handle;
    }

    $handle db_connect();

    $query $handle->prepare("insert into publications (PublicationName, FileName, FileSize, Category, CreatedBy, DatePublished, DateCreated) values (?, ?, ?, ?, ?, ?, NOW())");        
    if(
    $query)
    {
        
    $query->bind_param("ssisss"$PublicationName$NewFileName$FileSize$Category$_SESSION['auth_user'], $PublishDate);
        
    $query->execute();
    }
    else
    {
        if (
    mysqli_connect_errno())
        {
            
    printf("Connect failed: %s\n"mysqli_connect_error());
            exit();
        }
        else
        {
            echo 
    'So, $query is not an object, but there is not a connection error?';
            exit();
        }

    The echo statement "So, $query is not an object..." is all I ever get. I don't understand why $query isn't an object. Any ideas?

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Do you have a valid $handle?

    For reference, here is the (trimmed) example code from the mysqli_prepare documentation page.

    Code php:
    $mysqli = new mysqli("localhost", "my_user", "my_password", "world");
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }
    $city = "Amersfoort";
    if ($stmt = $mysqli->prepare("SELECT District FROM City WHERE Name=?")) {
        $stmt->bind_param("s", $city);
        $stmt->execute();
        $stmt->bind_result($district);
        $stmt->fetch();
        printf("%s is in district %s\n", $city, $district);
        $stmt->close();
    }
    $mysqli->close();
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Addict darkwater23's Avatar
    Join Date
    Nov 2005
    Location
    Omaha, NE
    Posts
    335
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure. I figured that if the handle was invalid, the prepare method would fail. However, the prepare method seems to fire just fine. That function that returns the handle is in an include file that is used all over the site without issue.

    Any suggestions?

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    What information do you have when you var_dump $query?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    You could also try having a look at $query->error()
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  6. #6
    SitePoint Addict darkwater23's Avatar
    Join Date
    Nov 2005
    Location
    Omaha, NE
    Posts
    335
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    var_dump returns: bool(false)

    trying to echo $query->error() gets me:
    Fatal error: Call to a member function error() on a non-object

    Which is similar to the errors I get when I try to call bind_param and execute.

  7. #7
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    So either $handle->prepare has problems, or $handle has problems. Does that make sense?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  8. #8
    SitePoint Addict darkwater23's Avatar
    Join Date
    Nov 2005
    Location
    Omaha, NE
    Posts
    335
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It makes sense, I just don't know what the issue is or what to check for. Even the example at PHP.net doesn't check for failure of the prepare statement. If mysqli returns a object with no error, prepare should just work.

    I tried switching to:

    $query = mysqli_prepare($handle,"sql statement...");

    But, I got the same error. WTH??

  9. #9
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    echo $handle->error;

    http://www.php.net/manual/en/mysqli.error.php

    As the name of the function suggests, mysqli_connect_error() provides an error message about connecting to the db failing.

  10. #10
    SitePoint Addict darkwater23's Avatar
    Join Date
    Nov 2005
    Location
    Omaha, NE
    Posts
    335
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Right there with you crmalibu! I was just doing that when you posted. I have a problem with a column name. Yay! Now, maybe I get this done and go get some dinner. :P


Bookmarks

Posting Permissions

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