Stuck with this Code. I need to get the GidNumber of a particular user using PHP. Please help me in this. I have switched to get GIdNumber instead of getting MemberOf function since it is not working. Thanks in Advance.
<?php
session_start();
$ldaphost ="ldap://(hostaddress)";
$ldapport = 389;
$ds = ldap_connect($ldaphost, $ldapport)
or die("Could not connect to $ldaphost");
$person="comp1";
$dn = "ou=Users,dc=compldap,dc=com";
$filter="(|(sn=$person*)(givenName=$person*))";
$justthese = array("gidNumber");
$sr=ldap_search($ds, $dn, $filter, $justthese);
$info = ldap_get_entries($ds, $sr);
echo "ENTRY RESULTS: ";
print_r($info[0]['gidNumber']);
echo "<br />";
?>
The filter seems odd to me, try :
$filter="(|(sn=".$person."*)(givenName=".$person."*))";
Also, the gitNumber should be indexed like this :
print_r($info[0]['gidnumber'][0]);
Try also to check how many entries your search returned :
echo $info["count"]." entries returned\n";
EDIT :
Strangely enough I did not see that you never bind to the server before doing your search ... :
See : http://php.net/manual/fr/function.ldap-bind.php
Related
I have an LDAP annuary working on Active Directory (Win Server 2008). I try to connect to this AD with PHP (and LDAP lib) and retrieve all distributions lists from a specific OU.
The connection and authentication is working, but when I want to retrieve the lists from OU, I dont have any results (rights are OK and in local it's working too, without PHP)
Here is my code :
$ldap_host = "ip.add.re.ss";
$ldap_dn = "OU=Listes,DC=domain,DC=tld"; // all there infos are ok
$base_dn = "DC=domain,DC=tld";
$ldap = ldap_connect($ldap_host);
$user = 'username'; $password = '';
$filter="(sAMAccountName=recette)";
$filter="(&(objectClass=user)(objectCategory=person)(OU=Lists))";
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION,3);
ldap_set_option($ldap, LDAP_OPT_REFERRALS,0);
ldap_bind($ldap, $user, $password);
$results = ldap_search($ldap,$base_dn, $filter);
$member_list = ldap_get_entries($ldap, $results);
If I delete the OU=Lists, it's working, I have results, but the one I want. Then, when I add the OU=Lists, I don't have any results.
Can you help me please? Thanks.
You said you are trying to "Retrieve Distributions lists" and yet your LDAP filter is to retrieve users which have the attribute ou=Lists:
(&(objectClass=user)(objectCategory=person)(OU=Lists))
Try an LDAP filter as:
(&(objectCategory=Group)(proxyAddresses=*))
more like these are at:
http://ldapwiki.com/wiki/Active%20Directory%20Group%20Related%20Searches
I have a successful connection to the database through this php script but it is not returning any values even though it is connected. I am checking for the results on my web browser and it just returns a blank screen. I have used the same script (different queries) to access two other tables in the database and they are both working fine. Here is my code:
<?php
$username = "xx";
$password = "xxx";
$host = "xxxxx";
$database="xxxxx";
$server = mysql_connect($host, $username, $password);
$connection = mysql_select_db($database, $server);
$myquery = "SELECT `AUTHOR`, `In_order` from `authors`";
$query = mysql_query($myquery);
if ( ! $query ) {
echo mysql_error();
die;
}
$data = array();
for ($x = 0; $x < mysql_num_rows($query); $x++) {
$data[] = mysql_fetch_assoc($query);
}
echo json_encode($data);
mysql_close($server);
?>
It is probably some silly mistake that I have over looked but I have been stuck on it for longer than I should! thanks in advance for any feedback
Tried you code locally on some data and it returns everything ok.
I needed to change the select to match my data
So I am 95% sure the problem is in your query / db settings.
I would first check if your columns in database is really called AUTHOR and 'In_order' with the exact capital letters.
MySql names can be case sensitive depending on your db server settings, and this could be the problem
Sidenote: if you can research mysqli and pdo for connecting to DB instead of mysql that is deprecated.
Try this:
$myquery = "SELECT `AUTHOR`, `In_order` from `authors`";
$query = mysql_query($myquery);
$num = mysql_num_rows($query);
var_dump($query);
var_dump($num);
echo mysql_error();
and tell us what it all says.
Edit: okay, so it's 231 rows in your table, as the var_dump($num) says. Now let's try and get them at last, but in a slightly more efficient way:
while ($row = mysql_fetch_assoc($query)) {
$data[] = $row;
}
echo json_encode($data);
I have a feeling that your "for" loop and mysql_fetch_assoc() inside is what plays tricks with you, because both of them use different internal counters.
I recently started studying webdevelopment and for the first project we have to make a very simple login and registration function. now I got somewhere but suddenly I started getting this error.
Unable to jump to row 0 on MySQL result index 4.
This is the code where I get the error.
$conn = mysql_connect($servername, $username, $password, $database) or die("!Server");
if(!$conn){
die("Connection error: " . mysql_error());
}else{
echo "connection made";
}
$select_db= mysql_select_db($database, $conn);
if(!$select_db){
die("Database selection failed:: " . mysql_error());
}else{
echo "database selected";
}
$login_user = $_GET['username'];
$select_password = "SELECT `password` FROM `members` WHERE `username` = '$login_user'";
$result = mysql_query($select_password);
if(!$result){
die("Could not fetch the data " . mysql_error());
}
$password = mysql_result($result, 0);
echo $password;
I know there are other posts asking the same question but none of them where really helpful.
hope someone is able to help me.
Thanks for reading :)
It is possible that this row:
password = mysql_result($result, 0);
Cannot get rows when it does not match anything which is why you get the error.
Try instead:
if( $password = mysql_fetch_assoc($result) ) {
echo $password['password'];
} else {
echo 'no match';
}
See if that helps.
However, you really should move from mysql_* to PDO or mysqli.
I suggest to work with PDO, instead of mysql_... functions.
Not only because it's newer and object oriented but also because mysql... is deprecated.
So my answer is with PDO
From your code example I assume you have variables of servername, database, username & password. So to connect with PDO will look like this:
$con = new PDO("mysql:host=$servername;dbname=$database", $username, $password);
Then you can take your $_GET parameter using filter_input, which is also more suggested. Simple assignment look like this:
$login_user = filter_input(INPUT_GET,'username');
and then to run & fetch query like this:
$result_query = $con->query("SELECT `password` FROM `members` WHERE `username` = '$login_user'");
$result = $result_query->fetch(PDO::FETCH_ASSOC);
Now $result is an associative array, so to get password just use:
echo $result['password'];
Thanks alot for the help :) out of these answers I got everything I needed and now I am also going to switch to PDO for the final release though I am gonna stay with MySQL for now since I have to turn it in by monday.
Thanks alot :)
Change this:
$select_password = "SELECT `password` FROM `members` WHERE `username` = '$login_user'";
Into this:
$select_password = "SELECT password FROM members WHERE username = '$login_user'";
Can anyone see what the problem with my code is / where im going wrong?
I know i have the correct host,database,user and password.
This is the code in the php file, it should get all the details available on the players from my sql database, however if i go on the page it just gives me a white page. Im using go daddy as a host and my database is also on there.
Any ideas? thanks
<?php
$host = "abc12345"; //Your database host server
$db = "abc12345"; //Your database name
$user = "abc12345"; //Your database user
$pass = "abc12345"; //Your password
$connection = mysql_connect($host, $user, $pass);
//Check to see if we can connect to the server
if (!$connection) {
die("Database server connection failed.");
} else {
//Attempt to select the database
$dbconnect = mysql_select_db($db, $connection);
//Check to see if we could select the database
if (!$dbconnect) {
die("Unable to connect to the specified database!");
} else {
$query = "SELECT * FROM Player";
$resultset = mysql_query($query);
$records = array();
//Loop through all our records and add them to our array
while ($r = mysql_fetch_assoc($resultset)) {
$records[] = $r;
}
//Output the data as JSON
echo json_encode($records);
}
}
?>
The script is all right, I checked it with a different query.
Assuming that the table Player is not empty, the error can be either with your raw sql query (as pointed by #Sharikov in comments), otherwise the error is with the way you have configured your godaddy server.
For debugging that, I would suggest inserting dummy print_r or echo statements before you execute the query, and going through your apache logs at /var/log/apache2/access.log.
Also make sure that you don't have any core php package missing on your server (like php5-mysql if you use mysql).
Please can someone help me with this I cant find very much information on this problem.
I am connecting fine to PostgreSql database but when I look through the array and display nothing is being displayed, however the exact amount of rows are being displayed s oi know the connection/query are the right syntax, must be the variables syntax but ive tried everything I can find to make it work, any ideas?
<?php
pg_connect("host=******** port=**** dbname=****** user=***** password=********") or die("Couldn't Connect"); // Connect to the Database
$query = "SELECT * FROM phones";
$query = pg_query($query);
while($row = pg_fetch_array($query))
{
echo "Model: ".$row['Model']."<br />";
echo "OS: ".$row['OS']."<br />";
echo "Description: ".$row['Description']."<br /><br />";
}
?>
Thank you in advance for any help
Try to use pg_last_error();
<?php
$dbconn = pg_connect("dbname=publisher") or die("Could not connect");
// Query that fails
$res = pg_query($dbconn, "select * from doesnotexist");
echo pg_last_error($dbconn);
?>
http://us2.php.net/pg_last_error
Try pg_fetch_assoc instead of pg_fetch_array, it might work. It works for MySQL.