Ju-Pao Posted March 8, 2007 Share Posted March 8, 2007 How can I retrieve a data from my mySQL database using PHP and save the data in a variable in in my PHP code for example i have this table in the database First name Last Name Juan Punkista I would like to store 'Juan' in variable $first and 'Punkista' in variable $last thank you for your notice Link to comment https://forums.phpfreaks.com/topic/41752-php-and-mysql/ Share on other sites More sharing options...
mmarif4u Posted March 8, 2007 Share Posted March 8, 2007 How can I retrieve a data from my mySQL database using PHP and save the data in a variable in in my PHP code for example i have this table in the database First name Last Name Juan Punkista I would like to store 'Juan' in variable $first and 'Punkista' in variable $last thank you for your notice Do like this: After ur query write this, $first= $row-> First Name $last= $row-> Last Name Link to comment https://forums.phpfreaks.com/topic/41752-php-and-mysql/#findComment-202393 Share on other sites More sharing options...
Wildhalf Posted March 8, 2007 Share Posted March 8, 2007 Hi, You will have to change the follow with reference to your Database 1. ocalhost 2. DB_User 3. password 4. DB You will have to change the follow with reference to your Table 1. table_name 2. first_name 3. last_name Try this.... # Create DB connection $dbhost = 'localhost'; $dbusername = 'DB_User'; $dbpasswd = 'password'; $database_name = 'DB'; #under here, don't touch! $connection = mysql_pconnect("$dbhost","$dbusername","$dbpasswd") or die ("Couldn't connect to server."); $db = mysql_select_db("$database_name", $connection) or die("Couldn't select database."); /* Let's get the user info */ $sql = mysql_query("SELECT * FROM table_name"); $data = mysql_fetch_array($sql); $first = $data[first_name]; $last = $data[last_name]; Link to comment https://forums.phpfreaks.com/topic/41752-php-and-mysql/#findComment-202394 Share on other sites More sharing options...
Gruzin Posted March 8, 2007 Share Posted March 8, 2007 First of all u have to connect to your mysql database using MYSQL_CONNECT, then you can retrive info from db. For example: $sql = mysql_query("SELECT * FROM tablename"); while($row = mysql_fetch_array($sql)){ $first = $row['name']; $last = $row['lastname']; } Link to comment https://forums.phpfreaks.com/topic/41752-php-and-mysql/#findComment-202396 Share on other sites More sharing options...
Ju-Pao Posted March 8, 2007 Author Share Posted March 8, 2007 thanks for the reply guys Link to comment https://forums.phpfreaks.com/topic/41752-php-and-mysql/#findComment-202398 Share on other sites More sharing options...
Ju-Pao Posted March 8, 2007 Author Share Posted March 8, 2007 follow up question, is there a way where I can select the latest row that was added in mySQL? Link to comment https://forums.phpfreaks.com/topic/41752-php-and-mysql/#findComment-202405 Share on other sites More sharing options...
Wildhalf Posted March 8, 2007 Share Posted March 8, 2007 Best way to do that would have a unique key in your database that auto increaments then qeury the database for the max number Link to comment https://forums.phpfreaks.com/topic/41752-php-and-mysql/#findComment-202407 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.