-1

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?

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
tst_ehbw
  • 45
  • 6
  • 2
    after the very first return, nothing else matters. you need to push all the result sets (rows) into an array/object which has to be returned *after* the loop. `$video[]` would do the magic within your second attempt. – mitkosoft Feb 26 '20 at 12:18
  • 2
    [How can I prevent SQL injection in PHP?](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?rq=1) – brombeer Feb 26 '20 at 12:19
  • also if there are no results returned from your query, better *return* something (like `false`) rather than `echo`. – mitkosoft Feb 26 '20 at 12:23
  • 2
    Your code is vulnerable to SQL injection. You should use prepared statements. – Dharman Feb 26 '20 at 12:23
  • @Dharman, I just wondered how much time it will take to see your comment about SQL injection. :) – mitkosoft Feb 26 '20 at 12:24
  • I'm just trying to get the mechanics to work right now, I'll worry about the extras after – tst_ehbw Feb 26 '20 at 12:25

1 Answers1

-1

To create an array of arrays:

$video = array() # placed outside of loop and before if statement
if (mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_assoc($result)) {
        // add next row to the $video array:
        $video[] = array(
             "key"=>$row["Viewkey"],
              etc.
        );
    }
} else {echo 'No Results';}
return $video;
Booboo
  • 38,656
  • 3
  • 37
  • 60