Page 1 of 1

Session variables.

Posted: Fri Jul 22, 2011 10:56 am
by darrenorient
Hi, learning as I go at the moment but getting unstuck.

I understand how to create a variable on a page and display it. The following sets the variable to 1:

Code: Select all

<?php
$current_game = 1;
?>
and then this displays it on the page

Code: Select all

<p>Current game: <?php echo $current_game ?></p>
That's nice and easy, but I need to be able to set the variable from an outside source, i.e my database. I'm using Dreamweaver and I thought having created a recordset with the record I want to pull in I would be able to use this code:

Code: Select all

<?php
$dcurrent_game = $row_Current_game['current_game'];
?>
But it just gives me an undefined variables error, so clearly I'm missing something.

Can some help me understand what I need to do please to bring in a piece of data from the database, set this is the variable and then display this (or use it in an if statement, but I'll come on to that) within the page.

Many thanks,
Darren

Re: Session variables.

Posted: Fri Jul 22, 2011 12:22 pm
by social_experiment
It depends on the type of database that you are using. This example uses MySQL as its basis:

Code: Select all

<?php
 // connect database, etc.

 // select data using a simple query
 $query = "SELECT field1, field2 FROM table";
 $sql = mysql_query($query);
 // using a mysql function retrieve the information
 while ($array = mysql_fetch_array($sql))
 {
  // assign values to variables
  $field1 = $array['field1'];
  $field2 = $array['field2'];
 }
?>
The values contained in $field1 and $field2 depends on what your query is, if you select a single record you can assign the values then display them. If you select multiple records, you would display the records within the while statement. Look at other data retrieving mysql functions such as mysql_fetch_object(), mysql_fetch_assoc(), mysql_fetch_row()