• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 343
  • Last Modified:

Grab first row in field and create session

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;
0
pixelscape
Asked:
pixelscape
1 Solution
 
haloexpertsexchangeCommented:
do a print_r($logoprice); it will let you see the contents of the array including any keys.
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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

0
 
haloexpertsexchangeCommented:
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.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Greg AlexanderLead DeveloperCommented:
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

0
 
Ray PaseurCommented:
Try something more like this:
<?php
error_reporting(E_ALL);

session_start();

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

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

echo "<pre>";
var_dump($row);
var_dump($_SESSION);
0
 
Ray PaseurCommented:
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

0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now