0

i have this function for check news files from MySQL database:

function check_Files($id){

    $files = sqlaccess::fetch("SELECT url FROM ".FILES." WHERE news_id=".$id." AND type = 'video' "); 

    if (count($files) > 0) {
        foreach($files as $file){
            $url = $file['url'];
        return TRUE;
        }

    } else
    {
        return FALSE;
    }

}

HTML :

if(check_Files($id) == TRUE){

echo $url;

}

But I see this error:

Notice: Undefined variable: url in ......

How do can I back/send $url from function to html ?!

5
  • What kind of database library is behind your sqlaccess class? Maybe they return values as properties: $file->url Commented Jun 11, 2014 at 10:06
  • You are using loop to get the urls and it might be possible to have multiple urls ? Commented Jun 11, 2014 at 10:07
  • Your code could be vulnerable to SQL injection. Use PDO if possible! Commented Jun 11, 2014 at 10:19
  • @tangrs: this is PDO with customize Code ;) Commented Jun 11, 2014 at 10:24
  • Hmmm, it doesn't look like you're using any of the parameter binding code in the PDO library... Well, as long as $id and FILES is properly sanitised, I guess it's fine. Commented Jun 11, 2014 at 10:28

5 Answers 5

4

Instead of

return TRUE;

in your check_Files function do:

return $url;

And use it in following way:

if (($url = check_Files($id)) !== false){

    echo $url;

}

Or to pass an array of urls from database:

function check_Files($id){

    $files = sqlaccess::fetch("SELECT url FROM ".FILES." WHERE news_id=".$id." AND type = 'video' "); 

    if (count($files) > 0) {
        $urls = array();
        foreach($files as $file){
            $urls[] = $file['url'];
        }
        return $urls;
    } else {
        return FALSE;
    }

}

if (($urls = check_Files($id)) !== false){

    foreach ($urls as $url) {
        echo $url;
    }

}

The test can be simplified to:

if ($urls = check_Files($id)){}
Sign up to request clarification or add additional context in comments.

Comments

0

This is about variable scope. Inside the function, $url exists. Outside the function, it isn't around anymore. Your best bet is to return the URL instead of true/false:

function check_Files($id){

    $files = sqlaccess::fetch("SELECT url FROM ".FILES." WHERE news_id=".$id." AND type = 'video' "); 

    if (count($files) > 0) {
        foreach($files as $file){
            $url = $file['url'];
        return $url;
        }

    } else
    {
        return FALSE;
    }

}

Then outside in your code, you can call it like so:

$link=check_Files($id);
echo $link;

Or even simply

echo check_Files($id);

Comments

0

Make $url global inside the function and define $url above the function then only you can get the $url;

$url="";

function check_Files($id){
    global $url;

    $files = sqlaccess::fetch("SELECT url FROM ".FILES." WHERE news_id=".$id." AND type = 'video' "); 

    if (count($files) > 0) {
        foreach($files as $file){
            $url = $file['url'];
        return TRUE;
        }

    } else
    {
        return FALSE;
    }

}

if(check_Files($id) == TRUE){
    echo $url;
}

Check yourself and enjoy!

Comments

0

Is the function defined in the same file that is is being called ?? Because the error seems to be that you are trying to output a variable that has not be defined. A better approach would be

function check_Files($id){

    $files = sqlaccess::fetch("SELECT url FROM ".FILES." WHERE news_id=".$id." AND type = 'video' "); 

    if (count($files) > 0) {
        foreach($files as $file){
            $url = $file['url'];
        return $url
        }

    } else
    {
        return FALSE;
    }

}

if(check_Files($id) != FALSE){

echo check_Files($id);

}

Comments

0
function check_Files($id){

    $files = sqlaccess::fetch("SELECT url FROM ".FILES." WHERE news_id=".$id." AND type = 'video' "); 

    if (count($files) > 0) {
        foreach($files as $file){
            $url = $file['url'];
        return $url;
        }

    } else
    {
        return FALSE;
    }

}

and in html

if (($url = check_Files($id)) != false){

    echo $url;

}

1 Comment

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.