Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <php
- // configuration
- $db_user = 'root';
- $db_pass = 'password';
- $db_name = 'namaDB';
- // connect to database
- $dsn = 'mysql:dbname=' . $db_name . ';charset=utf8;host=localhost';
- try {
- $dbh = new PDO( $dsn, $db_user, $db_pass );
- } catch ( PDOException $e ) {
- echo 'Connection failed: ' . $e->getMessage();
- }
- // Fetch [single result]
- $sth = $dbh->prepare( "SELECT * FROM user WHERE user_id=?" );
- $sth->execute( array( $_SESSION['user_id'] ) );
- $f = $sth->fetch( PDO::FETCH_OBJ );
- echo $f->name;
- // Row [multiple result]
- $sth = $dbh->prepare( "SELECT * FROM movies WHERE genre=? AND year=?" );
- $sth->execute( array( $_GET['genre'], $_GET['year'] ) );
- while ( $f[] = $sth->fetch( PDO::FETCH_ASSOC ) );
- if ( empty( $f[count( $f ) - 1] ) ) unset( $f[count( $f ) - 1] );
- foreach ( $f as $r ) {
- echo $r['title'];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement