0

i have looked at the other results for what i'm trying to do, none of them do what i need them to. What i am trying to do is something like this:

myfunction(){
    require('./connect.php'); //connect to database
    $query = mysql_query("SELECT * FROM users WHERE username='$user'"); //user is defined outside the function but it works in my login function which i use the same way.
    $numrows = mysql_num_rows($query);
    if($numrows == 1){
        $row = mysql_fetch_assoc($query);
        $value = row['value'];
        mysql_close();
        return $value;
   } else {
       $errmsg = "connection failed.";
       $value = 0;
       return $value;
   }
}

In my php file i would do something like this at the top.

$value = myfunction();

This does not work. Ultimately what i'm trying to accomplish is getting a value from the database and output it from the function in another file. (this is my first post on stackoverflow so if i need to change this feel free to tell me and i shall)

Magicprog.fr
  • 4,072
  • 4
  • 26
  • 35
Nightmare
  • 3
  • 2
  • 4
  • Okay, the code you posted does return something into $value? have you printed it? – Marco Mura Nov 17 '14 at 13:52
  • is there a col named "value" or would you like to return e.g. the col "id"? – donald123 Nov 17 '14 at 13:54
  • $user is undefined, so you're doing `username=''`, basically. – Marc B Nov 17 '14 at 13:59
  • Marc $user is defined outside the function then passed to it. donald123 there is a col named value and it has a number in it. i would like to have that value taken out of the col and displayed to the user. Marco i have it just outputs 0. even when i have a value set in the database. – Nightmare Nov 17 '14 at 14:00
  • How are you passing $user to the function? Could you include that part perhaps? Because we don't see any parameters. I would suggest you to use the answer of lolka_bolka and add the parameter to it it should work perfectly. – Sinan Samet Nov 17 '14 at 14:17
  • Sinan in the actual function i am not using a perameter for my login/logout function and it works just fine. but i'll try it this way. – Nightmare Nov 17 '14 at 16:54

3 Answers3

1

Your code has several syntax error. Check this, and read my comments:

function myfunction() {
    //connect to database
    require('./connect.php');

    //user is defined outside the function but it works in my login function which i use the same way.
    $query = mysql_query("SELECT * FROM users WHERE username='" . mysql_real_escape_string($user) . "'");
    $numrows = mysql_num_rows($query);
    if ($numrows == 1) {
        $row = mysql_fetch_assoc($query); 
        return $row['value']; //Missing $ sign
        //No need to create $value if you just return with that.
        //mysql_close();
        //return $value;
    } else {
        //Where do you use this errmsg????
        $errmsg = "connection failed.";
        return 0;
        // These 2 lines are unnecessary.
        //$value = 0;
        //return $value;
    }
} //Missing function close

In my example, I've just leave the mysql functions, but please do not use them, they are deprecated. Use mysqli or PDO instead. Also, avoid sql injections by escapeing your variables!

vaso123
  • 12,347
  • 4
  • 34
  • 64
0
$row = mysql_fetch_assoc($query);
$value = row['value']; // <-------- you forgot the $

and most probably, the correct way to extract the result is,

$row[0]['value'];

Note:

Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Community
  • 1
  • 1
itachi
  • 6,323
  • 3
  • 30
  • 40
-1

i'm thinking you forgot a dot here.

require('./connect.php'); 

And a bit of function improvement

    myfunction(){
        require_once('../connect.php'); //connect to database
        $query = mysql_query("SELECT * FROM users WHERE username='".$user."'"); //user is defined outside the function but it works in my login function which i use the
        $numrows = mysql_num_rows($query);
        if($numrows == 1){
            $row = mysql_fetch_assoc($query);
            $value = $row['value'];
            mysql_close();
        }
        else{
            $errmsg = "connection failed.";
            $value = 0;
        }
        return $value;
    }
Sjoerd de Wit
  • 2,353
  • 5
  • 26
  • 45