4

My issue is that I am trying to get the columns from a mysql query using $query->list_fields().

I have a local Windows x64 machine and need have been using PHP 5.4 and everything works fine I have had not issues. I then moved over to the server which is LINUX centOS and none of the columns are pulled back at the database call. I have replicated the scenario issue on my local machine as best as I can and it pulls back the fields with no issue on my local machine. The weird thing is that I have a piece of code that pulls back the fields and puts it into an array for me and it works for a different call but not the one I want. I have validated the sql call and it returns a single result which is what I want and I have validated this.

Here are the specs:

      |  Client                  |   Server
------|--------------------------|-----------------------------
OS    |  Windows x64 Professional|  CentOS release 6.4 (Final)
Apache|  Apache/2.4.4            |   2.2.24
MYSQL |  5.5.32                  |   5.5.33
PHP   |  5.4.16                  |   5.4

The server I am using is a a hostgator shared plan

Here is the php code:

private function get_single_result_from_single_row_query(&$table, &$id, &$id_var = 'id')
{
    $query = $this->db->limit(1)->get_where($table, array($id_var => $id));
    if ($query->num_rows === 1)
    {
        $rows = ($query->result_object());
        $cols = $this->get_collumns_as_array($query);
        return $this->table_object_from_cols($cols, $rows[0]);
    }
    else
    {
        return NULL;
    }
}


private function get_collumns_as_array(&$query)
{
    $collumns = array();
    foreach ($query->list_fields() as $field)
    {
        log_message('error', 'col field: '. $field);
        array_push($collumns, $field);
    }
    if(empty($collumns))
    {
        log_message('error', 'collumns is empty'. $field);
    }
    return $collumns;
}

private function table_object_from_cols(&$collumns, &$row)
{
    if ($collumns == NULL || empty($collumns))
    {
        return NULL;
    }
    $table_object = array();
    foreach ($collumns as $col)
    {
        log_message('error', 'row->col : ' . $row->$col);
        $table_object[$col] = $row->$col;
    }
    return $table_object;
}

As you can see I have pumped out an error when there are no columns to try as to best ascertain why! And also as should be seen is that the query can only go forward is the number of rows is equal to 1

Due to my plan I can not remotely debug the server apparently

Basically I am completely stumped all helps is soooooo appreciated

EDIT: I have tried removing the '&' from my functions and it has made no difference EDIT: I am now using PHP 5.4 on the server

bubblebath
  • 939
  • 4
  • 18
  • 45
  • please try to declare your functions parameters without `&` (without var reference) before each variable, there might be a different configuration on hosting. – Kyslik Oct 16 '13 at 14:25
  • Done this and it has made no difference – bubblebath Oct 16 '13 at 14:36
  • please turn on the profiler `$this->output->enable_profiler(TRUE);`, comment out all redirects after queries etc. and run the script, does queries run are they generated ok? – Kyslik Oct 16 '13 at 14:39
  • Queries are generated properly – bubblebath Oct 17 '13 at 10:58
  • Why don't you show us what the `table_object_from_cols` method looks like. Also, maybe you're getting an error that is not being displayed because error reporting may be turned off. Is error reporting turned on? http://stackoverflow.com/questions/6575482/how-do-i-enable-error-reporting-in-php. Also, does your database on your server contain any data? – Catfish Nov 25 '13 at 16:39
  • Yes the database has data in it and I pump out the query and it does return data. I am doing error reporting and nothing is returned. I am just about to edit the question to include the method you want – bubblebath Nov 25 '13 at 18:16
  • 1
    take a look [here](http://stackoverflow.com/questions/13676279/codeigniter-list-fields) – mamdouh alramadan Dec 01 '13 at 23:05
  • I had the similar issue. Working fine with Windows and not in Linux env. After an hour of checking, i found that it is issue with the db query caching. If you have enabled the db query caching, this particular function (list_fields) not returning anything. But i still don't know the reason why it is not working if query caching is enabled (Even in the Windows env). – Raja Sep 16 '15 at 10:29

6 Answers6

1

I've seen some (intermittent/strange) problems with CentOS 6.4 with MySQL versions higher than the one installed by default (5.1.69) due to the fact that the PHP MySQL Client API library has not been updated (check with phpinfo that is version 5.1.69). You should update it if it's the case.

ferran
  • 46
  • 2
0

You could double-check the case of the filenames. Windows ignores it and therefore can find files that you've named differently to the recommended system. Moving it to Linux would cause the filenames to be case sensitive and the system might not find the relevant libraries/models/etc.

Stephen O'Flynn
  • 2,309
  • 23
  • 34
0

This shouldn't be very hard to debug at least to the point of finding the exact point of failure.

First, your paragraph description is complete, but very hard to follow precisely which of your logging points are being captured. At little editing would be helpful.

1) Call ->num_rows(), not the variable directly, is a better idea

2) Confirm for us that get_collumns_as_array() is being called, as that's where your code issue is.

3) Not a good idea to use $query->functions() in the foreach loop: bad performance, and you can't easily test what's happening. Dump it in a variable and var_dump() it so we can confirm that list_fields() isn't returning anything, and it isn't just hiding some other issue (ie, $list_fields = $query->list_fields() )

4) I'm confused why you are using result_object() - this is normally used for passing in a class object; I think you only want result_array(), but perhaps you are doing something I don't see. It also seems like you are setting $rows = true/false, but again maybe there's more here. Seems like you are using too much "clever" code

5) If you confirm that things run correctly up until $list_fields = $query->list_fields(), this means your issue is in the list_fields() function. This function just uses mysql_fetch_field() to pull the fields from the query result. So, set up a simple php test on your server to confirm that function is working correctly

At this point, you should at least have narrowed things down much more precisely, and you can come back with a more specific question people can try to help you unravel.

jmadsen
  • 3,635
  • 2
  • 33
  • 49
0

Here seems to be the same issue

CodeIgniter list-fields

Im not sure why, What will work is changing to this

private function get_collumns_as_array(&$query){
    $row = $query->first_row();
    return array_keys($row);
}

putting back in any logging functions etc

Does the query work ? Is the application using the correct mysql address and the correct users. Can you try logging in as the user the application uses and run the query ?

Community
  • 1
  • 1
exussum
  • 18,275
  • 8
  • 32
  • 65
0

Check if both Mysql installations are set up the same. For example, check if both server have the same sql_mode:

SELECT @@SESSION.sql_mode;

If for example 1 is strict and the other is not, one could give a result + warning, while the other only gives an error.

nl-x
  • 11,762
  • 7
  • 33
  • 61
-2

I think you should try $this->db->list_fields()

$this->db->list_fields('table_name');
Ashok Maharjan
  • 163
  • 1
  • 7