Page 1 of 1
Passing variables [SOLVED]
Posted: Fri Aug 17, 2012 3:15 am
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...
Re: Passing variables...
Posted: Fri Aug 17, 2012 9:14 am
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?
Re: Passing variables...
Posted: Fri Aug 17, 2012 1:09 pm
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.
Re: Passing variables...
Posted: Fri Aug 17, 2012 2:21 pm
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
Re: Passing variables...
Posted: Fri Aug 17, 2012 2:34 pm
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.
Re: Passing variables...
Posted: Fri Aug 17, 2012 3:14 pm
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?
Re: Passing variables...
Posted: Fri Aug 17, 2012 3:32 pm
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.