How to convert MySql data into variables?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mcc_shane
Forum Newbie
Posts: 22
Joined: Sat May 12, 2012 1:47 pm

How to convert MySql data into variables?

Post by mcc_shane »

Hi Everyone,

I'm trying to convert my MySql data into variables so I can use them on my website. For example, if I want to convert a city or even first or last name into a variable. How would I go about doing that? Thanks everyone!
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: How to convert MySql data into variables?

Post by social_experiment »

Depending on how you retrieve the results (mysql_fetch_object, mysql_fetch_array, mysql_fetch_row) you can use that to access the values.

Code: Select all

<?php
 // 
 $obj = mysql_fetch_object($sql);

 echo $obj->field1; // $obj->field1 contains data present in 'field1' of your table
 // etc.
?>
If you want to create and use (other) variables you can equate the above values like this -

Code: Select all

<?php
 $field1 = $obj->field1;

 echo $field1;
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply