Web Analytics Made Easy -
StatCounter Different ways to use Prepared Statements? - CodingForum

Announcement

Collapse
No announcement yet.

Different ways to use Prepared Statements?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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
    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
    CodingForum Supreme Overlord
    All Hail Spookster

    Comment


    • #3
      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

      Comment


      • #4
        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.




        Spookster
        CodingForum Supreme Overlord
        All Hail Spookster

        Comment


        • #5
          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.



          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.
          ZCE

          Comment

          Working...
          X
          😀
          🥰
          🤢
          😎
          😡
          👍
          👎