Jump to content

Recommended Posts

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...

Link to comment
https://forums.phpfreaks.com/topic/56639-solved-simple-question/
Share on other sites

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.

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.

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

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...

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.