I have trying to create a list of manufactures with a count.
This is my query
$query = $this->pdo->prepare('SELECT manufacture, count(*) AS count
FROM listed_watches
GROUP BY manufacture');
But when i do print_r, its duplicating the results. It is showing "manufacture" and "count" but its also showing [0] and [1], how come?
I just want it to show [manufacture], [count]
Array
(
[0] => Array
(
[manufacture] => Audemars Piguet
[0] => Audemars Piguet
[count] => 2
[1] => 2
)
[1] => Array
(
[manufacture] => Bell and Ross
[0] => Bell and Ross
[count] => 3
[1] => 3
)
[2] => Array
(
[manufacture] => Bulova
[0] => Bulova
[count] => 1
[1] => 1
)
)
try this :
$result = $query->fetchAll(PDO::FETCH_ASSOC);
You can use by default this fetching method in the initialization of your connection with :
$pdo_options[PDO::ATTR_DEFAULT_FETCH_MODE] = PDO::FETCH_ASSOC;
Related
I have the following question, let's say I'm querying MySQL database to get the following array:
Array ( [0] => Array ( [topicid] => 4 ) [1] => Array ( [topicid] => 5 ) [2] => Array ( [topicid] => 6 ) )
These are the results from that I get from the following SQL request
$sql=$db->query("SELECT topicid from opentopics WHERE userid=?","$userid")->fetchAll();
This query gives me the list of topics that are opened to certain user ids. Now there are several topics with IDs up to 3 that are open to everyone, so I want to use them also on my webpage. These topics are not opened in the opentopics table, but are made available to everyone based on their ID (less than 3).
So the question is following, how do I modify the multidimensional (i assume) array I get from MySQL request, how to add 3 new entries to the beghinning of the above mentioned array so it looks like:
Array ( [0] => Array ( [topicid] => 1 ) [1] => Array ( [topicid] => 2 ) [2] => Array ( [topicid] => 3 ) [3] => Array ( [topicid] => 4 ) [4] => Array ( [topicid] => 5 ) [5] => Array ( [topicid] => 6 ) )
As you may have notices in my text editory I have manually edited the text of the array, by adding this
[0] => Array ( [topicid] => 1 ) [1] => Array ( [topicid] => 2 ) [2] => Array ( [topicid] => 3 )
you can fetch your result like this example then use array_merge
$mysqli = new mysqli('localhost', 'root', '', 'test');
$manual = array(
[ 'id' => 9 , 'first_name' => 'John9' , 'last_name' => 'Doe9'] ,
[ 'id' => 19 , 'first_name' => 'John19' , 'last_name' => 'Doe19']
);
$sql = " SELECT * from users ";
$result = $mysqli -> query($sql);
$rows = $result -> fetch_all(MYSQLI_ASSOC);
$results = array_merge($manual , $rows);
the results will be like this
Array
(
[id] => 9
[first_name] => John9
[last_name] => Doe9
)
Array
(
[id] => 19
[first_name] => John19
[last_name] => Doe19
)
Array
(
[id] => 1
[first_name] => John
[last_name] => Doe
)
Array
(
[id] => 2
[first_name] => John3
[last_name] => Doe
)
Array
(
[id] => 4
[first_name] => John4
[last_name] => Doe
)
Here is my Sample table:
Here is My Query:
SELECT * FROM table
Result for above query is:
Array
(
[0] => Array
(
[id] => 1
[name] => a
[country] => x
)
[1] => Array
(
[id] => 2
[name] => b
[country] => y
)
)
I need to get the field names in the first element of the array.
Expected result array is:
Array
(
[0] => Array
(
[id] => id
[name] => name
[country] => country
)
[1] => Array
(
[id] => 1
[name] => a
[country] => x
)
[2] => Array
(
[id] => 2
[name] => b
[country] => y
)
)
How can I modify the Query to get this result?
Thanks in advance...
Display issues should generally be dealt with in application code, not SQL queries. If you have your values in an array (called $results, say), you can use this code to add the desired entry:
array_unshift($results, array_combine(array_keys($results[0]), array_keys($results[0])));
print_r($results);
Output:
Array
(
[0] => Array
(
[id] => id
[name] => name
[country] => country
)
[1] => Array
(
[id] => 1
[name] => a
[country] => x
)
[2] => Array
(
[id] => 2
[name] => b
[country] => y
)
)
Demo on 3v4l.org
SELECT 'id' id, 'name' name, 'country' country
UNION ALL
SELECT id, name, country FROM table
ORDER BY 'id' != id
One way to do it with array_keys, array_combine and array_unshift. If I were you I will get the SQL result as $array variable and do some processing on php side like below on query side.
<?php
$array = [['id'=>1,'name'=>'a','country'=>'x'],['id'=>1,'name'=>'b','country'=>'y']];
$keys = array_keys($array[0]);
$first = array_combine($keys,$keys);
array_unshift($array,$first);
print_r($array);
?>
WORKING DEMO: https://3v4l.org/IqZVk
I'm a beginner in this framework. Though I have gone over the basics, there is one thing which is troubling me. As of now, I'm using
$group_sql = "SELECT uid FROM {$table}";
$group_users = Yii::app()->db->createCommand($group_sql)->queryAll();
print_r($group_users);
results in
Array
(
[0] => Array
(
[uid] => 2
)
[1] => Array
(
[uid] => 3
)
[2] => Array
(
[uid] => 4
)
[3] => Array
(
[uid] => 5
)
)
But I'd like to change the format in which the data is returned. What I'm looking for is something like
Array
(
[0] => 2
[1] => 3
[2] => 4
[3] => 5
)
OR
Array
(
[uid] => Array
(
[0] => 2
[1] => 3
[2] => 4
[3] => 5
)
)
I'm aware that I can go through the documentation and get my answer, but due to time constraints, I'm taking the liberty to shamelessly ask this over here.
Thanks in advance.
Use queryColumn() method instead of queryAll()
I am using PEAR's PDO library to grab out the results I need.
Whilst I know I could just manually rebuild the array when the results are in, I was wondering is there a internal function I have missed that might do this for me when grabbing results directly from the DB?
Currently:
function get($query){
$affected =& $this->conn->query($query);
// Always check that result is not an error
if (PEAR::isError($affected)) {
die($affected->getMessage());
}
$res = $affected->fetchAll();
return $res;
}
Returns
[sexnumbers] => Array
(
[0] => Array
(
[sex] => 1
[no] => 35
)
[1] => Array
(
[sex] => 2
[no] => 17
)
[2] => Array
(
[sex] => 3
[no] => 3
)
)
Wanted Result Set
[sexnumbers] => Array
(
[sex] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[no] => Array
(
[0] => 35
[1] => 17
[2] => 3
)
)
As you can see below I'm not getting any user info when I do a LDAP search to the security group. I want to use the $_SERVER[remote_user] to check if the user is a member of this group. I would also like to retrieve the info of this user and update the sql database with it. Is this possible?
$dn = "CN=Intra,OU=Common Security Groups,DC=mydomain,DC=local";
$filter = "(member=*)";
$ad = ldap_connect("IP") or die("Couldn't connect to AD!");
ldap_set_option($ad, LDAP_OPT_PROTOCOL_VERSION, 3);
$bd = ldap_bind( $ad, "username#mydomain.local", "password") or die("Can't bind to server.");
$sr = ldap_search($ad,$dn,$filter);
$entries = ldap_get_entries($ad, $sr);
print_r($entries);
Returns this:
Array
(
[count] => 1
[0] => Array
(
[objectclass] => Array
(
[count] => 2
[0] => top
[1] => group
)
[0] => objectclass
[cn] => Array
(
[count] => 1
[0] => Intra
)
[1] => cn
[description] => Array
(
[count] => 1
[0] => Group for (LDAP) INTRANET server access
)
[2] => description
[member] => Array
(
[count] => 4
[0] => CN=Fname1 Lname1,OU=Mail enabled users,OU=Aberdeen,DC=mydomain,DC=local
[1] => CN=Fname2 Lname2,OU=Mail enabled users,OU=Forres,DC=mydomain,DC=local
[2] => CN=Fname3 Lname3,OU=Houston,DC=mydomain,DC=local
[3] => CN=Fname4 Lname4,OU=Mail enabled users,OU=Bergen,DC=mydomain,DC=local
)
[3] => member
[distinguishedname] => Array
(
[count] => 1
[0] => CN=Intra,OU=Common Security Groups,DC=mydomain,DC=local
)
[4] => distinguishedname
[instancetype] => Array
(
[count] => 1
[0] => 4
)
[5] => instancetype
[whencreated] => Array
(
[count] => 1
[0] => 20100711172407.0Z
)
[6] => whencreated
[whenchanged] => Array
(
[count] => 1
[0] => 20100712063949.0Z
)
[7] => whenchanged
[usncreated] => Array
(
[count] => 1
[0] => 17491499
)
[8] => usncreated
[usnchanged] => Array
(
[count] => 1
[0] => 17498823
)
[9] => usnchanged
[name] => Array
(
[count] => 1
[0] => Intra
)
[10] => name
[objectguid] => Array
(
[count] => 1
[0] =>
)
[11] => objectguid
[objectsid] => Array
(
[count] => 1
[0] =>
)
[12] => objectsid
[samaccountname] => Array
(
[count] => 1
[0] => Intra
)
[13] => samaccountname
[samaccounttype] => Array
(
[count] => 1
[0] => 268435456
)
[14] => samaccounttype
[grouptype] => Array
(
[count] => 1
[0] => -2147483646
)
[15] => grouptype
[objectcategory] => Array
(
[count] => 1
[0] => CN=Group,CN=Schema,CN=Configuration,DC=mydomain,DC=local
)
[16] => objectcategory
[count] => 17
[dn] => CN=Intra,OU=Common Security Groups,DC=mydomain,DC=local
)
)
Everything worked fine when I used the normal DN:
$dn = "OU=Mail enabled users,OU=Bergen,DC=mydomain,DC=local";
But a AD expert told me this was a big NO-NO and that I should use Security Groups instead :\
Query the AD like this:
$dn = "DC=mydomain,DC=local";
$group_DN = "CN=Intra,OU=Common Security Groups,DC=mydomain,DC=local";
$filter = "(&(objectCategory=user)(memberOf=$group_DN))";
// ...
$sr = ldap_search($ad, $dn, $filter);
Have a look at the MSDN article about the LDAP search filter syntax for info on more complex filters.
Be sure to pay attention to the Special Characters section down on that page. A correct solution must pass $group_DN through an escaping mechanism before using it in the filter string!
Always try build filters as specific as possible. It is more efficient to let the LDAP server sort out records you don't want, instead of having more records transferred over the wire than you need and throw away half of them on the client.
Tomalak
I think the problem is that not all users in the Security Group comes from the same OU.
If I change
$dn = "DC=mydomain,DC=local";
to
$dn = "OU=Bergen,DC=mydomain,DC=local";
the filter works. But I also have 2 more OU's with users.