Welcome Guest, Not a member yet? Register   Sign In
Undefined index: id error
#1

[eluser]phpworker[/eluser]
Hello there, seems this is my second post here Smile
OK, this is the problem: I am creating my first bigger project with CI and there's a problem I do not understand. I have the following code in my main controller:

Code:
$q = 'select login,password,name,surname,group_id from '.$this->tb['users'].' where login="'.$data['login'].'" and password="'.$data['password'].'" and name="dsdsdsd"';
                $query = $this->db->query($q);
                if($query == false)
                    echo "<br>login: query = false";
                else
                    echo "<br>login num_rows=".$query->num_rows();

                if ($query->num_rows() == 1)
                {
                    // succeed

                    $r = $query->row_array();

                    $d1 = array(
                               'user_id'         => $r['id']
                               ,'login'          => $r['login']
                               ,'user_name'       => $r['name'].' '.$r['surname']
                               ,'group_id'         => $r['group_id']
                    );
                    $this->session->set_userdata($d1);

And that gives me errors like "Undefined index: id" but $query->num_rows() gives me 1. I also added in query one dummy value "dsdsdsd" for column "name" and still there is one row returned :/
It seems CI doesn't generate any results. I have no idea what to do to solve it.

Any help, please?

I use OCI8 with Oracle database v9.2. Of course connection with it was previously tested.
#2

[eluser]deviant[/eluser]
You aren't actually selecting a column called id in that SQL query, so the array that the database library returns wont have that index.
#3

[eluser]phpworker[/eluser]
I've just added this code:
Code:
foreach ($query->list_fields() as $field)
{
   echo "<br>".$field;
}
and it returned:

Code:
ID
NAME
DESCR
I checked one more time the table structure and data in it, everything seems to be ok.
Changed the case of columns names used with $r[] array but no positive result from it.
#4

[eluser]phpworker[/eluser]
deviant: thanx, that's true I ommited it. Corrected now and the result is:
Code:
Undefined index: LOGIN
...
Undefined index: SURNAME
...
Undefined index: GROUP_ID
...
Cannot modify header information - headers already sent by (output started at /usr/local/apache2/htdocs/krzysztofk/aq/05a/system/application/controllers/main.php:90)
...
Cannot modify header information - headers already sent by (output started at /usr/local/apache2/htdocs/krzysztofk/aq/05a/system/application/controllers/main.php:90)
#5

[eluser]deviant[/eluser]
Array keys are case sensitive as far as I know, make 'em lower case.
#6

[eluser]phpworker[/eluser]
Just did it. This is updated version:
Code:
$q = 'select id,login,password,name,surname,group_id from '.$this->tb['users'].' where login="'.$data['login'].'" and password="'.$data['password'].'" and name="dsdsdsd"';
                $query = $this->db->query($q);
                if($query == false)
                    echo "<br>login: query = false";
                else
                    echo "<br>login num_rows=".$query->num_rows();

                foreach ($query->list_fields() as $field)
                {
                   echo "<br>".$field;
                }

                if ($query->num_rows() == 1)
                {
                    // succeed

                    $r = $query->row_array();
                    //$r = $query->first_row('array');

                    $d1 = array(
                               'user_id'         => $r['id']
                               ,'login'          => $r['login']
                               ,'user_name'       => $r['name'].' '.$r['surname']
                               ,'group_id'         => $r['group_id']
                    );
                    $this->session->set_userdata($d1);
                    $query->free_result();

                    redirect('main','location');

Result:

Code:
login num_rows=1
ID
NAME
DESCR

Undefined index: id
...
Undefined index: login
.
..
...
#7

[eluser]phpworker[/eluser]
Maybe this would help - here is table data and structure:
Code:
create table WPR_T_AQ_USERS
(
  ID       NUMBER not null,
  NAME     VARCHAR2(20) not null,
  SURNAME  VARCHAR2(20) not null,
  LOGIN    VARCHAR2(10) not null,
  PASSWORD VARCHAR2(50) not null,
  EMAIL    VARCHAR2(30),
  GROUP_ID NUMBER
)

    1    Krzysztof    Kępiński    kkepinski    krzysztof    <empty>        1

also, in application/config/tb.php i have defined:

Code:
$config['users'] = 'wpr_t_aq_users';
#8

[eluser]deviant[/eluser]
Well you could try adding this somewhere to see exactly what you are getting back:

Code:
$result = $query->result_array();

print_r($result);
#9

[eluser]phpworker[/eluser]
Well, just did it and the result is quite interesting:

Quote:login num_rows=1
ID
NAME
DESCR
RESULT:
Array ( [0] => Array ( [ID] => 0 [NAME] => Goscie [DESCR] => ) )

What you see, especially the word 'Goscie' is placed in ANOTHER table and this is name of the group of users.
I removed $this->tb['users'] from the query and replaced it with 'wpr_t_aq_users' - no difference in results!
#10

[eluser]phpworker[/eluser]
Maybe this would help - this is the content of the config/tb.php file:
Code:
&lt;?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

    /*    -------------------------------------------------
            Custom settings
            -------------------------------------------------    */
            
// tables used in database

$config['sessions'] = 'wpr_t_aq_sessions';            
$config['users'] = 'wpr_t_aq_users';            
$config['users_groups'] = 'wpr_t_aq_users_groups';            


?&gt;




Theme © iAndrew 2016 - Forum software by © MyBB