I have a class which works fine with php 5.3 (XAMPP 1.7.3, windows 7) but doesn't work in my server(php 5.2.17 - Safe mode On):
<?php
class MYSQL_DB {
var $connection;
function MYSQL_DB() {
$this->connection = mysql_connect(S, U, P) or die('Can\'t connect to MySQL server.');
mysql_select_db(DB, $this->connection) or die(mysql_error());
}
function getJobs($wid) {
$q = "SELECT * FROM " . TB_PREFIX . "joblist where owner = {$wid} order by ID ASC";
$result = mysql_query($q, $this->connection);
$ret = $this->mysql_fetch_all($result);
mysql_free_result($result);
return $ret;
}
function mysql_fetch_all($result) {
$all = array();
if ($result) {
while ($row = mysql_fetch_assoc($result)) {
$all[] = $row;
}
return $all;
}
}
}
$db=new MYSQL_DB();
?>
And in another file, I used getjobs
function:
<?php
$tempbJobs=$db->getJobs(1368);
var_dump($tempbJobs);
?>
when I use var_dump
right before return $ret;
in getjobs
function, it shows me correct values, but var_dump($tempbJobs);
will print NULL.
P.S: I simplified the code, it works on my localhost but not on production server.
P.S: If I change return $ret;
to return 'DUMPED';
, returned value would be string(6) "DUMPED"
var_dump($ret );
output:
array(2) {
[0]=>
array(5) {
["id"]=>
string(5) "10755"
["owner"]=>
string(5) "23626"
["field"]=>
string(1) "6"
["type"]=>
string(1) "2"
["expi"]=>
string(10) "1372144648"
}
[1]=>
array(5) {
["id"]=>
string(5) "10756"
["owner"]=>
string(5) "23626"
["field"]=>
string(1) "5"
["type"]=>
string(1) "2"
["expi"]=>
string(10) "1372144654"
}
}
Based on the fact that you only return $all from the function mysql_fetch_all($result) if $result is true I have to assume that the mysql_query() is returning false.
After the call to
$result = mysql_query($q, $this->connection);
Can you add this
$result = mysql_query($q, $this->connection);
if ( ! $result ) {
echo "ErrorNo: " . mysql_errno() . " Error Message: " . mysql_error();
}
This might help identify what the problem actually is as it has to be a database error of some sort.
Could you check that your constants S, U, P, TB_PREFIX
are defined()
and they have values.
Also modify your function mysql_fetch_all
to return response anyway:
function mysql_fetch_all($result) {
$all = array();
if ($result) {
while ($row = mysql_fetch_assoc($result)) {
$all[] = $row;
}
}
return $all;
}
I reviewed all your comments.
In one of your comments you said that return is not working but I think it is working properly because you already said in one of your comments that when you print out the result before return $ret
it gives you the correct result.
fact : mysql_fetch_all($result)
also returning result.
checkout the sql code : $q = "SELECT * FROM " . TB_PREFIX . "joblist where owner = {$wid} order by ID ASC";
I do not know {$wid} what it is.
Test it
$q = "SELECT * FROM " . TB_PREFIX . "joblist where owner = '$wid' order by ID ASC";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With