How to make echo a PHP array - php

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();

Related

PHP echo returning the word 'Array'

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.

How to print only index of an array

Using select query am select some data from database.
i fetched data using while loop.
while($row=mysql_fetch_array($query1))
{
}
now i want to print only index of $row.
i tried to print using following statements but it prints index and value(Key==>Value)
foreach ($row as $key => $value) {
echo $key ;
}
and i tried array_keys() also bt it is also not helpful to me.
echo implode(array_keys($row));
please help to get out this.
i need to print only index.
You are fetching the results row as both associative array and a numeric array (the default), see the manual on mysql_fetch_array.
If you need just the numeric array, use:
while($row=mysql_fetch_array($query1, MYSQL_NUM))
By the way, you should switch to PDO or mysqli as the mysql_* functions are deprecated.
You should pass separator(glue text) in Implode function.
For comma separated array keys, you can use below code.
echo implode(",",array_keys($row));
The $row variable in your while loop gets overwritten on each iteration, so the foreach won't work as you expect it to.
Store each $row in an array, like so:
$arr = array();
while($row=mysql_fetch_array($query1)) {
$arr[] = $row;
}
Now, to print the array keys, you can use a simple implode():
echo implode(', ', array_keys($arr));
$query1 from while($row=mysql_fetch_array($query1)) should be the result from
$query1 = mysql_result("SELECT * FROM table");
//then
while($row=mysql_fetch_array($query1))
To get only the keys use mysql_fetch_row
$query = "SELECT fields FROM table";
$result = mysql_query($query);
while ($row = mysql_fetch_row($result)) {
print_r(array_keys($row));
}

Passing the values of mysql_fetch_array to an Array()?

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);

My fetch array is not working

I'm trying to pull an array to use on another query but it's not working, because the last comma.
<?php
include"connection.php";
$pos = mysqli_query($not,"SELECT * FROM equipos");
$logos = array();
while($row= mysqli_fetch_assoc($pos)){
$logos[] = "<br>'".$row['abrv']."'=>"."'".$row['logo']."'";
}
$logos = implode(",", $logos);
$enjuego = mysqli_query($not,"SELECT * FROM partidos WHERE dprt='ftbls'");
while($part=mysqli_fetch_array($enjuego)){
$liga=$part['serie'];
$eq1= $part['eq1'];
$eq1s= strtoupper($eq1);
$eq2= $part['eq2'];
$eq2s= strtoupper($eq2);
echo $logos[$eq1].'<br>';
}
?>
It gives me the same error over and over again.
This is the closest I came but just doesn’t work.
Can someone tell me what am I doing wrong?
The error I get is: Warning: Illegal string offset 'gua' in line 22
You have several problems with your code:
You never created an array
You constantly overwrite $logos
Your usage of substr_replace() indicates a deeper problem.
Here's a better approach:
Build the array.
$logos = array();
while($row= mysqli_fetch_assoc($pos)){
$logos[] = "<br>'".$row['abrv']."'=>"."'".$row['logo']."'";
}
There are many ways condense an array into a string. I encourage you to browse the manual on PHP Array Functions. In your case, you are interested in implode()
$logos = implode(",", $logos);
Note: The value for $logos smells. You should construct your arrays to hold data, not formatting.
For example:
$logos[$row['abrv']] = $row['logo'];
Output:
print_r($logos);
What you want can be achieved in many ways, but let's look at your code, there are quite a few things wrong in there.
Semantically meaningless variable names like pos, not, equipos, abrv. Only logo and result are good variable names.
Using the * selector in database queries. Don't do that, instead select the exact fields you need, it's better for performance, maintainability, code readability, testability, ... need I say more?
Fetching per row and running code on each row when what you actually want is an array containing all rows. Solution:
$result = mysqli_query($not, "SELECT * FROM equipos");
$logos = mysqli_fetch_all($result, MYSQLI_ASSOC);
Concatenating subarrays by using strings, that's not how it works, what you could do:
while($row = mysqli_fetch_assoc($result))
{
$logos[][$row['abrv']] = $row['logo'];
}
But as I said, that's not necessary.
Overwriting your variable $logos with each iteration of the loop. You'd need to do $logos[] = ....
Typically, implode is useful for such cases.
Without knowing what exactly you want to do, take this as a first hint:
$logos = array();
while($row= mysqli_fetch_assoc($pos)){
$logos[] = "<br>'".$row['abrv']."'=>"."'".$row['logo']."'";
}
$logos = implode(",", $logos);
The simplest way I would have thought is just to change the query and fetch the array into that -
<?php
include_once("connection.php");
$result = mysqli_query($not,"SELECT abrv, logo FROM equipos");
$rows = mysqli_fetch_array($result, MYSQLI_ASSOC);
// display the result
echo "<pre>"
print_r($rows);
echo "</pre>"
?>

how to get a data from a DB using MYSQL and store it in javascript Array

Is there any possible way to get a data from a DB using MYSQL and store it in javascript Array?
Fetch it as an associative array, and then use json_encode to create a JavaScript array, stored in a string.
.
// first, build your query:
$sql = "SELECT name, email FROM users";
$result = mysql_query($sql);
// then build up your data
$rows = array();
while ($row = mysql_fetch_assoc($result)) {
$rows[] = $row;
}
//then write it in a way Javascript can understand:
echo "<script type=\"text/javascript\">\n"
. "var users = " . json_encode($rows) . ";\n"
. "</script>";
<?php
$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);
// of course this array can be created by looping through your mysql result set
// and doing a mysql_fetch_assoc
// for example, $sql = your query here
// mysql_fetch_assoc($result); etc
echo json_encode($arr);
?>
{"a":1,"b":2,"c":3,"d":4,"e":5}
Then you can do something like
<script type="text/javsacript">
var abc = "<? echo json_encode($arr);?>";
</script>
OR
echo '<script type="text/javsacript">
var abc ="'.json_encode($arr).'";
</script>';
Actually this is a pretty vague question, but I think AJAX is what you're looking
for.
EDIT: Of course JSON will workout too and might even be more straight forward...

Categories