Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Created by PhpStorm.
- * User: Ostico
- * Date: 12/02/16
- * Time: 11.18
- */
- $user = "admin";
- $pass = "admin";
- $host = "mysql";
- $db = "test";
- $con = new PDO( "mysql:host=" . $host , $user, $pass );
- $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- $con->query( "CREATE DATABASE IF NOT EXISTS test" );
- $con->query( "USE test" );
- $con->query( "CREATE TABLE IF NOT EXISTS MyGuests (
- id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
- name VARCHAR(30) NOT NULL
- )" );
- try {
- $con->query( "INSERT INTO MyGuests VALUES ( 8, 'name1' ), ( 9, 'name2' ), ( 10, 'name3' ) " );
- } catch ( Exception $e ){}
- echo "// Wrong Code Below --------------";
- $sth = $con->prepare('SELECT id
- FROM MyGuests
- WHERE id IN ( :id )');
- foreach( array( 8, 9 ) as $k => $v ){
- $sth->bindValue( ':id', $v );
- }
- $sth->execute( );
- var_dump( $sth->fetchAll() );
- var_dump( $sth->rowCount() );
- echo "// Wrong code Below -----------------";
- $sth = $con->prepare('SELECT id
- FROM MyGuests
- WHERE id IN ( :id )');
- $sth->execute( array( 'id' => implode( ", ", array( 8, 9 ) ) ) );
- var_dump( $sth->fetchAll() );
- var_dump( $sth->rowCount() );
- echo "// Right code Below -----------------";
- $ids = array( 8, 9 );
- $place_holders = implode( ',', array_fill( 0, count($ids), '?' ) );
- $sth = $con->prepare("
- SELECT id
- FROM MyGuests
- WHERE id IN ( $place_holders )
- ");
- $sth->execute( array( 8, 9 ) );
- var_dump( $sth->fetchAll() );
- var_dump( $sth->rowCount() );
- echo "// Right code Below -----------------";
- $ids = array( 8, 9 );
- $place_holders = implode( ',', array_fill( 0, count($ids), '?' ) );
- $sth = $con->prepare("
- SELECT id
- FROM MyGuests
- WHERE id IN ( $place_holders )
- ");
- foreach( $ids as $k => $v ){
- $sth->bindValue( $k + 1, $v, PDO::PARAM_STR );
- }
- $sth->execute( );
- var_dump( $sth->fetchAll() );
- var_dump( $sth->rowCount() );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement