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...
To print the array you can do:
print_r($r_idarray);
instead of
echo $r_idarray;
You have to use print_r($r_idarray) .
echo is used for printing strings.
use print_r for printing arrays.
Use print_r or var_dump for printing the array
Replace:
echo $r_idarray;
With:
print_r($r_idarray);
Or:
var_dump($r_idarray);
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);
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);
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);
Related
Been lurking on Stackoverflow for a long time but this is my first post. I am receiving a error related to displaying an array which is supposed to be populated by a mysql query. The echo function just returns ArrayArrayArray instead of what is supposed to be there. The mysql query is comparing a form input (the variable $data) .
<?php
$data = $_POST["search"];
global $data;
// Create Connection
$con = mysqli_connect(xxxxx,xxxxxx,xxxxx,xxxxx);
// Check Connection
if (mysqli_errno($con))
{
echo "Failed To Connect To The Database" ;
}
//Perform Query To Compare And Return Results
$result_array = array();
$query = " SELECT url FROM data WHERE url LIKE '%$data%' " ;
$result = mysqli_query($con, $query);
// While Loop To Return All Comparable Results
while ($row = mysqli_fetch_array($result)) {
$result_array[] = $row['url'];
echo $result_array ;
}
?>
echo will print a string, try using something like print_r() or var_dump() instead
Example
echo '<pre>';
print_r($result_array);
echo '</pre>';
<pre> will allow for easier reading of the array
You can try with following code.
<?php echo '<pre>'; print_r($result_array); echo '</pre>'; ?>
In your code, you want to replace echo $result_array; by echo $row['url']; (to display the content of url at each loop), or remove that line and add a print_r($result_array); after the while{} loop, to display all in one command.
It's also useful use
echo implode(';', $result_array);
for joining strings of each element in $result_array
echo posts the string version of your variable, which in this case will look like Array.
You can use var_dump($result_array); or print_r($result_array); to get the results printed correctly.
I am having an issue with how I am converting my php array into a JSON object. No matter what I try, I either print everything out as multiple objects or it comes out as null.Wrapping it in pre tags, here is the closest that I got it:
My code:
$content = mysqli_query($dbcon,
"SELECT title, last_name AS lastname
FROM revision, field_last_name
WHERE vid = entity_id;"
);
echo "<pre>";
while($row = mysqli_fetch_array($content))
{
print json_encode($row);
print '<br/>';
}
echo "</pre>";
My output:
{"0":"John Apple","title":"John Apple","1":"Apple","lastname":"Apple"}
{"0":"Kumar Patel","title":"Kumar Patel","1":"Patel","lastname":"Patel"}
{"0":"Michaela Quinn","title":"Michaela Quinn","1":"Quinn","lastname":"Quinn"}
{"0":"Peyton Manning","title":"Peyton Manning, MD","1":"Manning","lastname":"Manning"}
{"0":"John Doe","title":"John Doe","1":"Doe","lastname":"Doe"}
{"0":"Jane Lee","title":"Jane Lee","1":"Lee","lastname":"Lee"}
{"0":"Dan McMan","title":"Dan McMan","1":"McMan","lastname":"McMan"}
{"0":"Yu Win","title":"Yu Win","1":"Win","lastname":"Win"}
My two questions are:
1) Why is there a "0":"John Apple" and a "1":"Apple" when all I want is "title":"John Apple" and "lastname":"Apple" in my object?
2) Why is everything displaying as multiple objects?
Thanks!
---EDIT---
$arr = array()
echo "<pre>";
while($row = mysqli_fetch_assoc($content))
{
$arr[] = $row;
}
print $arr;
echo "</pre>";
field_last_name is your table name? can you distinguish each column name prefix by table name like revision.title in your query and get all data in a single array and then json_encode it?
$content = mysqli_query($dbcon,
"SELECT title, last_name AS lastname
FROM revision, field_last_name
WHERE vid = entity_id;"
);
$arr = array();
echo "<pre>";
while($row = mysqli_fetch_assoc($content))
{
$arr[] = $row;
}
print_r(json_encode($arr));
echo "</pre>";
Change this:
while($row = mysqli_fetch_array($content))
{
print json_encode($row);
print '<br/>';
}
To this:
$row = mysqli_fetch_assoc($content);
json_encode($row);
...because you're printing out multiple objects. If you want a single object which is an array, you need to append the results of mysql_fetch_assoc (see other answer covering field names vs positions) to an array, then json_encode the array in one shot. Example:
$myarray = array();
while($row = mysqli_fetch_assoc($content))
{
$myarray[] = $row;
print '<br/>';
}
print json_encode($myarray);
I have one query where the output processing looks like this:
while($row = mysql_fetch_array($result)){
echo $row['username'] . " " . $row['earning'];
}
And outputs result is like this:
JOHN 200
JOHN 350
NEO 100
NEO 220
What I want to achieve is that every name appears once, and "earning" is sum of all earnings of that name, like this:
JOHN 550
NEO 320
I have to mention that I CANNOT change the query; that is biggest problem.
Is there any hope? Some suggestions? :)
You can sum the values in the loop to another array and then output it.
Try:
$earnings = array();
while($row = mysql_fetch_array($result)) {
if (!isset($earnings[$row['username']])) $earnings[$row['username']] = 0;
$earnings[$row['username']] += $row['earning'];
}
foreach($earnings as $user => $earnings) {
echo "$user $earnings<br />\n";
}
try:
$user = array();
while($row = mysql_fetch_array($result)){
$user[$row['username']] += $row['earning'];
}
to do echo :
foreach($user as $key => $value) {
echo $key."=". $earnings."<br>";
}
To get a quick solution to this answer you may want to simply append these results to an associated array and then simply loop over it to get the final count.
So, something like this:
$names= array();
while($row = mysql_fetch_array($result)){
$names[$row["username"]] += $row["earning"];
}
foreach($names as $k => $v) {
echo $k." ".$v."\n";
}
Instead of just selecting the data, select the field and SUM(otherfield) with otherfield being the field with the number to be summed. Then add GROUP BY with the first field.
try this in mysql side you don't need to calculate in php side so don't change your PHP side code jst change query like this
SELECT
username,
SUM(earning)
FROM yourtable
GROUP BY (username)
hope its will work for you
Hey all i have a mind bug with this wile loop , i am a bit of a noob
$sql_advanced_bio = mysql_query("SELECT * FROM bio_details WHERE mem_id='$id'");
while($row = mysql_fetch_assoc($sql_advanced_bio)){
$bio_time_family = $row["bio_time_family"];
}
So this is the php now in my HTML i have this
*php echo $bio_time_family *
Why dose it echo only one value ?
SIMPLE ANSWER
so i tried somethingh easier like this $bio_fun_family .= $row["bio_fun_family"].",";
and then this
$bio_fun_family = substr($bio_fun_family,0,-1);
I added the .next to the =
It appends the values
You need to store the values in an array, otherwise you're just overwriting one variable each time, so you'll just end up with the last value.
You also need to use a loop to echo out each value in the array.
$sql_advanced_bio = mysql_query("SELECT * FROM bio_details WHERE mem_id='$id'");
$bio_time_family = array();
while($row = mysql_fetch_assoc($sql_advanced_bio)) {
$bio_time_family[] = $row["bio_time_family"];
}
And then for the output;
foreach($bio_time_family as $b) {
echo($b . '<br />');
}
With each iteration of the while loop, you're assigning to $bio_time_family the newly extracted value. If you'd like to make an array of the, instead, use $bio_time_family[] = $row["bio_time_family"];
Because every time you run through the loop you're re-assigning $bio_time_family to the next row's value. Try calling echo directly in your loop or add the row to an array, then loop through that and output the rows
when I attempt to echo these names, nothing comes up...
//
$getuser = "SELECT * FROM user WHERE account_id = $id";
$showuser = #mysqli_query ($dbc, $getuser); // Run the query.
while ($row = mysqli_fetch_array($showuser, MYSQLI_ASSOC)) {
$names[]=$row['user_username'];
}
echo $names;
To Echo an Array use
print_r($names)
Echo will only print out a simple variable (Text, Number). print_r is used to format and print out complex types such as arrays and objects.
Further information can be found on the php.net website.
http://php.net/manual/en/function.print-r.php
You can use print_r() or var_dump()
Instead of echo use print_r.
print_r($names);
Like that.
you should initialize $names as array before the while loop $names = array();