Jump to content

[SOLVED] Taking info from a query and making it a variable


Recommended Posts

I have this code:

<?php
// Make a MySQL Connection
mysql_connect("localhost", "___", "___") or die(mysql_error());
mysql_select_db("____") or die(mysql_error());

// Retrieve all the data from the "TECH" table
$result = mysql_query("SELECT * FROM tech WHERE ID='".$_POST['tech']."'") or die(mysql_error());  


// Print out the contents of the entry 

while($row = mysql_fetch_array($result)){
echo $row['NAME']. " - ". $row['ID']. " - ". $row['ALLOW']. " - ". $row['TEAM']. " - ". $row['LOCA'];
echo "<br />";
}
?>
<html>
Hello <?php echo $name; ?>!  Your tech id is <?php echo $id; ?>.  Your allowance is <?php echo $allow; ?>.  Your team is <?php echo $team; ?>.  Your location is <?php echo $loca; ?>.
</html>

 

I want to be able to call NAME, ID, ALLOW, TEAM, and LOCA later in the php page.  How can I turn those into a variable to be called later in the php document?  I tried $row['ALLOW'] but no dice.  Thanks for any tips!

Hi SEVIZ

 

The row variable is only local, you will need to create global variables, outside of the while loop. Example.

 

$id=0;

$name="";

 

while($row = mysql_fetch_array($result)){

 

  $name = $row['NAME'];

  $id =  $row['ID'];

 

}

 

print $name;

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.