please help me out regarding this function. I want to fetch data from the database and if there are results, I want to return a message and data array.
$this->query=("select * from user where pno='".$search."'");
$rd = $this->executeQuery();
$data = $rd->fetch_assoc();
if ($data) {
$message = "Record found.";
} else {
$message = "Record not found.";
}
return array($data, $message);
I am calling it this w开发者_运维百科ay:
list($data, $message)=$user->search($result);
echo $message
echo $data['name'];
I am getting the message but I am unable to fetch the array. Please help me out if there is a problem in the function or if I can improve it.
Never do it this way
you should put your message setting code outside of the function.
function getData(){
$this->query=("select * from user where pno='".$search."'");
$rd = $this->executeQuery();
return $rd->fetch_assoc();
}
if ($data = getData()){
$message = "Record found.";
echo $message;
echo $data['name'];
} else {
$message = "Record not found.";
echo $message;
}
I'm not sure what you're using, but most likely you'll have an array of rows in $data, even if there is only single row. Try $data[0]['name']
. Besides, I'd suggest that you move that message from your function to the place where it's called, and make the function return NULL or FALSE if nothing found.
$this->query=("select * from user where pno='".$search."'");
$rd = $this->executeQuery();
$data = $rd->fetch_assoc();
return empty($data)?FALSE:$data;
You’re missing a semicolon after echo $message
in your code example.
Try appending this to your code and tell us what it returns:
var_dump($data);
精彩评论