lupld Posted June 22, 2007 Share Posted June 22, 2007 This is gonna sound stupid... but I am trying to simplify all my code, so I figured I better make sure I'm doing this right... I want to know how to set info from a mysql connection into a variable. I have this so far... mysql_connect($db_host,$db_user,$db_password) or die(mysql_error()); mysql_select_db($db_name) or die(mysql_error()); $query = "SELECT username, firstname, lastname, email, yim, aim, msn, icq, gender FROM users WHERE username='$req'"; $result = mysql_query($query); what do I need to call each field into its own variable? The only code I have calls it in a loop, and I just want to set it to a variable... thanks in advance... Quote Link to comment https://forums.phpfreaks.com/topic/56639-solved-simple-question/ Share on other sites More sharing options...
rempires Posted June 22, 2007 Share Posted June 22, 2007 I'm not quite sure what your asking, if you want to retrieve all that info and put it into variables names after the fields themselves you could do this while ($row=mysql_fetch_array($result,MYSQL_ASSOC)) { foreach ($row as $colname => $value) { ${$colname} = $value; } } if you wanted to pull out each individual you could do $username = $result[0]; $firstname = restult[1]; ect. Quote Link to comment https://forums.phpfreaks.com/topic/56639-solved-simple-question/#findComment-279724 Share on other sites More sharing options...
lupld Posted June 22, 2007 Author Share Posted June 22, 2007 thanks, I thought it was something like that... I finally got it to work the way I wanted with the loop, but I'll switch it to use the $result[0] format.... Quote Link to comment https://forums.phpfreaks.com/topic/56639-solved-simple-question/#findComment-279725 Share on other sites More sharing options...
teng84 Posted June 22, 2007 Share Posted June 22, 2007 I'm not quite sure what your asking, if you want to retrieve all that info and put it into variables names after the fields themselves you could do this while ($row=mysql_fetch_array($result,MYSQL_ASSOC)) { foreach ($row as $colname => $value) { ${$colname} = $value; } } if you wanted to pull out each individual you could do $username = $result[0]; $firstname = restult[1]; ect. Quote Link to comment https://forums.phpfreaks.com/topic/56639-solved-simple-question/#findComment-279726 Share on other sites More sharing options...
teng84 Posted June 22, 2007 Share Posted June 22, 2007 I'm not quite sure what your asking, if you want to retrieve all that info and put it into variables names after the fields themselves you could do this while ($row=mysql_fetch_array($result,MYSQL_ASSOC)) { foreach ($row as $colname => $value) { ${$colname} = $value; } } if you wanted to pull out each individual you could do $username = $result[0]; $firstname = restult[1]; ect. ^^^^ sure about that Quote Link to comment https://forums.phpfreaks.com/topic/56639-solved-simple-question/#findComment-279727 Share on other sites More sharing options...
lupld Posted June 22, 2007 Author Share Posted June 22, 2007 I dunno, I tried it a couple of times and switched back to a way I found while messing around... <?php $req = $_GET['req']; mysql_connect($db_host,$db_user,$db_password) or die(mysql_error()); mysql_select_db($db_name) or die(mysql_error()); $query = "SELECT username, firstname, lastname, email, yim, aim, msn, icq, gender FROM users WHERE username='$req'"; $result = mysql_query($query); $r = mysql_fetch_array($result) ?> <body> <div align="center"> <table> <tr><td colspan="2"><span style="font-size:36px; text-decoration:underline;">User Info</span></td></tr> <tr><td align="right" width="50%">Username:</td><td align="left" width="50%"><?php echo "$r[username]"; ?></td></tr> <tr><td align="right">First Name:</td><td align="left"><?php echo "$r[firstname]"; ?></td></tr> <tr><td align="right">Last Name:</td><td align="left"><?php echo "$r[lastname]"; ?></td></tr> <tr><td align="right">Email Adress:</td><td align="left"><?php echo "$r[email]"; ?></td></tr> <tr><td align="right">Yahoo:</td><td align="left"><?php echo "$r[yim]"; ?></td></tr> <tr><td align="right">AIM:</td><td align="left"><?php echo "$r[aim]"; ?></td></tr> <tr><td align="right">MSN:</td><td align="left"><?php echo "$r[msn]"; ?></td></tr> <tr><td align="right">ICQ:</td><td align="left"><?php echo "$r[icq]"; ?></td></tr> <tr><td align="right">Gender:</td><td align="left"><?php echo "$r[gender]"; ?></td></tr> </table> </div> I just removed the while() portion from one of my other codes and it worked... Quote Link to comment https://forums.phpfreaks.com/topic/56639-solved-simple-question/#findComment-279732 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.