2

I am trying to get a set of id from one table and get the details of the id from another table. Both tables are from 2 different database on two separate servers. I get an error when the data in the other table is empty.

In this example below, I stored all the user_id in an array called $user_data. Then, I would like to append to the existing array and add more info like first_name, last_name and email to the same user_id.

table: user

$user_data = array();

$sql_get_userid = "SELECT user_id FROM user
                   WHERE apply_date = ?";

$statement_get_id = $DB_3306->link->prepare($sql_get_userid);
$statement_get_id->bind_param("s", $date);
$statement_get_id->execute();

if($rs_get_id =  $statement_get_id->get_result())
{
    while($row = $rs_get_id->fetch_assoc())
    {
        $user_data[] = $row;
    }
}

There are times when the user_id might still exist but the data associated with the user_id in the other table is obsolete and deleted. Therefore, I get an error when I try to retrieve the non-existing data.

table: user_personal

$sql_get_user_data = "SELECT first_name, last_name, email FROM user_personal
WHERE user_id = ?"

foreach($user_id as $user => $child)
{ 
    $user_id = $child['user_id'];

    $statement_get_data = $DB_3308->link->prepare($sql_get_user_data);
    $statement_get_data->bind_param("s", $user_id);
    $statement_get_data->execute();

    if($rs_get_data = $statement_get_data->get_result())
    {
        while($row = $rs_get_data ->fetch_assoc())
        {
            if(isset($row['first_name']) && !empty($row['first_name']))
            {
               $first_name = $row['first_name'];
            }
            else
            {
                $first_name = 'NA';
            }
            $user_data[$user]['first_name'] = $first_name;
        }
    }
}

As you can see from the second part of the code, I try to check if the data is set and not empty and to add a message if it's empty. I still get an error "Notice: Undefined index". What did I do wrong here?

EDIT: Actually the $user_data doesn't even append the data if the other table doesn't have it.

//A rough idea of the data stored in $user_data
$user_data = Array( [0] => Array ( [user_id] => 111), [1] => Array ( [user_id] => 222) [2] => Array ( [user_id] => 333))

//If user_id = 333 has no data associated with it in user_personal table the array would look like this (showing only first_name value)
 $user_data = Array( [0] => Array ( [user_id] => 111 [first_name] => Kenny ), [1] => Array ( [user_id] => 222 [first_name] => Kenny) [2] => Array ( [user_id] => 333))

Actually the array from the codes here are called to an display file where the data are displayed. So, I moved the if(isset(var) && !empty(var)) to that file and so far it works fine.

3
  • can you tell the line at which notice:" undefined index" is coming? Commented Oct 14, 2014 at 8:13
  • Hey! The statement should be prepared only once! Move your $statement_get_data = $DB_3308->link->prepare($sql_get_user_data); outside the foreach loop. Commented Oct 14, 2014 at 8:13
  • @stackMonk - it's on another php file where I output the result. If I have 7 user_id and only 6 have data, the last one with no data will give me an error. The other 6 will echo with no problem. Commented Oct 14, 2014 at 8:18

2 Answers 2

2

Change your code from

        if(isset($row['first_name']) && !empty($row['first_name']))
        {
           $first_name = $row['first_name'];
        }
        else
        {
            $first_name = 'NA';
        }

     to 

        if($row['first_name']!='')
        {
           $first_name = $row['first_name'];
        }
        else
        {
            $first_name = 'NA';
        }

isset will never work because $row['first_name'] always set

Hope this will solve your problem

Sign up to request clarification or add additional context in comments.

2 Comments

Actually Sohail, I tried isset($row['first_name']) && !empty($row['first_name']) && ($row['first_name']) != "" ... but it gives me same error.
to debug your variable what you have in it use var_dump($row['first_name']);
0

please change the code in second code block which is this

$statement_get_data->bind_param("is", $user_id);

because amount of parameter needed is only one and supplied two
two types
i for integer and
s for string

so change that line to

$statement_get_data->bind_param("i", $user_id);

please see the php menual about bind_param

http://php.net/manual/en/mysqli-stmt.bind-param.php

1 Comment

Actually that was a typo on my end. It still doesn't work after changing the param.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.