Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    939
    Thanks
    21
    Thanked 0 Times in 0 Posts

    Different ways to use Prepared Statements?

    Are there different ways to use Prepared Statements in PHP?

    I have been using code like this...
    PHP Code:
            // Prepare statement.
            
    $stmt mysqli_prepare($dbc$q);

            
    // Bind variable.
            
    mysqli_stmt_bind_param($stmt's'$prettyTitle);

            
    // Execute query.
            
    mysqli_stmt_execute($stmt);

            
    // Store result-set.
            
    mysqli_stmt_store_result($stmt); 
    ...but it seems to me that some seems passionate about PDO?

    Or maybe there are even other ways?


    **NOTE: I am not a object-oriented type person because I am a wimp, so I'd sorta like to stay with a Procedural approach unless someone can give me compelling reasons to switch?!



    Debbie

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    PDO (PHP Data Objects) allow you to use prepared statements. Prepared statement allow the database to retain some of the information about your query statement so the next time it executes your query it doesn't have as much work to do. You would want to use that for queries you execute alot where the only thing that changes are your parameters.

    PDO is a database abstraction layer in PHP. It allows you to switch between different database types with little or no code changes because none of the calls to it's API are database specific except for the initial database connection call that specifies which type of database you are using.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    Regular Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    939
    Thanks
    21
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Spookster View Post
    PDO (PHP Data Objects) allow you to use prepared statements. Prepared statement allow the database to retain some of the information about your query statement so the next time it executes your query it doesn't have as much work to do. You would want to use that for queries you execute alot where the only thing that changes are your parameters.

    PDO is a database abstraction layer in PHP. It allows you to switch between different database types with little or no code changes because none of the calls to it's API are database specific except for the initial database connection call that specifies which type of database you are using.
    So PDO and Prepared Statements are mutually exclusive?


    Debbie

  • #4
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Quote Originally Posted by doubledee View Post
    So PDO and Prepared Statements are mutually exclusive?


    Debbie
    PDO is not there just for the purpose of using prepared statements. It's a database abstraction layer. It does alot more than just that. You can use 1 of 3 different database APIs in PHP for MySQL. (Original MySQL API, MySQLi or PDO). MySQL and MySQLi are APIs for MySQL where PDO can be used with any of the supported databases. You can use prepared statements with MySQLi as well.

    Prepared statements are a feature of databases in general. You can use prepared statements with many other languages also.


    http://www.php.net/manual/en/intro.pdo.php
    http://php.net/manual/en/pdo.prepared-statements.php
    http://php.net/manual/en/mysqli.prepare.php
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #5
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Without switching over to the OOP-enhanced syntax, what you have there is pretty much on target.

    The link below will show a number of different ways to bind parameters to a prepared statement using the OOP syntax.

    http://www.php.net/manual/en/pdostatement.bindparam.php

    As you can see in the example code in that link, you can bind parameters using names, such as :colour, :calories, etc... or numerically using question marks to specify parameter location and then target them by their numerically sequential position.

    Slightly off topic, PDO is not a database abstraction. There is no query builder, and it cannot compensate for proprietary syntax or missing features between different types of RDBMS. There is more than a slight difference between data-access abstraction and database abstraction.


  •  

    Posting Permissions

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