php - Attempting to learn mysqli prepared statements; what am I doing wrong? -
here's error i'm getting...
failed prepare statement: (1064) have error in sql syntax; check manual corresponds mysql server version right syntax use near '?.pages slug='?'' @ line 1
and here's code...
require_once("../database/config.php"); $pageslug = "home"; $db = new mysqli(_db_host, _db_user, _db_password, _db_name); if ( $db->connect_errno ) { echo "failed connect mysql: (" . $db->connect_errno . ") " . $db->connect_error; exit(); } if ( !$selectquery = $db->prepare("select * ?.pages slug='?'") ) { echo "failed prepare statement: (" . $db->errno . ") " . $db->error; exit(); } if ( !$selectquery->bind_param("ss", _db_name, $pageslug) ) { echo "binding parameters failed: (" . $selectquery->errno . ") " . $selectquery->error; exit(); } if ( !$selectquery->execute() ) { echo "exexute failed: (" . $selectquery->errno . ") " . $selectquery->error; exit(); } echo "<pre>i got here!</pre>"; exit();
the ../database/config.php
contains global variables reference above ("_db_name", etc).
i guess i'm still wrapping head around prepared statements things , don't know i'm doing wrong.
thanks in advance!
prepared statements can not use parameters supply identifiers (schema names, table names, column names, etc), because submitted dbms verify syntax, before supplying values of parameters.
the markers legal in places in sql statements. example, allowed in values() list of insert statement (to specify column values row), or in comparison column in clause specify comparison value. however, not allowed identifiers (such table or column names), in select list names columns returned select statement, or specify both operands of binary operator such = equal sign. latter restriction necessary because impossible determine parameter type. it's not allowed compare marker null ? null too. in general, parameters legal in data manipulation language (dml) statements, , not in data definition language (ddl) statements
http://dev.mysql.com/doc/refman/5.0/en/prepare.html
parameter markers can used data values should appear, not sql keywords, identifiers, , forth.
still, may use dynamic sql. example:
$table = 'example'; // should safe, avoid user input. $sql = "select * `{$table}` `id` = ?"; $stmt = $db->prepare($sql); // ...
upd:
i've noticed, you're using single quotes '
around string parameter markers. should avoided because, dbms cares them itself. slug='?'
should slug = ?
.
read carefully: http://php.net/mysqli-prepare.
Comments
Post a Comment