$fields = array("name","dob","email");
$values = array("'soma lkzx'", "12-12-1900","'user@example.com'");
if($is_bob) {
array_push($fields, "height");
array_push($values, 145);
}
$sql = sprintf("INSERT INTO tablename (%s) VALUES (%s)", implode($fields, ","), implode($values,","))
I am not a PHP coder by any means and this is just back-of-the-napkin code. You could also take advantage of key-value pairs in associative arrays instead of having separate $fields and $values.
$statement = $db->prepare( "select * from foo where bar = :baz" );
$statement->bindParam( ':baz', $baz, PDO::PARAM_STR );
$statement->execute();
while( $row = $statement->fetch( PDO::FETCH_ASSOC ) ) {
echo $row['columnName'];
}
You are not logged in, either login or create an account to post comments
posted by odinsdream at 11:03 AM on March 30