Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php function doesn't return a value

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"
  }
}
like image 217
undone Avatar asked Oct 03 '22 17:10

undone


4 Answers

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.

like image 62
RiggsFolly Avatar answered Oct 12 '22 15:10

RiggsFolly


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;
}
like image 25
user1402647 Avatar answered Oct 12 '22 16:10

user1402647


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";
like image 44
Sonu Sindhu Avatar answered Oct 12 '22 15:10

Sonu Sindhu


try after defining array for $ret in the function getjobs()

$ret=array(); //try 
like image 41
Umesh Moradiya Avatar answered Oct 12 '22 16:10

Umesh Moradiya