PHP OOP function won't grab data unless another function is called
I'm having this weird problem right now, I'm just working on a small admin backend thing that will allow users to upload a file and download it again, simple stuff.
I'm using PHP OOP with Classes and functions and things. Still pretty new at it. Currently on one page I have a "getRecentLinks" function that will call information from a couple tables and put it all out on the page in a table, it works just fine. One of the options on this page is to download the file that was uploaded to that individual row. So I just want it to be a link you click on to say, file-download.php?id=3.
Now on file-download.php I'm currently just testing to get the information on the page before I add the headers and things in there. So I just have a simple
$l = new Links();
$file = $l->getFileInfo($_GET['id']);
print_r($file);
this SHOULD just return information from the d开发者_运维问答atabase, id, name of file, size, data, and mimetype.
Now this doesn't work. I have no idea why but it doesn't.
Now on my page where I have the getRecentLinks() function it works just fine. I even brought in the getRecentLinks() into my file-download.php page so it's setup like this.
$l = new Links();
$l->getRecentLinks();
$file = $l->getFileInfo($_GET['id']);
print_r($file);
This works just fine and dandy, The second I remove getRecentLinks() it stops calling information from getFileInfo() and I cannot figure it out. I mean it's not a huge deal i could just keep #l->getRecentLinks() there but I can see this getting annoying if I have to add it to every page I want to do something, I'm just at the start of this project.
Here's the code in the Links class
public function getFileInfo($id)
{
$result = $this->runQuery("SELECT * FROM file WHERE id ='".mysql_real_escape_string($id)."'");
$resultSet = $this->fetch($result);
return $resultSet;
}
and
public function getRecentLinks()
{
$result = $this->runQuery("SELECT * FROM links ORDER BY date DESC");
while($j = $this->fetch($result))
{
$resultSet[] = $j;
}
return $resultSet;
}
And heres my connections and fetch functions a friend helped me develop
public function runQuery($sql) {
$this->connection = mysql_connect($this->dbhost, $this->dbuser, $this->dbpass);
if(!$this->connection) {
die("MySQL is down.");
} else {
mysql_select_db($this->dbname);
}
$result = mysql_query($sql,$this->connection) or die(mysql_error());
return $result;
}
public function fetch($result)
{
$resultSet = mysql_fetch_assoc($result);
return $resultSet;
}
mysql_real_escape_string requires a connection. Since getRecentLinks
opens a connection it works. But you call mysql_real_escape_string before runQuery
(in execution order) in getFileInfo
. In this case, I assume the id is an integer, so you're better off and a lot cheaper to call intval($_GET['id'])
.
精彩评论