5

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"
  }
}
undone
  • 7,857
  • 4
  • 44
  • 69
  • does your production server has data? – DevZer0 Jun 25 '13 at 06:54
  • @DevZer0 Yes, it has. As I said: if I use `var_dump` right before `return $ret;` in `getjobs` function, it shows me correct values. – undone Jun 25 '13 at 06:55
  • @FDL thanks but I know that. – undone Jun 25 '13 at 07:01
  • What happens if you change `return $ret` to something specific like `return 'working'`, do you get a return value then? – naththedeveloper Jun 25 '13 at 07:03
  • You are calling `getjobs` or `getJobs` method? – Omar Faruk Sharif Jun 25 '13 at 07:06
  • @OmarSharif both definition and usage are using same letters! – undone Jun 25 '13 at 07:09
  • Could you show us the array you generate when you use `var_dump` right before `return $ret;`? If the string returns fine I can only think there is some issue with the array being generated, but god knows why seeing as your code looks fine.. – naththedeveloper Jun 25 '13 at 07:13
  • What's the difference between what we're looking at and the real unsimplified code? – Dale Jun 25 '13 at 07:17
  • @FDL I added var_dump's output to question, – undone Jun 25 '13 at 07:24
  • @dale Actually nothing, i just removed other functions – undone Jun 25 '13 at 07:24
  • 1
    I can't fathom this out, the code looks sound to me – Dale Jun 25 '13 at 07:31
  • None of the functions you are using are involved with `Safe mode On`, but since you explicitly mention it. Does it work when safe mode is off? Also I would minify the code even further to find the problem. Instead of changing `return $red` to `return 'test'`, try `$red = 10` just before the return statement. Does it show 10? If so, try to remove the free result. Or dont use set `$red= $this->mysql_fetch_all($result);` but just use `return $this->mysql_fetch_all($result);`. Also dont set `$tempbJobs` but just `var_dump($db->getJobs(1368));`. Remove as much code as possible. – Hugo Delsing Jun 27 '13 at 10:26

5 Answers5

1

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.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • Thanks but as I said I used `var_dump` on `$ret` and it was fine! the only problem is no value is returned! – undone Jun 27 '13 at 13:45
  • Yes... but if you read what I said, the only way to get NO VALUE is if the execution of the query i.e. mysql_query() returns an error. If that happens $result will be FALSE.... the test in mysql_fetch_all will never execute the return statement.... so it will return NULL the default for a function that does not execute a return; ....And you will get NULL or in your tems NO VALUE. So therefore THE QUERY HAS AN ERROR. To be honest I cant see why you need the curly brackets around the parameter $wid i.e. {$wid}. – RiggsFolly Jun 27 '13 at 14:24
  • I agree -- try to return empty array -- move `return $all` one bracket `}` further. – Voitcus Jun 28 '13 at 06:44
1

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;
}
user1402647
  • 480
  • 4
  • 9
1

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";
Muc
  • 1,464
  • 2
  • 13
  • 31
Sonu Sindhu
  • 1,752
  • 15
  • 25
1

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

$ret=array(); //try 
TerryA
  • 58,805
  • 11
  • 114
  • 143
0

I know this is a little bit rusty question but yet still interesting. Are you able to found solution on that ?

If same code doesn't work on different server i would have suspected in that case memory limit. Try to increase php memory limit.

And it could be wise to check phpinfo(); on both servers to compare differences.

mirza
  • 5,685
  • 10
  • 43
  • 73