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...
Passing variables [SOLVED]
Moderator: General Moderators
Passing variables [SOLVED]
Last edited by robertb13 on Tue Aug 28, 2012 6:21 pm, edited 3 times in total.
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: Passing variables...
Do you want to make the variable available to a different page?robertb13 wrote:How do I make that variable available for use in sandbox.php?
“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
Re: Passing variables...
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.
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.
Re: Passing variables...
$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: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];
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];[*] http://php.net/manual/en/mysqli-result.fetch-array.php
Re: Passing variables...
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 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.
Re: Passing variables...
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 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?
Re: Passing variables...
I'm still not getting the picture -- tell me WHICH script(s) you are running and refreshing.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?