From index.php
I'm calling the function <?php displayTitle(7) ?>
passing the id in order to get this record's title. The function doesn't return anything, nor do I get any errors. What am I missing here?
Any help would be greatly appreciated.
Folder structure:
index.php
|_includes/
|_downloads/
| |_displayInfo.php
|
|_db.php
index.php
<?php include('includes/downloads/displayInfo.php'); ?>
<div><?php displayTitle(7) ?></div>
db.php
<?php
$DBServer = 'xxxx';
$DBUser = 'xxxx';
$DBPass = 'xxxx';
$DBName = 'xxxx';
$DBtable = 'db_name';
$conn = mysqli_connect($DBServer, $DBUser, $DBPass, $DBName);
// check connection
if ($conn->connect_error) {
die('Error : ('. $conn->connect_errno .') '. $conn->connect_error);
}
?>
displayInfo.php
<?php
require_once('../includes/db.php');
function displayTitle($id) {
$sql = "SELECT title FROM " . $DBtable . " WHERE id = $id";
$rs=$conn->query($sql);
if($rs === false) {
trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);
} else {
$arr = $rs->fetch_all(MYSQLI_ASSOC);
}
foreach($arr as $row) {
$titles = $row['title'];
echo $titles;
}
}
?>
Database table:
CREATE TABLE IF NOT EXISTS `db_name` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`url` varchar(250) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`clicks` int(11) NOT NULL,
`dates` date NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;