I would like to make a general function to select last inserted id's of different tables.
I already tried multiple things, this might be the best effort (in db.inc.php);
<?php
define( 'DB_HOST', 'localhost' );
define( 'DB_NAME', 'modlar' );
define( 'DB_USER', 'root' );
define( 'DB_PASS', 'password' );
function verbinden(){
$verbinding = 'mysql:host=' . DB_HOST . ';DB_NAME=' . DB_NAME;
$db = null;
try{
return new PDO( $verbinding, DB_USER, DB_PASS );
}catch( PDOException $e ){
return NULL;
}
}
function loopdoor( $naam, $ding){
$regels = '';
if( is_array($ding) || is_object($ding)):
$wat = (is_array($ding)? 'array' : 'object');
$regels .= '<strong>'.$naam.'['.$wat.']</strong><ul>';
foreach( $ding as $k => $v):
$regels .= loopdoor( $k, $v);
endforeach;
$regels .= '</ul>';
else:
$regels .= '<li><strong>'.$naam.'</strong> => '.$ding.'</li>';
endif;
return $regels;
}
function last_id( &$db , $table, $column){
if( is_null( $db ) ) return array();
$sql = "
SELECT
max(modlar.table.column)
FROM
modlar.table";
$vraag = $db->prepare( $sql );
$vraag->bindValue( ':table', $table, PDO::PARAM_STR );
$vraag->bindValue( ':column', $column, PDO::PARAM_STR );
$vraag->execute();
return $vraag->fetchAll( PDO::FETCH_OBJ );
}
Where modlar is my database. And my variables (in db.php);
<?php
require_once 'db.inc.php';
// Verbinden met database
$db = verbinden();
if( is_null($db))
die('<h1>Error</h1>');
echo 'done';
$table = 'time';
$column = 'time_id';
$last = last_id($db, $table, $column);
echo '<ul>'.loopdoor('all data', $last).'</ul>';
?>
But I dont get any data on my screen (and no error). However, when I add the variables directly to the code, I'll get the last id of that table. So like;
$sql = "
SELECT
max(modlar.time.time_id)
FROM
modlar.time";
What is going wrong?