Page 1 of 5
turning database fetched values into $variables?
Posted: Thu Jun 21, 2007 2:05 pm
by revocause
Hi,
i pulled some data from a database and its working .
the problem is, i never had to do convert anything before.
So how would i convert a 'fetched' database value into a useable php variable to pass on in sessions?
example (and i hope i do the tags right so i dont get in trouble l o l )
On tha tpage its working great as $quantity.
but i need to get it into sessions. I thought you could just use
something like
Code: Select all
$ SESSION['quantity'] = $_POST['quantity'];
look feyd! no re-editing!

...i hope.
Posted: Thu Jun 21, 2007 2:31 pm
by volka
If youw want to store the value of $row['quantity'] (which already is a "usable" php string) as session data just assign it to an element of the array _SESSION
Code: Select all
$_SESSION['quantity'] = $row['quantity'];
Posted: Thu Jun 21, 2007 2:40 pm
by revocause
I'm doin that , but im getting this Warning.
"Warning: Unknown: Your script possibly relies on a session side-effect which existed until PHP 4.2.3. Please be advised that the session extension does not consider global variables as a source of data"
I'm pretty sure its talking about this
Code: Select all
$_SESSION['quantity'] = $row['quantity'];
I dont want that warning to be popping up on webpages : ) ..anyway to avoid that , so that no users see that message?
Posted: Thu Jun 21, 2007 3:02 pm
by superdezign
It says global variables.. Is there anything else you are doing with sessions? Maybe using old session_register() functions or something? Or are you actually trying to use global variables in your session? It's gotta be something beyond that one line.
Posted: Thu Jun 21, 2007 3:25 pm
by volka
Does your script contain a call to session_register()?
Posted: Thu Jun 21, 2007 3:39 pm
by revocause
I have two things going on with SESSIONS.
The first starts at top of page.
Code: Select all
<?php
session_start();
if (isset($_POST['product_name'] )) {
$_SESSION['product_name'] = $_POST['product_name'];
$_SESSION['quantity'] = $row['quantity'];
}
?>
That's working. Except for that Warning message popping up.
the Second thing I'm using SESSIONS with is, = I have a select menu 'save history' of user select.
so its basically looks like this. (and there are approx 7 of them on the page)
Code: Select all
<?php
// select menu product_1
function makeSelectMenu2($menuName2, $options2, $chosen2) {
$list2 = '';
foreach
($options2 as $option2 => $text2) {
$selected2 = ($chosen2 == $option2) ? ' selected="selected"' : '';
$list2 .= " <option value=\"$option2\"$selected2>$text2</option>\n";
}
return "\n<select name=\"$menuName2\" STYLE=\"width: 110 px\" id=\"$menuName2\">\n$list2</select>\n";
}
session_start();
$options2 = array(
'' => 'Please Select ',
'1' => '500',
);
$menuName2 = 'product_1';
$chosen2 = '';
if (isSet($_POST[$menuName2])) $_SESSION[$menuName2] = $_POST[$menuName2];
if (isSet($_SESSION[$menuName2])) $chosen2 = $_SESSION[$menuName2];
$selectMenu2 = makeSelectMenu2($menuName2, $options2, $chosen2);
?>
there is a session start within each one of those (and there are 7 in all)
Its all working.
But im getting that error.
Also, the reason i feel this line is causing the problem, is because if i take it out , everything works, and no erorrs.
But, i need that variable 'quantity'
Code: Select all
$_SESSION['quantity'] = $row['quantity'];
Posted: Thu Jun 21, 2007 4:02 pm
by volka
So now is there a call to session_register() somewhere?
Posted: Thu Jun 21, 2007 6:58 pm
by bdlang
What's unclear to me is, where does $row['quantity'] come from? You have shown several lines of code but not one of them concerns the initiation of the $row array. The warning you're getting is likely because PHP 'thinks' $row is a global variable.
Posted: Fri Jun 22, 2007 7:37 am
by revocause
the $row is coming from ,
Code: Select all
<?php
// db query fetch for quantity, baseprice , weight.
if(isset($_POST['product_1']))
{
$sql = "SELECT quantity, baseprice, weight FROM db_product WHERE id = " . $_POST['product_quantity'];
$res = mysql_query($sql);
$row = mysql_fetch_assoc($res);
$quantity = $row['quantity'];
$baseprice = $row['baseprice'];
$weight = $row['weight'];
}
?>
So that line of code is valid, because its getting info from database.
everything works until that SESSIONS $row is put in.
Any idea whats making it cause that warning?
Posted: Fri Jun 22, 2007 7:53 am
by volka
is there a call to session_register? [yes | no | i don't understand]
Posted: Fri Jun 22, 2007 8:16 am
by bdlang
revocause wrote:the $row is coming from
...
Any idea whats making it cause that warning?
Not really, because you keep posting small snippets of unrelated code.
You don't make the $_SESSION assignment within that block of code, so where you do make it?
As
volka keeps asking, and I'm alluding to, POST THE RELEVANT CODE.
You have no troubleshooting in your code, how do you know that query even executes properly? That
$row contains any data?
Posted: Fri Jun 22, 2007 9:15 am
by revocause
I'm not using session_register()
the full code to this:
SESSIONS ( take note that 'product_name' is irrelevant to the product_main select menu)
Code: Select all
<?php
session_start();
if (isset($_POST['product_name'] )) {
$_SESSION['product_name'] = $_POST['product_name'];
$_SESSION['quantity1'] = $_POST['quantity1'];
}
?>
The Select menu (with a history session to save user selected options) :
Code: Select all
<?php
//select menu product_main
function makeSelectMenu($menuName1, $options1, $chosen1) {
$list1 = '';
foreach
($options1 as $option1 => $text1) {
$selected1 = ($chosen1 == $option1) ? ' selected="selected"' : '';
$list1 .= " <option value=\"$option1\"$selected1>$text1</option>\n";
}
return "\n<select name=\"$menuName1\" STYLE=\"width: 110 px\" id=\"$menuName1\">\n$list1</select>\n";
}
session_start();
$options1 = array(
'' => 'Please Select ',
'1' => '500',
'2 ' => '1000',
'3 ' => '1500',
'4 ' => '2000',
'5 ' => '2500',
'6 ' => '3000',
'7 ' => '3500',
'8 ' => '4000',
'9 ' => '4500',
'10 ' => '5000',
'11 ' => '6000',
'12 ' => '7000',
'13 ' => '8000',
'14 ' => '9000',
'15 ' => '10000',
);
$menuName1 = 'product_main';
$chosen1 = '';
if (isSet($_POST[$menuName1])) $_SESSION[$menuName1] = $_POST[$menuName1];
if (isSet($_SESSION[$menuName1])) $chosen1 = $_SESSION[$menuName1];
$selectMenu1 = makeSelectMenu($menuName1, $options1, $chosen1);
?>
The call to the database:
Code: Select all
<?php
// db query fetch for quantity, baseprice , weight.
if(isset($_POST['product_main']))
{
$sql = "SELECT quantity, baseprice, weight FROM b1_product WHERE id = " . $_POST['product_main'];
$res = mysql_query($sql);
$row = mysql_fetch_assoc($res);
$quantity = $row['quantity'];
$baseprice = $row['baseprice'];
$weight = $row['weight'];
}
?>
Turn quantity data into $variable for sh8ts and giggles:
All that will spit out a select menu.
When you make selection, it takes that options value="" and uses it to search that id number in database.
I have 7 select menus like that in total, but edited them out of this for space.
It all works, but im getting that Warning message:
Warning: Unknown: Your script possibly relies on a session side-effect which existed until PHP 4.2.3....blah blah blah
Posted: Fri Jun 22, 2007 9:58 am
by volka
revocause wrote:Also, the reason i feel this line is causing the problem, is because if i take it out , everything works, and no erorrs.
But, i need that variable 'quantity'
Code: Select all
$_SESSION['quantity'] = $row['quantity'];
There's no such code line ein your "the full code" post.
Posted: Fri Jun 22, 2007 10:04 am
by revocause
thats because i was taking it out
where the variables for quantity changed.
I'm messing with it and when i posted it i had already changed it , tyring to figure it out. sorry.
the warning still happens as long as I'm using 'quantity' . a database fetched data.
So im pretty sure the warning is being caused in that area .
I'm only saying that to help somebody out that might be tyring to figure this out.
Because if i take out the database fetch code. Then the page is standard php with basic variables already on the page.
when i add the database sql fetch. The warning happens.
So I've narrowed down that it obviously must be coming from the sql variable conversion.
there's really nothing else in the code that would do that .
I'm changing and experimenting with the code as i type this.
and I have to edit it to post a section here . trust me you dont want me posting the whole entire code length. its pretty darn big. : )
Posted: Fri Jun 22, 2007 11:09 am
by volka
You get this warning if you have a global variable with a value !=NULL and an element with the same name in _SESSION but with NULL as value.
simple example
Code: Select all
<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
session_start();
$quantity = 'xyz';
$_SESSION['quantity'] = NULL;
and according to the php 5.2.0 sources (and my understanding of the code

) that's the only way you can get this warning.