SirChick Posted August 10, 2007 Share Posted August 10, 2007 How do you obtain fields from a table and assign each field to a variable to be used as needed. An example: Say i had: table name : user Field 1 : User name Field 2: Password then i wanted: $username = field 1 $password = field 2. I know how to do the opposite whereby you input to a table from a variable but not the otherway round lol Quote Link to comment https://forums.phpfreaks.com/topic/64249-obtaining-info-from-database-table/ Share on other sites More sharing options...
MadTechie Posted August 10, 2007 Share Posted August 10, 2007 like so.. $conn = mysql_connect("localhost", "mysql_user", "mysql_password"); //error handle if (!$conn) { echo "Unable to connect to DB: " . mysql_error(); exit; } //error handle if (!mysql_select_db("mydbname")) { echo "Unable to select mydbname: " . mysql_error(); exit; } $sql = "SELECT id as userid, fullname, userstatus FROM sometable WHERE userstatus = 1"; $result = mysql_query($sql); $row = mysql_fetch_assoc($result)); $uid = $row["userid"]; $name = $row["fullname"]; $status = $row["userstatus"]; Quote Link to comment https://forums.phpfreaks.com/topic/64249-obtaining-info-from-database-table/#findComment-320277 Share on other sites More sharing options...
SirChick Posted August 10, 2007 Author Share Posted August 10, 2007 how does that know what table to look at? and whats "WHERE userstatus = 1";" do ? Quote Link to comment https://forums.phpfreaks.com/topic/64249-obtaining-info-from-database-table/#findComment-320286 Share on other sites More sharing options...
MadTechie Posted August 10, 2007 Share Posted August 10, 2007 if (!mysql_select_db("mydbname")) { // <---Tabe Selection "WHERE userstatus = 1"; is just part of the SQL statement just finds the records where field 'userstatus' = 1 Quote Link to comment https://forums.phpfreaks.com/topic/64249-obtaining-info-from-database-table/#findComment-320332 Share on other sites More sharing options...
wildteen88 Posted August 10, 2007 Share Posted August 10, 2007 if (!mysql_select_db("mydbname")) { // <---Tabe Selection No thats database selection. YOu select the table within your SQL Query: $sql = "SELECT id as userid, fullname, userstatus FROM sometable WHERE userstatus = 1"; sometable is the table mysql query to fetch the userid, fullname and userstatus where userstatus equals to 1. Quote Link to comment https://forums.phpfreaks.com/topic/64249-obtaining-info-from-database-table/#findComment-320353 Share on other sites More sharing options...
MadTechie Posted August 10, 2007 Share Posted August 10, 2007 my bad, misread, table as database.. thanks for the correction wildteen88, i'll go dig that hole now Quote Link to comment https://forums.phpfreaks.com/topic/64249-obtaining-info-from-database-table/#findComment-320477 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.