简体   繁体   中英

Calling pdo function with arguments

I'm trying to call this function with the argument of a table name, I have changed a number of things I'm not getting any output.

function getTbl($tablename)
{
    $mysqlConnection = getConnection();
    $sql = "SELECT * FROM ".$tablename;
    $Results = $mysqlConnection->query($sql);

    return $Results;
}

<?php 
  getTbl("college");
  echo Results();
?>

Well, your function return an object but you don't assign it.

try $results = getTbl("college");

Anyway you should ->fetch() or ->fetchAll() the object at some point, which returns an array and not an object

if you are using PDO::query() do a foreach

function getTbl($tablename){
   $mysqlConnection = getConnection();
   $sql = "SELECT * FROM ".$tablename;
   $Results = $mysqlConnection->query($sql);

  return $Results;
}


<?php 
  $itens =  getTbl("college");


  foreach($itens as $item){
     echo $item['key'];
  }
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM