Solved

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

Posted on 2014-03-26
6
538 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
  • 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 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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 109

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

777 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