I'm trying to place my db code into a function. I can't get the results to be returned from the function so all the variables on the page are throwing undefined variable errors.
function fetchVideo($dblink) {
$viewkey=$_GET['viewkey'];
$sql = "SELECT * FROM videos WHERE Viewkey='".$viewkey."'";
$result = mysqli_query($dblink, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
return $key=$row["Viewkey"];
return $embed=$row["Embed"];
return $link=$row["Link"];
return $url=$row["URL"];
return $categories=explode(";", $row["Categories"]);
return $rating=$row["Rating"];
return $username=$row["User_name"];
return $title=$row["Title"];
return $tags=str_replace(";","-", explode(",", $row["Tags"]));
return $duration=intdiv($row["Duration"],60);
return $thumbnail=$row["Thumbnail"];
}
} else {echo 'No Results';}
}
The above code works when taken out of the function and remove all the "returns". I tried it as an array and return the array and no dice:
function fetchVideo($dblink) {
$viewkey=$_GET['viewkey'];
$sql = "SELECT * FROM videos WHERE Viewkey='".$viewkey."'";
$result = mysqli_query($dblink, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$video=array (
"key"=>$row["Viewkey"],
"embed"=>$row["Embed"],
"link"=>$row["Link"],
"url"=>$row["URL"],
"categories"=>explode(";", $row["Categories"]),
"rating"=>$row["Rating"],
"username"=>$row["User_name"],
"title"=>$row["Title"],
"tags"=>str_replace(";","-", explode(",", $row["Tags"])),
"duration"=>intdiv($row["Duration"],60),
"thumbnail"=>$row["Thumbnail"]
);
}
} else {echo 'No Results';}
return $video;
}
What am I doing wrong here?