Jump to content

Data variables shortcut


master82

Recommended Posts

Here is a sample of code I use to get data from my users table, as you can see its very long and in some other tables there are alot more fields - so it very time consuming

[code]

include("connectionfile.php");
$values = "SELECT field, field1, field2, field3, field4, field5, field6, field7, field8, field9 FROM users WHERE field1 = '$anothervalue';
$output = mysql_query($values,$connection) or die("Unable to retrieve user data");
while ($newArray = mysql_fetch_array($output)){
$field1= $newArray[''field1];
$field2= $newArray['field2'];
$field3= $newArray['field3'];
$field4= $newArray['field4'];
$field5= $newArray['lfield5'];
$field6= $newArray['field6'];
$field7= $newArray['field7'];
$field8= $newArray['field8'];
$field9= $newArray['field9'];
}

[/code]

when I require one of these fields I simply

[code]

echo"$field1";

[/code]

It works fine but I've seen it done a shorter way, but couldnt get it to work:

[code]

$values=mysql_query("SELECT * FROM users WHERE field1 = $anothervalue");
while($us=mysql_fetch_array($values)) {
}

[/code]

I thought I could then use the following to call the data:

[code]

echo"$us[field1]";

[/code]

...but the values came out blank :-[

Any idea where im going wrong or a different way to do this?
Link to comment
Share on other sites

NOT tested but this should work or a less i have made a stupid litle mistake somewhere...
[code]<?php $values=mysql_query("SELECT * FROM users WHERE field1 = $anothervalue");
$us=mysql_fetch_array($values);
$i=1;
$ii='field1';
while ($us[$ii] !== '') {
$i++;
$$ii=$us[$ii];
$ii='field'.$i;
}
?>
[/code]


notice how i didnt use a while for the data being pulled out.. this is because you only have 1 line of data in your database else if you have more then you will have to put everything into an array like below..

[code]<?php $values=mysql_query("SELECT * FROM users WHERE field1 = $anothervalue");
while ($us=mysql_fetch_array($values)) {
$i=1;
$ii='field1';
while ($us[$ii] !== '') {
  $i++;
  $$ii[]=$us[$ii];
  $ii='field'.$i;
}
}
?>
[/code]


then you can call each filed1,2,3,4.... by using $file1[0] which will pull out the data of field 1 row 1...


Regards
Liam
Link to comment
Share on other sites

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.