Problems with array error - php

Hello stackoverflow people, can you help me with this error,
Warning: array_combine() expects parameter 2 to be array, null given
in
$check = mysql_query("SELECT user_task_types, user_task_types_id FROM dotp_user_task_type WHERE user_id = '$user_id'");
$row[] = mysql_fetch_array($check);
$check = array_combine($row[0], $row[1]);
Cant understand what is wrong with it. Ive tried doing [] this so it would be array but still nothng. My array looks like this:
Array
(
[0] => TVS darbai
[user_task_types] => TVS darbai
[1] => 14
[user_task_types_id] => 14
)
I want to connect user task type id with user task types it would look like [14] => TVS darbai

$row[] = appends to an array. Your array now looks like:
array(
0 => array(...)
)
As you see, there's no [1]. You probably just want $row = mysql_fetch_array(..), then $row[0] corresponds to the first column and row[1] to the second. It would still not make sense to array_combine those two columns though.
You probably want this:
$result = mysql_query(...);
$check = array();
while ($row = mysql_fetch_array($result)) {
$check[$row[0]] = $row[1];
}

You no need to combine. $row gives your output
$check = mysql_query("SELECT user_task_types, user_task_types_id FROM dotp_user_task_type WHERE user_id = '$user_id'");
while ($row = mysql_fetch_array($check) )
{
echo $row;
}

Related

Make Associative array with three element in array

I want three element in associative array, so far am successful in getting two in the array.
$sql = "SELECT * FROM `notification_table` ";
$resultsd1 = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($resultsd1);
$associativeArray = array();
while ($row = mysqli_fetch_assoc($resultsd1))
{
$associativeArray[$row['name']] = $row['price'] ;
}
foreach($associativeArray as $k => $id){
echo $k."=>".$id .' ';
}
And am getting the response like this
name1=>24.725 name2=>24.265
Now i want to add another column in array as well and the name is column is notification_check .
Am not able to get how to add three columns in a single array. Any help will be appreciated.
I want the output like name1=>24.725=>yes_notification name2=>25.43=>no_notification
And when i print_r($row) is show this output Array ( [sno] => 1 [name] => name1 [price] => 23 [notification_check] => yes_notification)
You could shorten this and use mysqli_fetch_all to create an array of all of the data and then manipulate the array using array_column to create the index...
$result = mysqli_fetch_all($resultsd1, MYSQLI_ASSOC);
$associativeArray = array_column($result, null, 'name');

How to return an entire column as an array using PHP

I am trying to fetch a column from a database and display the entire contents as an array. I have so far been able to fetch only one of the entries from the table. I understand that I am fetching only $row[0] which is why i am getting only 1 element. I have tried to fetch all elements but I have not succeeded yet. can someone please let me know how to do it correctly? I have attached my code below.
<?php
$con=mysqli_connect("localhost","root","raspberry","users");
if (mysqli_connect_errno())
{
echo "failled to connect:".mysqli_connect_error();
}
$sql = "SELECT `current` FROM `monitor` ORDER BY `Sno.`";
$result = mysqli_query($con,$sql);
$row = mysqli_fetch_array($result,MYSQLI_NUM);
printf("%s\n",$row[0]);
mysqli_close($con)
?>
PDO has a dedicated feature for your problem PDOStatement::fetchColumn.
So, from your question what I understand is- you want to fetch a database column and store this in a array which will make you to display the results.
<?php
$con=mysqli_connect("localhost","root","raspberry","users");
if (mysqli_connect_errno())
{
echo "failled to connect:".mysqli_connect_error();
}
$sql = "SELECT current FROM monitor ORDER BY Sno";
$result = mysqli_query($con,$sql);
$arr = array();
while ($row = mysqli_fetch_array($result)) {
$arr2 = array();
foreach ($row as $val) $arr2[] = $val;
$arr[] = $arr2;
}
$first_col = array_column($arr, 0);
print_r($first_col);
mysqli_close($con)
?>
i.e. if you have five entry to the database whose first column contains
the values like 'current1', 'current2', 'current3', 'current3' and 'current4' the result of this code will be
Array
(
[0] => current1
[1] => current2
[2] => current3
[3] => current4
[4] => current5
)

add values into multidimensional array

I have a foreach loop that goes through a list of items. For each of these items, I have a while loop that grabs data out of a database.
$output = array();
//$reference is a multidimensional array has been passed to this page
where the element `color` contains the color I want.
foreach ($reference as $c) {
$color = $c['color'];
$query = "SELECT DISTINCT name FROM $table where colorPreference = $color";
$exquery = mysqli_query($con, $customerQuery);
while ($row = mysqli_fetch_array($exquery)) {
$person = $row['person'];
array_push($output[$color], $person);
}
}
So this loops through, the first time searching 'red', and finding 5 people in the fake table who like red. Next, 'blue', where it finds 1 person, and then 'green' where it finds 3.
If I look at the individual results, my first array has "red, blue, green" and my second array has these lists of names.... I just don't know how to add them into an array together.
I'm trying to build an array like this:
Array
(
[Red] => Array
(
[0] => John
[1] => Sally
[2] => Bob
...
)
[Blue] => Array
(
[0] => Luke
)
[Green] => Array
(
..etc...
)
I'm not using array_push correctly though - I'm getting an Warning: Illegal offset type error. What am I doing wrong?
It's been a while since I've worked with PHP, but I think you need to initialize each "color" array that you're going to push into. So...
$output = array();
//$reference is a multidimentional array has been passed to this page
where the element `color` contains the color I want.
foreach ($reference as $c) {
$color = $c['color'];
$query = "SELECT DISTINCT name FROM $table where colorPreference = $color";
$exquery = mysqli_query($con, $customerQuery);
while ($row = mysqli_fetch_array($exquery)) {
$person = $row['person'];
if (!array_key_exists($color, $output)) {
$output[$color] = array();
}
array_push($output[$color], $person);
}
}
Try changing:
array_push($output[$color], $person);
Into:
$output[$color][] = $person;
From the manual on array_push:
Note: If you use array_push() to add one element to the array it's better to use $array[] = because in that way there is no overhead of calling a function.
Note: array_push() will raise a warning if the first argument is not an array. This differs from the $var[] behaviour where a new array is created.

How to change php array output

I have the following function:
public function albums($artist) {
$artist = $this->db->sanitize($artist);
$query = "
SELECT `album`, `cover`
FROM `albums`
join `artists`
on `albums`.`artistsID` = `artists`.`id`
WHERE `artists`.`artist` = ?
";
$stmt = $this->db->mysqli->prepare($query);
$stmt->bind_param('s', $artist);
$stmt->execute();
$result = $stmt->get_result();
while($row = $result->fetch_assoc()) {
$data[] = $row;
}
return $data;
}
As you can see this will return an array with the album name and the cove file path.
The array output looks like this:
Array
(
[0] => Array
(
[album] => Album name
[cover] => file path
)
[1] => Array
(
[album] => Album name
[cover] => file path
)
)
As you can see it returns an array with 0 and 1 with an array of album and cover.
What I would like it to be is an array which is easier to use with a while loop.
So I can do something like:
while($row = $data) {
echo $row['album'].' <img src="'.$row['cover'].'">';
}
instead of 2 foreach loops like I have to do now. So my question is how can I get an array which is easier to use like you would normaly get when you do something like:
<?php
$query = mysqli_query($connect, "SELECT `username` FROM `users` WHERE `id` = 1");
while($row = mysqli_fetch_assoc($query)) {
echo $row['username'];
} //Just as an example to make the question a bit more understandable
?>
I hope someone could help me out on this one. If you need more information please let me know.
What I would like it to be is an array which is easier to use with a while loop
You should rather use a foreach loop for this … then the whole “problem” solves itself without any further meddling with the array structure.
You're using fetch_assoc() so...
while($row = $result->fetch_assoc()) {
echo $row['album'] . ' <img src="' . $row['cover'] . '">';
}

mysql and php associative array

I have this table:
Username, Fan, Count
1danny22, katana1973, 2
bob, rita, 2
mattyhacky, hayley, 2
mattyhacky, dickie1eye, 1
mattyhacky, xxjodiexx, 1
And I want to load it into an associative array which would looks like this:
Array (
[1danny22] => Array (
[katana1973] => 2,
),
[bob] => Array (
[rita] => 2,
),
[mattyhacky] => Array (
[hayley] => 2,
[dickie1eye] => 1,
[xxjodiexx] = > 1,
)
)
$strSQL = "SELECT username, fan, count( * ) AS intCount
FROM fan
GROUP BY username, fan
ORDER BY username, intCount DESC";
$strResult = mysql_query($strSQL);
while($objRow=mysql_fetch_object($strResult))
{
code should go in here!!!
}
But I'm really struggling.
Can someone please help?
Many thanks
TheBounder.
Easy peasy...
$sql = "SELECT Username, Fan, Count FROM datatbl";
$res = mysql_query($sql);
while ($row = mysql_fetch_object($res))
$data[$row->Username][$row->Fan] = $row->Count;
I should explain further. What your doing here is creating a base index for your data array that uses the common associate Username as the index. Within that index you have another array, this using the Fan as the index and the value of each item being the count. My above example is the easiest way to build the structure that you are looking at.
Now if you run a print_r on the array you will see the structure you defined.
assuming your initial array is in $data, with for instance
$data[0] = array("1danny22", "katana1973", 2);
you could go with
$output = array();
foreach($data as $line) {
$output[$line[0]][$line[1]] = $line[2];
}
You could build it programmatically from the returned result... bare bones example:
$ar = array();
$result = mysql_query("SELECT * FROM your_table");
while ($ar[] = mysql_fetch_assoc($result));
print_r($ar);

Categories