We help IT Professionals succeed at work.

Grab first row in field and create session

pixelscape
pixelscape asked
on
Medium Priority
353 Views
Last Modified: 2012-06-21
How do I grab the first row in a field and apply to session? My output with echo at the bottom = array.


$result = mysql_query("SELECT max(logosetup) FROM app ");

$logoprice = mysql_fetch_row($result);
session_register('logoprice');
$_SESSION['logoprice'] = $logoprice;

// Testing
echo $logoprice;
Comment
Watch Question

do a print_r($logoprice); it will let you see the contents of the array including any keys.
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
this should do:
$result = mysql_query("SELECT max(logosetup) logosetup FROM app ");

$row = mysql_fetch_row($result);
$logoprice = $row["logosetup"];
session_register('logoprice');
$_SESSION['logoprice'] = $logoprice;

// Testing
echo $logoprice;

Open in new window

also to test what is in the session do this on the session variable itself instead of $logoprice, so do a print_r($_SESSION);  to see what is currently in your session.
Greg AlexanderLead Developer

Commented:
Try this:

$result = mysql_query("SELECT max(logosetup) FROM app ");

$data = mysql_fetch_row($result);
$logoprice = $data[0]['logosetup'];

session_register('logoprice');
$_SESSION['logoprice'] = $logoprice;

// Testing
echo $logoprice;

Open in new window

Most Valuable Expert 2011
Author of the Year 2014
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Most Valuable Expert 2011
Author of the Year 2014

Commented:
Discussion by line numbers

2. You really want to see all the possible errors and notices while you are debugging a script.
4. Required at the top of every page when you want to use sessions.  Most frameworks have this in a common script that is included before anything else.
6. Assigning a predictable name to the key in the results set array.  LIMIT might or might not make sense.  Haven't tested it.
7. Testing the query for success and visualizing the error message, if any (eliminates the guesswork).
9. Retrieve the one and only row
10. Do not use session_register() See http://php.net/manual/en/function.session-register.php.  Just copy the data to the $_SESSION array.
12. Makes the var_dump() output easy to read
13, et seq.  Show the work products from the code we just executed.

HTH, ~Ray
<?php 
error_reporting(E_ALL);

session_start();

$result = mysql_query("SELECT max(logosetup) AS logoprice FROM app LIMIT 1");
if (!$result) die( mysql_error() );

$row = mysql_fetch_assoc($result);
$_SESSION['logoprice'] = $row["logoprice"];

echo "<pre>";
var_dump($row);
var_dump($_SESSION);

Open in new window

Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.