php - MySQL PDO WHERE = $variable -
i have php function want data database calling get_database_data()
in function have 2 variables should used in where $variable1 = $variable2
.
i know use "where ".$variable1." = ".$variable2
. it's works $variable2
, not $variable1
.
something else use, or how make work?
edit:
there no errors, row don't rows returned
edit2:
i've got work, using: "where $variable1 = ".$variable2
you should use prepared statements.
here snippet can used form function can return data need. should able modify quite needs.
function get_database_data($variable1,$variable2) { $dbh = new pdo("mysql:host=url;dbname=somedb", "user", "pass"); $sqlupdate = "select * sometable {$variable1} = :variable2"; $sthupdate = $dbh->prepare($sqlupdate); $sthupdate->bindparam(':variable2', $variable2, pdo::param_str, 12); $sthupdate->execute(); $result = $sthupdate->fetchall(pdo::fetch_assoc); return $result; } print_r(get_database_data('fieldname','value'));
i found question quite interesting , suggest give read also:
can use pdo prepared statement bind identifier (a table or field name) or syntax keyword?
Comments
Post a Comment