[help] PHP and MYSQL problem, please help. not returning variables correctly...

Hey guys, I'm working on a website with PHP and MYSQL with my roommate, and am running into several snags. I was wondering if any of you would be able to help. Here is the situation:

this is a roster page I'm making. every person in my band at the beginning of the year has an index card that they write their information on. I'm trying to replicate that on this page. each person has an index card, the information is outputted on it. the roster is going to do the same thing. the idea is to click on a link in the (horizontal) scrolling box and it will refresh the information on the card

the information on the card is a set of global variables. all of them are session data that is fetched from the database when the user logs in. even though this is frowned upon usually, its the best way to do it in this scenario, because of the amount of times the info is being used on the site.

so far, my roster program fetches the usernames from the database, and displays them with an image above it, as it should. I"m running into a few snags:

the link to each person is going to have to query the database with their information. after the query, I can get the information from the database, update the variables in an array and then assign those global variables new values to be outputted on the page on the index card.

I've made the function below do just that. its at the bottom of this page. it gets the information from the database, stores it in an array, and I *think* assigns all of them values. it then *should* update the global variables and refresh only that part of the screen.

the link as you can see is a call to the function, it sends the global variable "$username" to the function. the function then takes $username, stores it in $received_username, and passes it to the SQL query. everything should then update. Am I doing this wrong?

I may be having a problem with the scope of the variables in the function, even though they are global variables, I'm not sure if the function recognizes them as such. what do you think?

The program used to return the usernames as links in the loop, which gave me hope. It is no longer doing that and I don't know what I did to mess it up.

Anyway, this is my situation. Any ideas?

					$get_username_query = "SELECT username FROM UCMB_Table";

$result_get_username_query = mysql_query($get_username_query, $connection)

or die ("And the dragon comes in the niIIIIght!.. i mean... Unable to execute get_username_query");

$number_of_rows = mysql_num_rows($result_get_username_query);



$width_of_inline_table = (150 * $number_of_rows) + (20 * ($number_of_rows - 1)) + (8.75 * ($number_of_rows - 1));

$width_of_inline_table .= "px";



/* ============== SELECT QUERY FOR PEOPLES ROSTER CARDS ============== */



if ($log_out != yes) /* LOGGED IN */

