Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- php query not excuting correctly
- select * from table where column1 = 'value1' and column2 = value2 //value2 has no ''
- query("select * from table where column1 = ? and column2 = ?",array($value1,$value2)
- select * from table where column1 = 'value1' and column2 = 'value2' //value2 has quotes, this is what i want to ignore / remove
- <?php
- $sth = $db->prepare('SELECT * FROM users WHERE username = ? AND pass = ?');
- $sth->execute(array('john', '1234'));
- $result = $sth->fetchAll();
- ?>
- <?php
- /**********************************************************
- * CREATES THE SAMPLE DATABASE/TABLE/DATA
- /*********************************************************/
- /*********************************************************
- CREATE DATABASE IF NOT EXISTS scratch
- CHARACTER SET = 'utf8' COLLATE = 'utf8_general_ci';
- CREATE TABLE `scratch`.`table1` (
- `column1` varchar(40) NOT NULL DEFAULT '',
- `column2` varchar(40) NOT NULL DEFAULT ''
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
- INSERT INTO `scratch`.`table1` (`column1`, `column2`)
- VALUES ('test1', 'test2');
- /*********************************************************/
- // connection parameters
- $hostname = '127.0.0.1';
- $database = 'scratch';
- $username = 'root';
- $password = 'rootpass';
- // connection parameters (mysql specific)
- $connectionString = "mysql:host=${hostname};dbname=${database}";
- $connectionOptions = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8');
- // query bound parameters
- $column1 = 'test1';
- $column2 = 'test2';
- // sql statement(s)
- $sql = 'SELECT
- `column1`, `column2`
- FROM
- table1
- WHERE
- `column1` = :column1
- and `column2` = :column2;
- ';
- // tl;dr
- try {
- $pdo = new PDO($connectionString, $username, $password, $connectionOptions);
- $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
- $statement = $pdo->prepare($sql);
- $statement->execute(compact('column1', 'column2'));
- $results = $statement->fetchAll();
- var_dump($results);
- $pdo = $statement = null;
- } catch(PDOException $e) {
- echo $e->getMessage();
- }
Add Comment
Please, Sign In to add comment