I'm quite experiences in programming in PHP, and for a little side project I'm making I want to get some information from the database
I made my own sql handler, with a singleton so the connection doesn't get overflowed.
This is my query function:
<?php
public static function query($q){
$a = '';
$b = '';
self::$queries[] = $q;
if(strpos($q, 'INSERT') !== false){
if(!mysql_query($q)){
self::$err[] = mysql_error()."<br />Query: ".$q;
return false;
}
return true;
} elseif(strpos($q, 'SELECT') !== false){
echo $q;
if(!$res = mysql_query($q)){
self::$err[] = mysql_error()."<br />Query: ".$q;
return false;
} else {
if(mysql_num_rows($res) > 1){
while($r = mysql_fetch_assoc($res)){
foreach($r as $key => $val){
self::$results[$r['id']][$key] = $val;
}
}
return self::$results;
} else {
$res = mysql_fetch_assoc($res);
foreach($res as $key => $val){
self::$results[$res['id']][$key] = $val;
}
return self::$results;
}
}
} elseif(strpos($q, 'UPDATE') !== false){
if(!mysql_query($q)){
self::$err[] = mysql_error()."<br />Query: ".$q;
return false;
}
return true;
} elseif(strpos($q, 'DELETE') !== false){
if(!mysql_query($q)){
self::$err[] = mysql_error()."<br />Query: ".$q;
return false;
}
return true;
}
}
?>
Now I want to get some data from a table that I created(Table contains 2 foreign keys) Table:
CREATE TABLE IF NOT EXISTS `loc_npc` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`loc_id` int(11) NOT NULL,
`npc_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `loc_id` (`loc_id`),
KEY `npc_id` (`npc_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
ALTER TABLE `loc_npc`
ADD CONSTRAINT `loc_npc_ibfk_2` FOREIGN KEY (`npc_id`) REFERENCES `npcs` (`id`),
ADD CONSTRAINT `loc_npc_ibfk_1` FOREIGN KEY (`loc_id`) REFERENCES `locations` (`id`);
The problem when I run this query SELECT id, loc_id, npc_id FROM loc_npc WHERE loc_id = 9
Is that it somehow returns data from another table, aswel as the correct data.
The query is ran in another handler:
public function getLocationNpc($id){
//return $this->sql->query("SELECT e.`name`, e.`desc`, e.`id` FROM npcs e LEFT JOIN(loc_npc i CROSS JOIN locations l) ON (e.id = i.npc_id AND i.loc_id = l.id) WHERE i.loc_id = $id");
$npcH = new npcHandler();
$npcId = $this->sql->query("SELECT id, loc_id, npc_id FROM loc_npc WHERE loc_id = $id");
echo '<pre>'.print_r($npcId, true).'</pre>';
foreach($npcId as $nId){
$npcArr[] = $npcH->getNpc($nId['npc_id']);
}
return $npcArr;
}
I tested the queries in phpmyadmin and it just returns the correct data, unlike the query executed through my handler.
I'm pretty sure it's something small and stupid, but I just can't seem to find it.