{

echo"<b>Name:</b> $first_name $last_name <b>Instrument:</b> $primary_instrument


<b>Class:</b> $class_standing <b>Address:</b> $city, $state <b>Phone:</b> $cell_phone


<b>Major:</b> $intended_major <b>Address on Campus:</b> $campus_address




<div class='edit_profile text_center double_break single_break_top'>

<p>

<div class='edit_profile_links'>

<ul>

<li>$first_name $last_name</li>


<li>$primary_instrument</li>


<li>$class_standing</li>


<li>$city, $state</li>


<li>$intended_major</li>


<li>$campus_address</li>


<li>$cell_phone</li>

</ul>

</div>

</p>

</div>



<div class='iframe_css'>

<p style='width: $width_of_inline_table;'>";



if ($number_of_rows == 0)

{

echo "Your search returned no results; please try another search.";

}



else

{

while ($username_array = mysql_fetch_assoc($result_get_username_query))



{

extract($username_array);

$number_of_rows--;



if ($number_of_rows != '0')

{



echo"

<span class='span_block text_center' style='float: left; margin-right: 20px;'>

<img src='../schedule_of_events_index-t.jpg' width='150' height='74' alt='' title='' style='margin: 0 0 5px;' />




<a href='";input_username($username);echo"' title=''>$username</a>

</span>

";

}

Here is the function for the code above:

function input_username($received_username)

{

//Initialize MySQL variables

blah blah blah



//Open MySQL connecion

$connection = mysql_connect($host, $user, $mysql_password)

or die ("Stupid Crap for Crap...Could not connect to the server");



//Open MySQL database

$db = mysql_select_db($databasename, $connection)

or die ("Crapfully Yours, Unable to select the database");



$get_index_card_query = "SELECT first_name, last_name, email_address, aim_screenname, cell_phone, campus_phone, campus_address, class_standing, intended_major, date_of_birth, home_phone, city, state, high_school, primary_instrument FROM UCMB_Table WHERE username LIKE "$received_username%"";



$result_get_index_card_query = mysql_query($get_index_card_query, $connection)

or die ("And the dragon comes in the niIIIIght!.. i mean... Unable to execute result_get_index_card_query");



$index_card_array = mysql_fetch_array($result_get_index_card_query);



// Assign Values To Global Variables

$first_name = $index_card_array['first_name'];

$last_name = $index_card_array['last_name'];

$email_address = $index_card_array['email_address'];

$aim_screenname = $index_card_array['aim_screenname'];

$cell_phone = $index_card_array['cell_phone'];

$campus_phone = $index_card_array['campus_phone'];

$campus_address = $index_card_array['campus_address'];

$class_standing = $index_card_array['class_standing'];

$intended_major = $index_card_array['intended_major'];

$date_of_birth = $index_card_array['date_of_birth'];

$home_phone = $index_card_array['home_phone'];

$city = $index_card_array['city'];

$state = $index_card_array['state'];

$high_school = $index_card_array['high_school'];

$primary_instrument = $index_card_array['primary_instrument'];



// Close MySQL Connection

mysql_close($connection);



return;

}

#275177

I'm not really sure but you have to rewrite your function like this:

[color=#000000][color=#007700]function [/color][color=#0000bb]input_username[/color][color=#007700]([/color][color=#0000bb]$received_username[/color][color=#007700]) 
{
[b]global [/b][/color][/color][b][color=#000000][color=#0000bb]$first_name = '';[/color][/color][/b]
[color=#000000][color=#007700] [/color][color=#ff8000]//Initialize MySQL variables
[/color][color=#0000bb]blah blah blah

[color=#007700] ...[/color][/color][color=#007700]
[/color][color=#ff8000]// Assign Values To Global Variables
[/color][/color][color=#000000][color=#0000bb]$first_name[/color][/color][color=#000000][color=#0000bb] [/color][color=#007700]= [/color][color=#0000bb]$index_card_array[/color][color=#007700][[/color][color=#dd0000]'first_name'[/color][color=#007700]];
[/color][color=#007700]
[/color][color=#ff8000]// Close MySQL Connection
[/color][color=#0000bb]mysql_close[/color][color=#007700]([/color][color=#0000bb]$connection[/color][color=#007700]);

return;
} [/color][/color]

and global var $first_name will have a new value...

#275308

ok...the thing is its global already, or at least I thought so b/c its declared in the session information. so you're telling me that the function doesn't recognize those values as global?

#275366

yep.

you have to tell function to use global var instead of create new local one...

if you create a class then you have to write like this

class Foo{
var Dummy;
function Foo(){
$this->Foo = 'test value';
}
}

if just write a function then you have to write like this

$GLOBALS['globalVar'] = 'bla-bla-bla';
function Foo(){
global $globalVar;
$globalVar = 'new value';
}

if you ommit "global $globalVar" then PHP create new local variable and will use it within function and your real global one stay untouched ;)

edit

Try to check php.net. It have a very good documentation with a-la forum all-in-one ))

You can find there a very good samples and ideas...

#275369

I did that and it still didn't work. here is a copy of my function, if you'd like to look at it

	function input_username($received_username)

{

global $received_username;



//Initialize MySQL variables //Open MySQL connecion //Open MySQL database



$get_index_card_query = "SELECT first_name, last_name, email_address, aim_screenname, cell_phone, campus_phone, campus_address, class_standing, intended_major, date_of_birth, home_phone, city, state, high_school, primary_instrument FROM UCMB_Registered_Users WHERE username LIKE "$received_username%"";



$result_get_index_card_query = mysql_query($get_index_card_query, $connection)

or die ("And the dragon comes in the niIIIIght!.. i mean... Unable to execute result_get_index_card_query");



$index_card_array = mysql_fetch_array($result_get_index_card_query);



// Assign Values To Global Variables

global $first_name;

global $last_name;

global $email_address;

global $aim_screenname;

global $cell_phone;

global $campus_phone;

global $campus_address;

global $class_standing;

global $intended_major;

global $date_of_birth;

global $home_phone;

global $city;

global $state;

global $high_school;

global $primary_instrument;



$first_name = $index_card_array['first_name'];

$last_name = $index_card_array['last_name'];

$email_address = $index_card_array['email_address'];

$aim_screenname = $index_card_array['aim_screenname'];

$cell_phone = $index_card_array['cell_phone'];

$campus_phone = $index_card_array['campus_phone'];

$campus_address = $index_card_array['campus_address'];

$class_standing = $index_card_array['class_standing'];

$intended_major = $index_card_array['intended_major'];

$date_of_birth = $index_card_array['date_of_birth'];

$home_phone = $index_card_array['home_phone'];

$city = $index_card_array['city'];

$state = $index_card_array['state'];

$high_school = $index_card_array['high_school'];

$primary_instrument = $index_card_array['primary_instrument'];



// Close MySQL Connection

//mysql_close($connection);



return;

}

and here is the function call, in case I"m messing that up too...

<a href='";input_username($username);echo"' title=''>$username</a>

I don't get this at all. I'm trying to make a global variable of the received_username variable and output it to see what its getting. it isn't getting anything. that doesn't make sense...

I commented out // Close Connection because I wasn't sure whether it was conflicting with the main program, as in whether me closing the database at the end of the function was affecting its ability to return values to me ( I wouldn't think so as its already gotten what it needs from the database...)

any ideas?

#275490

Then i didn't understand what exactly you want to do... I'll try to imagine )


<?php
$username = 'Unknown';

function setUserName($value){
global $username;

$sql = 'select FirstName from utable where usercode = '.$value;
#...
#execute and fetch $sql
#...
#$username = $row['FirstName'];
$username = 'Fetched name in func setUserName';
}

function setUserName2(&$value){
#to be short i just write simple =)
$value ='Fetched name in func setUserName2';
}

function setUserName3(&$value){
#to be short i just write simple =)
$value ='Fetched name in func setUserName3';
return $value;
}
###################################
#ok. now HTML part
###################################
echo $username.'';

setUserName($username);
echo ''.$username.'';

setUserName2($username);
echo ''.$username.'';

$username = setUserName3($username);
echo ''.$username.'';
?>

Now just copy&paste this sample into your empty php file and watch you get...

I think you will solve your problem ))

there 2 way to set global var: by declare it global and by reference...

Is it what you want?

edit

Added 3-rd variant - with return clause =)

And you can ommit return if didn't return anything ^^ (check your code)

#275587

After switch to PHP5 i found that code i suggest stop working... i.e.


$var
function x(){
global $var;

$var = 1;
echo $var; #1
}
echo $var; #empty

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

After some RTFM ;) i fount a workaround:

 
$tplData = array();
$content = new Content;

function contentHome(){
$art = $GLOBALS['content']->getArticle(-1, SECTION_VEXHIBITION);
$GLOBALS['tplData']['article'] = $art;
}
print_r($tplData);#filled with data fetched within function...

#277900

hey, thank you! :) I will try that out and see if it works

#282258

$GLOBALS['var_name'] will work 100% ;)

i use right now and it works perfectly

#282443