Passing variables [SOLVED]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
robertb13
Forum Newbie
Posts: 22
Joined: Thu Aug 16, 2012 5:04 pm

Passing variables [SOLVED]

Post by robertb13 »

I've been on Google for like an hour spinning in circles. I'm lost and can't get my head wrapped around the passing of variables...

I created the variable $surQid

SIde note: is there an easier way to get the 1st row/1st column of a database then what I used? Do I even need to create an array?

<u>File: time_spent.php</u>
$query = "SELECT * FROM survey WHERE survey_id = 1";
$results = mysqli_query($dbc, $query);
$surID = mysqli_fetch_array($results);
$surQid = $surID[0];

How do I make that variable available for use in sandbox.php? I would like to learn how to use "Sessions" as that seems to be the cool way to do it.
Love to learn a couple different ways!

Thanks for your help...
Last edited by robertb13 on Tue Aug 28, 2012 6:21 pm, edited 3 times in total.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Passing variables...

Post by social_experiment »

robertb13 wrote:How do I make that variable available for use in sandbox.php?
Do you want to make the variable available to a different page?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
robertb13
Forum Newbie
Posts: 22
Joined: Thu Aug 16, 2012 5:04 pm

Re: Passing variables...

Post by robertb13 »

Yes.. I think I made it work, but it seems to only recognize the variable AFTER refresh...

IN time_spent.php

<?php session_start();?>
......
$query = "SELECT * FROM survey WHERE survey_id = 1";
$results = mysqli_query($dbc, $query);
$surID = mysqli_fetch_array($results);
$_SESSION["surveyid"]=$surID[0];

IN sandbox.php

<?php session_start(); ?>
.........
$survID = $_SESSION["surveyid"];


```````

When I echo $survID it returns the correct value, put only AFTER a refresh. The first time I visit, the echo returns nothing.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Passing variables...

Post by califdon »

robertb13 wrote:SIde note: is there an easier way to get the 1st row/1st column of a database then what I used? Do I even need to create an array?

<u>File: time_spent.php</u>
$query = "SELECT * FROM survey WHERE survey_id = 1";
$results = mysqli_query($dbc, $query);
$surID = mysqli_fetch_array($results);
$surQid = $surID[0];
$results will contain all the data resulting from your query; in this case, you are expecting only one row, corresponding to a specific ID number. But whether there are just one or many rows, you have to fetch a row at a time, as you did. Whenever you fetch a row, it creates an array, although you can choose to index the array either by the number, as you are doing, or by the name of the field (in which case you would use mysqli_fetch_associative()* and refer to it as $surID['survey_id']. It is more common practice to use the variable names $result and $row for the intermediate values of a query, like this:

Code: Select all

$query = "SELECT survey_id FROM survey WHERE survey_id = 1";
$result = mysqli_query($dbc, $query);
$row = mysqli_fetch_array($result);
$surQid = $row[0];
This becomes easy to recognize as you read the code--$result is the result array of a query, $row is one row fetched from a result. You will notice also that I did not use the asterisk in the SELECT statement. It makes no difference in the data that is returned, but it's a good habit to form because when you later may be dealing with very large databases and complex queries, it is more efficient and easier to follow if you select only those columns (fields) that you are going to need for a query.

[*] http://php.net/manual/en/mysqli-result.fetch-array.php
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Passing variables...

Post by califdon »

robertb13 wrote:Yes.. I think I made it work, but it seems to only recognize the variable AFTER refresh...

IN time_spent.php

<?php session_start();?>
......
$query = "SELECT * FROM survey WHERE survey_id = 1";
$results = mysqli_query($dbc, $query);
$surID = mysqli_fetch_array($results);
$_SESSION["surveyid"]=$surID[0];

IN sandbox.php

<?php session_start(); ?>
.........
$survID = $_SESSION["surveyid"];


```````

When I echo $survID it returns the correct value, put only AFTER a refresh. The first time I visit, the echo returns nothing.
I'm not sure what you mean; you run time_spent.php, which sets a session variable, then you run sandbox.php and it doesn't immediately use the value of the session variable until you refresh sandbox.php? Of course if you have previously opened sandbox.php and it's still open, there's no PHP code to run until you open or refresh the page.
robertb13
Forum Newbie
Posts: 22
Joined: Thu Aug 16, 2012 5:04 pm

Re: Passing variables...

Post by robertb13 »

Thank you for the clear description of $result and $row

I'm not sure what you mean; you run time_spent.php, which sets a session variable, then you run sandbox.php and it doesn't immediately use the value of the session variable until you refresh sandbox.php?

If I echo $_SESSION and clear my cache then reload, it does not show the "1". when I refresh it shows.
Is that normal?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Passing variables...

Post by califdon »

robertb13 wrote:Thank you for the clear description of $result and $row

I'm not sure what you mean; you run time_spent.php, which sets a session variable, then you run sandbox.php and it doesn't immediately use the value of the session variable until you refresh sandbox.php?

If I echo $_SESSION and clear my cache then reload, it does not show the "1". when I refresh it shows.
Is that normal?
I'm still not getting the picture -- tell me WHICH script(s) you are running and refreshing.
Post Reply