Created
January 23, 2013 16:28
-
-
Save szbl/4609164 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// user, pass, db name, host | |
$db = new wpdb( 'root', 'password', 'dbname', 'localhost' ); | |
// insert some data | |
if ( $db->insert( 'company_contacts', array( | |
'first_name' => 'Andy', | |
'last_name' => 'Stratton', | |
'email_address' => '[email protected]' | |
)); | |
// retrieve a record | |
$user = $db->get_row( "SELECT * FROM company_contacts WHERE first_name = 'Andy'" ); | |
echo '<p>' . $user->first_name . ' ' . $user->last_name . ' is in our database.</p>'; | |
// retrieve a record set, useful for searches | |
$users = $db->get_results( "SELECT * FROM company_contacts WHERE first_name = 'Andy'" ); | |
if ( count( $users ) > 0 ) ) | |
{ | |
foreach ( $users as $user ) | |
echo '<p>' . $user->first_name . ' ' . $user->last_name . ' is in our database.</p>'; | |
} | |
// retrieve a value | |
$email = $db->get_var( "SELECT email_address FROM company_contacts WHERE last_name = 'Stratton'" ); | |
echo '<p>Email Address: ' . $email . '</p>'; | |
// preparing statements based on user input | |
// assuming form submits to http://www.mysite.com/?fname=Andy&lname=Stratton | |
$input = array( $_GET['fname'], $_GET['lname'] ); | |
// prepare our statement, %s denotes a string, | |
// %d denotes an integer, %f denotes a floating point number | |
$sql = $db->prepare( "SELECT * FROM company_contacts WHERE first_name = %s AND last_name = %s", $input ); | |
// $sql now has safe versions of our input strings inserted | |
// and is safe to query | |
$results = $db->get_results( $sql ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment