Reputation: 3175
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.
Upvotes: 2
Views: 2260
Reputation: 744
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
Upvotes: 0
Reputation: 518
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
Upvotes: 2