dynamic sql - binding to a select statment

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
CanMikeF1
Forum Newbie
Posts: 8
Joined: Wed Feb 18, 2009 7:32 am

dynamic sql - binding to a select statment

Post by CanMikeF1 »

I've got this nasty feeling that this is undoable.

Simple put: Can you bind a string to an 'order by' in a select statement.

Code: Select all

 
<?php
require_once ('./includes/connect_plsql.php');
 
$sql = 'select col1, col2, col3 from test_table [b]order by[/b] ?';
$stmt = $dbc->prepare($sql);
 
if ($dbc->errno <> 0) {
  die($dbc->errno.' #1: '.$dbc->error.'<br />');
}
 
$stmt->bind_param('s',  $orderby) or die($stmt->error);
$stmt->bind_result ($c1, $c2, $c3) or die ($stmt->error);
 
$orderby = "col1";
 $stmt->execute();
 
if ($dbc->errno <> 0) {
  die($dbc->errno.' #2: '.$dbc->error.'<br />');
}
 
while ($stmt->fetch())
  echo '1: '. $c1 .' 2: '. $c2 .' 3: '. $c3 .'<br />';
 
?>
 
By the way col1 = varchar,
col2 = text,
col3 = int

On execution returns all values but 'order by' is ignored.

Mike in Toronto
Post Reply