Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 582
  • Last Modified:

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

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
dynorich
Asked:
dynorich
  • 4
1 Solution
 
dynorichAuthor Commented:
<?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
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
you can do indeed:
$SESSION["xxxxx"] = $row[...];

and later in the code use the $SESSION array.
0
 
dynorichAuthor Commented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
dynorichAuthor Commented:
Maybe the problem is in this code:
<?php session_start();

$_SESSION['Fullname'] = $row[0];
$_SESSION['Email'] = $row[1];
$_SESSION['SponsorsPhone'] = $row[2];
?>
0
 
Ray PaseurCommented:
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
 
dynorichAuthor Commented:
Thanks
0

Featured Post

Industry Leaders: 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!

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