1

this is my code:

$scontain = "SELECT id FROM voting";
$qcontain = mysql_query($scontain);

$r_idarray = array();
while ($rcontain = mysql_fetch_array($qcontain))
{
    $r_idarray[] = $rcontain['id'];
    //let's say there are 5 names here//
}

echo $r_idarray;

I was trying to get the whole content of my table 'voting'. Inside the while loop it successfully prints out the whole content of the table which is under the column 'id' but when I tried to echo it outside the while loop it prints 'Array'? Can anyone knows how to solve this. Thank you in advance...

4
  • Can you rewrite your code without "enter code here" ? Too unreadable Commented Jan 31, 2013 at 7:33
  • Please rewrite your code clear so we can help you. Commented Jan 31, 2013 at 7:34
  • print_r recursively prints an array showing all keys and values. echo does not, and is intended for scalar values. Commented Jan 31, 2013 at 7:38
  • Another thing, my real objective was to use $r_idarray; how do I do that, do I still need to declare a variable and use print_r($r_idarray); like, $a = print_r($r_idarray); Thank you :) Commented Jan 31, 2013 at 7:53

6 Answers 6

1

To print the array you can do:

print_r($r_idarray);

instead of

echo $r_idarray;
Sign up to request clarification or add additional context in comments.

1 Comment

Another thing, my real objective was to use the values inside $r_idarray; to use it for comparison in my If else, how do I do that, do I still need to declare a variable and use print_r($r_idarray); like, $a = print_r($r_idarray); Thank you :)
0

You have to use print_r($r_idarray) .

echo is used for printing strings.

use print_r for printing arrays.

Comments

0

Use print_r or var_dump for printing the array

Replace:

echo $r_idarray;

With:

print_r($r_idarray);

Or:

var_dump($r_idarray);

Comments

0

If you want to display whole content

SELECT id FROM voting

To

SELECT * FROM voting

And after that you can use print_r($r_idarray);

Comments

0

You can't "echo" an array, you can only "echo" a string. Of course you will see 'Array'. In order to print out the contents of an array, you either have to iterate through it's contents, or do a "var_dump($array)".

while($val in $array){
  echo $val;
}

or:

print_r($array);

or:

var_dump($array);

Comments

0

One thing youe query should be like this to select all columns

$scontain = "SELECT * FROM voting";

You only have to do this

$i=0;
while ($rcontain = mysql_fetch_array($qcontain))
{
    $r_idarray[$i] = $rcontain;
    $i++;
}
echo '<pre>';
print_r($r_idarray);    

And if you need particular columns from result

$i=0;
while ($rcontain = mysql_fetch_array($qcontain))
{
    $r_idarray[$i]['id'] = $rcontain['id'];
    $r_idarray[$i]['column1'] = $rcontain['column1'];
    $r_idarray[$i]['column2'] = $rcontain['column2'];
    $r_idarray[$i]['column3'] = $rcontain['column3'];
    $i++;
}

echo '<pre>';
print_r($r_idarray);

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.