473,398 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

use a variable in a function?

11
[PHP]function flash_search()
{
$query="SELECT score FROM hi WHERE name='$sname'";
$scre=mysql_query($query);
$res=mysql_fetch_array($scre,MYSQL_ASSOC);
echo $sname;
echo $res["score"];
}[/PHP]
This, by some crazy therory, should display the name and score of one person in my database.

This is where $sname="Test" (an existing user)
the database has 2 columns Name and Score. hi is the table's name.

Why doesn't this work? I am only a novice in PHP...

TA, Snaked
Apr 2 '08 #1
7 1480
ronverdonk
4,258 Expert 4TB
Since this is a function, did you define $sname as a global? If not: a function cannot address non-global variables. So you either pass the var to the function or you define it as global.

Ronald
Apr 2 '08 #2
Snaked
11
$sname=$_GET["name"];
if that is what you mean, yes.
Apr 2 '08 #3
ronverdonk
4,258 Expert 4TB
No, $_GET is global, $sname is not.

Ronald
Apr 2 '08 #4
Snaked
11
EDIT: Never mind, i fixed it myself. Whoops
Apr 2 '08 #5
ronverdonk
4,258 Expert 4TB
EDIT: Never mind, i fixed it myself. Whoops
So how did you fix it. Can we share that?

Ronald
Apr 2 '08 #6
aktar
105 100+
This post is for those who are seeking a solution to the same problem

As Ronald stated earlier, if you're going to include an external variable inside a function then that variable must be declared as global. Heres how:

[PHP]global $your_variable_name;[/PHP]
this will make your variable (and ofcourse its value) available inside the function.

NOTE: you do not have to do this for _POST, _GET _REQUEST etc
Apr 2 '08 #7
Markus
6,050 Expert 4TB
This post is for those who are seeking a solution to the same problem

As Ronald stated earlier, if you're going to include an external variable inside a function then that variable must be declared as global. Heres how:

[PHP]global $your_variable_name;[/PHP]
this will make your variable (and ofcourse its value) available inside the function.

NOTE: you do not have to do this for _POST, _GET _REQUEST etc
And if you create a variable inside a function, declaring it as global will give you access to it outside of that function, no?
Apr 2 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Scott | last post by:
I have an XML Document in a format like: <Variable name="Bob">ABCDEFG</Variable> <Variable name="Steve">QWERTYUI</Variable> <Variable name="John">POIUYTR</Variable> <Variable...
7
by: Klaus Johannes Rusch | last post by:
Is the following code valid and supported by current implementations? function somename() { this.show = function () { document.write("somename called") } } var somename = new somename();...
3
by: Grant Wagner | last post by:
Given the following working code: function attributes() { var attr1 = arguments || '_'; var attr2 = arguments || '_'; return ( function (el1, el2) { var value1 = el1 + el1; var value2 = el2...
3
by: Datta Patil | last post by:
Hi , #include<stdio.h> func(static int k) /* point2 : why this is not giving error */ { int i = 10 ; // static int j = &i ; /* point 1: this will give compile time error */ return k; } /*...
10
by: Blaxer | last post by:
There is probably a really easy way to do this, so please forgive me but I would like to set the value of a variable from a variable, an example would be... function Calculate_Something(ByVal...
23
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
4
by: Ray | last post by:
Hello, I think I've had JavaScript variable scope figured out, can you please see if I've got it correctly? * Variables can be local or global * When a variable is declared outside any...
1
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
2
by: Florian Loitsch | last post by:
hi, What should be the output of the following code-snippet? === var x = "global"; function f() { var x = 0; eval("function x() { return false; }"); delete x; alert(x); }
20
by: teddysnips | last post by:
Weird. I have taken over responsibility for a legacy application, Access 2k3, split FE/BE. The client has reported a problem and I'm investigating. I didn't write the application. The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.