1

I need to convert this new database connectivity and retrive information from database to older fashion for my project. I have done the connection according to older fashion way PHP 4.0 but i need to use this PDO connectivity code to older database connectivity as i am not familiar with how to use and retrive information with using the older database connectivity. Thank you.

<?php 
  $pdo = new PDO('mysql:host=localhost;dbname=sitepoint', 'root', '*****');
  $opts = $_POST['filterOpts'];
  $qMarks = str_repeat('?,', count($opts) - 1) . '?';
  $statement = $pdo->prepare("SELECT mobile_phone.id, name, model, price FROM mobile_phone INNER JOIN brand ON brand_id = brand.id WHERE name IN ($qMarks)");
  $statement -> execute($opts);
  $results = $statement -> fetchAll(PDO::FETCH_ASSOC);
  $json = json_encode($results);
  echo($json);
?>

and i have tried this way with the older database connevtivity but its giving me nothing:

$opts = $_POST['filterOpts'];
  $qMarks = str_repeat('?,', count($opts) - 1) . '?';
  $statement = "SELECT mobile_phone.id, name, model, price FROM mobile_phone INNER JOIN brand ON brand_id = brand.id WHERE name IN ($qMarks)";
  $statement1 = mysql_query($statement);
  $results = mysql_fetch_assoc($statement1);
  $json = json_encode($results);
  echo($json);
user3659737
  • 31
  • 1
  • 8

1 Answers1

0

mysql_query (and related) doesn't support bindings, so the final query is as follows SELECT mobile_phone.id, name, model, price FROM mobile_phone INNER JOIN brand ON brand_id = brand.id WHERE name IN (?,?,?,?)

To be able of use dynamic parameters the same way with PDO, you should wrap each of them manually.

An approach could be as follows,

$opts = array();
foreach ($_POST['filterOpts'] as $filter) {
    $opts[] = '\'' . mysql_real_escape_string($filter) . '\'';
}

  $qMarks = implode(',', $opts);
  $statement = "SELECT mobile_phone.id, name, model, price FROM mobile_phone INNER JOIN brand ON brand_id = brand.id WHERE name IN ($qMarks)";
  $statement1 = mysql_query($statement);
  $results = mysql_fetch_assoc($statement1);
  $json = json_encode($results);
  echo($json);
NeoParla
  • 1
  • 1