Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How do I make the results returned by a SELECT querie into SESSION variables?

Posted on 2014-03-26
6
Medium Priority
?
572 Views
Last Modified: 2014-03-26
I am building an html php mailer email and need to add to the body. So far I have the following code which is working fine:
$mail->Body    = '<h1>Extra Leverage</h1> ' . $_SESSION['Firstname'] . ' ' . $_SESSION['TempID'] . '<br />
Your application to become a Member has been accepted.<p>Your Membership will be activated, giving you access to all<br>
Member benefits .<br>
Contact your Benefactor (below) for instructions.<p>
<b>Please contact for information:<br>'. $_SESSION['GuestID'] ;  

I need to add a name,email and phone number to the body. I have done a Select query to get the values;
         $query = "SELECT Fullname, EMail, Phone ";
       $query .= "FROM customers";
       $query .= " WHERE cid = {$GuestID}";
How can I turn these 3 values into variables or SESSION variables that I can use in the email body? Iwould like to end up with:
Please contact for information
           Fullname
             Email
            Phone
0
Comment
Question by:dynorich
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 

Author Comment

by:dynorich
ID: 39955915
<?php
   //3. Use returned data (if any)
   while($row = mysqli_fetch_row($result)) {
      // output data from each row
        echo $row[0] . "<br />";
        echo $row[1] . "<br />";
        echo $row[2] . "<br />";
        echo "<hr />";
   }


?>
The above code shows the info I want to add to the email. However, adding $row[0] etc doesn't work in the email.
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 39955990
you can do indeed:
$SESSION["xxxxx"] = $row[...];

and later in the code use the $SESSION array.
0
 

Author Comment

by:dynorich
ID: 39956035
The data never displayed. Here is a list of SESSION Variables:
 SESSION VARIABLES

      Array
(
    [TempID] => 625961
    [GuestID] => 899820
    [cid] => 899820
    [Firstname] => emailtest3
    [Lastname] => Genealogy
    [EMail] => edthomas@telus.net
    [St1] => B.C.
    [Country] => U.S.A.
    [Phone] => 604 532-7919
    [Fullname] =>
    [Email] =>
    [SponsorsPhone] =>
)
0
Independent Software Vendors: 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!

 

Author Comment

by:dynorich
ID: 39956039
Maybe the problem is in this code:
<?php session_start();

$_SESSION['Fullname'] = $row[0];
$_SESSION['Email'] = $row[1];
$_SESSION['SponsorsPhone'] = $row[2];
?>
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39956041
How can I turn these 3 values into variables or SESSION variables
The basics of how to run a query, test for success and retrieve the data are available in a number of books on PHP.  If you had to choose one, I would start with Welling/Thompson.  Get the latest edition and when a new edition comes out, get the new edition and give the old edition to one of your enemies.

If you're new to PHP and want to learn the basics, this article will be able to point you to some good learning resources, and more importantly, steer you away from the many bad PHP code examples that litter the internet.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

The PHP session may or may not be directly related to what you're doing, but it's just an array that offers the promise of persistent data between requests.  You assign variables the same way you assign variables in any other array, with the equal sign.  More information on the session is available here:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11909-PHP-Sessions-Simpler-Than-You-May-Think.html

This is an excerpt from my teaching example showing how to run queries and retrieve results.  Please read it over and post back if you still have questions.  And good luck with your project! ~Ray

<?php // RAY_EE_mysqli_example.php
ini_set('display_errors', TRUE);
error_reporting(E_ALL);
echo '<pre>';

// THE ABSOLUTE MINIMUM YOU MUST UNDERSTAND TO USE PHP AND MYSQLI
// MAN PAGE: http://php.net/manual/en/mysqli.overview.php
// MAN PAGE: http://php.net/manual/en/class.mysqli.php
// MAN PAGE: http://php.net/manual/en/class.mysqli-stmt.php
// MAN PAGE: http://php.net/manual/en/class.mysqli-result.php
// MAN PAGE: http://php.net/manual/en/class.mysqli-warning.php
// MAN PAGE: http://php.net/manual/en/class.mysqli-sql-exception.php <-- DID NOT WORK PHP 5.3+, MySQL 5.1+
// MAN PAGE: http://php.net/manual/en/mysqli.construct.php
// MAN PAGE: http://php.net/manual/en/mysqli.real-escape-string.php
// MAN PAGE: http://php.net/manual/en/mysqli.query.php
// MAN PAGE: http://php.net/manual/en/mysqli.errno.php
// MAN PAGE: http://php.net/manual/en/mysqli.error.php
// MAN PAGE: http://php.net/manual/en/mysqli.insert-id.php
// MAN PAGE: http://php.net/manual/en/mysqli-result.num-rows.php
// MAN PAGE: http://php.net/manual/en/mysqli-result.fetch-array.php
// MAN PAGE: http://php.net/manual/en/mysqli-result.fetch-object.php

// DATABASE CONNECTION AND SELECTION VARIABLES - GET THESE FROM YOUR HOSTING COMPANY
$db_host = "localhost"; // PROBABLY THIS IS OK
$db_name = "??";
$db_user = "??";
$db_word = "??";

// OPEN A CONNECTION TO THE DATA BASE SERVER AND SELECT THE DB
$mysqli = new mysqli($db_host, $db_user, $db_word, $db_name);

// DID THE CONNECT/SELECT WORK OR FAIL?
if ($mysqli->connect_errno)
{
    $err
    = "CONNECT FAIL: "
    . $mysqli->connect_errno
    . ' '
    . $mysqli->connect_error
    ;
    trigger_error($err, E_USER_ERROR);
}

// CREATE AND SEND A SELECT QUERY AND TEST THE RESULTS
$fname    = "RAY";
$safe_fn  = $mysqli->real_escape_string($fname);

// CONSTRUCT THE QUERY USING THE ESCAPED VARIABLE
$sql = "SELECT id, lname FROM my_table WHERE fname='$safe_fn' ORDER BY lname, fname";
$res = $mysqli->query($sql);

// IF mysqli_query() RETURNS FALSE, LOG AND SHOW THE ERROR
if (!$res)
{
    $err
    = "QUERY FAIL: "
    . $sql
    . ' ERRNO: '
    . $mysqli->errno
    . ' ERROR: '
    . $mysqli->error
    ;
    trigger_error($err, E_USER_ERROR);
}
// IF WE GET THIS FAR, THE QUERY SUCCEEDED AND WE HAVE A RESULT OBJECT IN $res
// AND SO WE CAN NOW USE $res IN OTHER MYSQLI FUNCTIONS


// DETERMINE HOW MANY ROWS OF RESULTS WE GOT
$num     = $res->num_rows;
$num_fmt = number_format($num);
if (!$num)
{
    echo "<br/>QUERY: $sql ";
    echo "<br/>FOUND NO DATA ";
    echo PHP_EOL;
}
else
{
    echo "<br/>QUERY: $sql ";
    echo "<br/>FOUND $num_fmt ROWS OF DATA ";
    echo PHP_EOL;
}
echo PHP_EOL;

// ITERATE OVER THE RESULTS SET AS AN OBJECT TO SHOW WHAT WE FOUND
echo "USING MySQLi_Result::Fetch_<i>Object</i>(): ";
echo PHP_EOL;
while ($row = $res->fetch_object())
{
    // ROW BY ROW PROCESSING IS DONE HERE
    print_r($row);
    echo PHP_EOL;
}

Open in new window

0
 

Author Closing Comment

by:dynorich
ID: 39956758
Thanks
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question