Solved

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

Posted on 2014-03-26
6
527 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
Comment Utility
<?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
Comment Utility
you can do indeed:
$SESSION["xxxxx"] = $row[...];

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

Author Comment

by:dynorich
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

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

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

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
Thanks
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

772 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now