0

Is it possible to do something like this? I'm new to this so don't know!

//row 1 in mysql array
$var = "hello"; 
$id = 1; 

//row 2 in mysql array
$var = "bye";
$id = 2;

//Closing while loop

//Is the following possible?
echo $var1; //outputs "hello"
echo $var2; //outputs "bye"
8
  • 3
    I think you need to learn a bit more about php. Commented Oct 22, 2013 at 8:31
  • You have incorrect syntax at the end, it should be echo $var1; echo $var2; but still, since var1 and var2 haven't been defined, this isn't possible to do it like this. You could define var and id as arrays and then access them using var[0] var[1] etc. Commented Oct 22, 2013 at 8:32
  • The way you write your $echo means it's a variable the proper syntax is echo $yourvar/string. Commented Oct 22, 2013 at 8:32
  • 1
    Why will it be useful? Commented Oct 22, 2013 at 8:34
  • Woops my bad, just tired ^^ How do I save them in arrays? @JonathonHenderson. Commented Oct 22, 2013 at 8:34

4 Answers 4

3

Not like you want, you can use an array though:

$var[1] = "hello";
$var[2] = "bye";

echo $var[1];
echo $var[2];
Sign up to request clarification or add additional context in comments.

Comments

0

You mention MySQL. So a more complete example of what you want to achieve would be:

$var = array();
$id  = array();
while ($row = mysql_fetch_assoc($result)) {
    $var[] = $row['text']; // don't know how the column is named
    $id[]  = $row['id'];
}

echo $row[0]; // hello
echo $row[1]; // bye

With a looping variable i:

$var = array();
$i = 1;
while ($row = mysql_fetch_assoc($result)) {
    $var[$i] = $row['text']; // don't know how the column is named
    $i++;
}

echo $row[1]; // hello
echo $row[2]; // bye

4 Comments

Lets say i dont have the id in mysql. just like id = 0 and in while loop id++; How do I put it in array then? @Reeno
Even better: $var[$row['id']] = $row['text']; then he can look up $var[$id]
@PeterB if you do: $var[] = $row['text']; it will automatically set the index. First element will be in $var[0], the second in $var[1] etcetera
@PeterP I added an example with the variable i. But also works as Jeroen described (my first example)
0

I would do so

$id = 1; 
$var[$id] = "hello";
$id = 2;
$var[$id] = "bye";
echo $var['1']; 
echo $var['2']; 
// or
echo $var[$id];

Comments

0
// Define them both as arrays
$var = array();
$id = array();

// In your loop
$var[] = "hello";
$id[] = 1;

// Next iteration
$var[] = "bye";
$id[] = 2;

// Printing them out
echo $var[0]; // prints out "hello"
echo $var[1]; // prints out "bye"

Using arrays^^

Alternatively:

$rows = array();

// In your loop
$rows[] = array("id" => 1, "var" => "hello");

// Next iteration
$rows[] = array("id" => 2, "var" => "bye");

// Print them out
echo $rows[0]["var"]; // Prints hello
echo $rows[1]["var"]; // Prints bye

Comments

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.