1

Well, I have this function in a custom script for SMF:

$query = "SELECT id_member, real_name, id_group FROM smf_members WHERE id_group > 0 AND id_group != 9 AND id_group != 12 ORDER BY id_group ASC";
$result = mysql_query($query, $con);
function Display($member_id)
{
$queryDisplay = "SELECT value
 FROM smf_themes
 WHERE id_member = ".$member_id."
 AND variable = 'cust_realfi'";
}
while ($row = mysql_fetch_array($result)) {
Display($row['id_member']);
$resultDisplay = mysql_query($queryDisplay, $con);
echo ("<td>"); 
if (mysql_num_rows($resultDisplay) == 0) echo ("Not yet entered");
else {
 while ($rowDisplay = mysql_fetch_array($resultDisplay)) {
 if (strlen($rowDisplay['value']) > 0) echo ("".$rowDisplay['value'].""); 
 }
}
echo ("</td>"); 
}

If I echo ($member_id); it works just fine, but as soon as I put it like there ^, it doesn't do anything.

What am I doing wrong? :(

asked Jul 7, 2011 at 20:01
5
  • There is no echo in the Display function, as I can see. Commented Jul 7, 2011 at 20:03
  • 3
    Where you actually execute the query? Right now that function does nothing but create and destroy a variable that happens to contain some sql. Commented Jul 7, 2011 at 20:03
  • did you try to echo $queryDisplay; Commented Jul 7, 2011 at 20:04
  • What do you expect this function to do? You declare a variable which you don't use and then exit the function (eg. the variable is lost) Commented Jul 7, 2011 at 20:04
  • maybe query database and return results Commented Jul 7, 2011 at 20:05

5 Answers 5

1

is that the entire function? You are not even running the query. All you are doing is making a string with the sql and not running it.

Edit:

I think you need help in understanding variable scope in PHP.

http://php.net/manual/en/language.variables.scope.php

Basically it says that the code inside of a function has no access to variables (most variables) defined outside of the function. So for example:

$test = "test value";
function testFunc(){
 //Since the code inside this function can't
 //access the variables outside of this function
 //the variable $test below is just an empty
 //variable.
 echo $test;
}
testFunc();

This also works in reverse where variables inside of a function can't be accessed outside that function. This is what you are doing wrong.

function testFunc(){
 $someNewVar = "some new string";
}
testFunc();
//$someNewVar is never defined outside the function so it doesn't exist.
echo $someNewVar;

Once a function is done running, all variables declared and used locally inside of it are deleted from memory.

So to get a variable into the function, you need to pass it as an argument. To get a variable out, you need for the function to return the variable.

function testFunc($testVar){
 echo $testVar;
 $testVar = "some new string";
 return $testVar;
}
$test = "test val";
//passing the variable into the function and setting
//the return value back into $test.
$test = testFunc($test);
echo $test; //test now has "some new string".

Honestly, the php page will describe it better than I can. but this should give you an idea of what is wrong.

answered Jul 7, 2011 at 20:04
Sign up to request clarification or add additional context in comments.

3 Comments

No, it's still the same. You have a function that sets a variable. That's it. You need to either return the variable, echo the variable, or set a global variable, which I strongly recommend against. Return or echo it. Look up variable scope.
Ye, but you don't get it. It works perfectly fine. I just can't get the function to define $queryDisplay. If I put echo ($member_id); in the function and call it, it displays the information I want.
@Aart Thing is, it doesn't "work perfectly fine". Or else you wouldn't be asking for help. the function needs to return $queryDisplay and you need to set it back in to $queryDisplay outside of the function. so you need to add return $queryDisplay; as the last line of the function and when you call the function do $queryDisplay = Display($row['id_member']);. like I said above, you can't define the value inside of the function and use it outside. Variables inside a function are not available outside. Once the function is over, any variables used in the function are destroyed.
1

Your function doesn't do anything: It assigns a value to a local variable, and then does nothing with the value and the variable. It should either return it (the value) or execute the query.

Also note that doing string concatenation to put the variable in the query is creating a security hole: http://bobby-tables.com/

You should consider using mysqli and parametrized queries as suggested here: http://bobby-tables.com/php.html

At the very least, consider quoting the values you include in the query with the functions provided by PHP to do so. All values. But parametrized queries are better and easier.

answered Jul 7, 2011 at 20:06

Comments

0

You are just preparing sql query, but not executing it.

answered Jul 7, 2011 at 20:04

Comments

0

you function only makes a variable $queryDisplay (that is traped within the scope of the function)

answered Jul 7, 2011 at 20:05

Comments

0

your query

function Display($member_id){
$queryDisplay = "SELECT value FROM smf_themes WHERE id_member = ".$member_id." AND variable = 'cust_realfi'";
}

right query

 function Display($member_id){
 $queryDisplay = mysql_query("SELECT value FROM smf_themes WHERE id_member = '$member_id' AND variable = 'cust_realfi'");
 while ($row = mysql_fetch_array($queryDisplay));
 return $row['value'];
 }
answered Jul 7, 2011 at 20:03

8 Comments

lmao why vote down?!? what is wrong with this. post your reasoning for lowering an answer... i swear i think someone is following me around downvoting me...
Maybe he is missing some code from the Display function. As such, it shouldn't be labeled as 'right query'. (?)
there is nothing wrong with it, if the connection string is initialized outside of the function, then the code works fine, worked for me.
@Matthew Warner - You are returning $row['value'] but where is $row declared?
roflcopter down!!! guess thats why i should still be sleeping lmao. sorry bout that i changed it
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.