1

Hello Ihave some question. first I have to say that my english isnt so good. Sorry for it.

my problem is:

In my PHP script are some Vars like

var $_email = 'no';
var $_fname = 'no';
var $_lname = 'no';

etc.

The vars will init if the user visit my page. but now I want to init the vars autom. from a sql column array... like:

$query = "SHOW COLUMNS FROM user";
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)) {
   $column[] = $row["Field"];
}

foreach($column as $value)
{
var $value = 'no';
}

if somebody understand what i mean please help me..

1
  • the query i made is ` $query = "SHOW COLUMNS FROM user"; $result = mysql_query($query); while ($row = mysql_fetch_assoc($result)) { $column[] = $row["Field"]; }` Commented Mar 8, 2012 at 15:07

2 Answers 2

1

You can go with a key/value approach. When you query the database, store the values in an associative array.

eg. if you ran:

SELECT email, fName, lName FROM ...

then you would save them as:

someArray['email'] = $result['email'];
someArray['fName'] = $result['fName'];
someArray['lName'] = $result['lName'];

Or something similar.

Sign up to request clarification or add additional context in comments.

1 Comment

I get the columns like... $query = "SHOW COLUMNS FROM user"; $result = mysql_query($query); while ($row = mysql_fetch_assoc($result)) { $column[] = $row["Field"]; and every column be a var how containing "no". }
0

I guess you want to use something called Variable variables (sounds strange, I know).

Here's an example:

$column = array('_email', '_fname', '_lname');

foreach($column as $value)
{
    $$value = 'no';
}

echo $_email . '<br>';
echo $_fname . '<br>';
echo $_lname . '<br>';

BUT bare in mind that I don't recommend this. Using associative arrays is a much better approach (ashes999's response is the right choice to go).

Here's the same example with an associative array:

$column = array('_email', '_fname', '_lname');

foreach($column as $value)
{
    $arrValues[$value] = 'no';
}

echo $arrValues['_email'] . '<br>';
echo $arrValues['_fname'] . '<br>';
echo $arrValues['_lname'] . '<br>';

One variable ($arrValues) - everything in one place.

Arrays can be very powerful if you learn how to use them. And PHP has the luxury of having associative arrays.

Also, when declaring variables in PHP, there's no need for the keyword var. That's in other languages, like JavaScript for example.

I hope this helped.

2 Comments

Hmm, downflagged. Although, I stated that variable variables aren't the way to go. It's just an approach, an interesting feature of PHP that other languages don't have.
And I meant it about having the luxury of associative arrays. Trying to do tasks that involve arrays, in Delphi 7 for example (which doesn't know associative arrays) can be very painful. 3 lines in PHP code can become 20 in Delphi equivalent.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.