Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with a query in PHP (not returning any records)

Status
Not open for further replies.

deeciple

Technical User
Mar 1, 2012
70
US
Hi All,

I have this code to authenticate users on an active directory server and then query a table on my database that contains their user level. For some reason I am not getting any records returned when I run the query in my script but if I run it directly in MySQL i get back the expected record. In my troubleshooting I am getting the dreaded "mysql_fetch_array() expects parameter 1 to be resource, boolean given..".

Here is the script:

PHP:
<?php require_once("connection.php"); ?>
<?php
function authenticate($user, $password) {
  // Active Directory server
  $ldap_host = "My-AD-Server";

  // Active Directory port
  $ldap_port = 389;

  // connect to active directory
  $ldap = ldap_connect($ldap_host, $ldap_port);

  // verify user and password
  if($bind = @ldap_bind($ldap, $user, $password)) {

    // valid
    // check presence in groups
    $strUser=mysql_real_escape_string($user);
    $result=mysql_query("SELECT * FROM tblusers WHERE adUserName = '$strUser'");

    if($result) {
      $row=mysql_fetch_array($result);
      $access=$row['userLevel'];
    }else{
      $access="visitor";
    } 

    // establish session variables
    $_SESSION['MM_Username'] = $user;
    $_SESSION['MM_UserGroup'] = $access;
    return true;

  } else {
    // invalid name or password
    return false;
  }
}
?>
<?php mysql_close($dbConnection); ?>

I am sure there is something simple I am overlooking. Thanks in advance for your help.

Kind regards,

Ken
 
Just an update:
As a troubleshooting measure I modified the query line as follows:
PHP:
    $result=mysql_query("SELECT * FROM `tblusers` WHERE adUserName = '$strUser'") or die(mysql_error());

Now the mysql error I am getting is "No database selected", which is odd because line 1 of my script creates the connectionand selects the database. I have included this in all of my scripts that run queries and they all work fine except for this one.

Any help is appreciated.

Thanks,

Ken
 
What is the precise error message?

Post the content of connection.php (passwords replaced with asterisks)
 
... and ... obviously make sure that it is the 'right' connections.php. if the connection script works in some scripts but not in others then it is very likely that you have two connection scripts and are referencing the incorrect one. This is not uncommon in apps that have a deep hierarchy and where the coder is using relative links and/or relying on the include path. In such circumstances I strongly recommend referencing all files using absolute uris; and to make that easier, do something like this in a script that is always called from the webroot

Code:
if(!defined('ABSPATH')) define('ABSPATH', realpath(dirname(__FILE__)) . DIRECTORY_SEPARATOR);

for debugging your connections.php should have express error catching

Code:
$dbHost = '';
$userName = '';
$password = '';
$dbName = '';
mysql_connect($dbHost, $userName, $password) or die(mysql_error());
mysql_select_db($dbName) or die(mysql_error());

remember also that in your code above, the function is in the function scope and you have not provided the code that calls the function. If the calling code is AFTER or on another page to the call to mysql_close then the database connection will be closed by the time you get to the function scope. and an error will be thrown (although I would not have expected it to be the error you quoted).

Typically we don't bother with the mysql_close() call as php will execute it automatically at the end of its execution (normally).

If you _really_ want to keep it then change your connections.php to this instead

Code:
class dbConnection {
 private $dbHost = '';
 private $userName = '';
 private $password = '';
 private $dbName = '';
 public $dbConnection;

 public function __construct(){
  $this->dbConnection = mysql_connect($this->dbHost, $this->userName, $this->password) or die(mysql_error());
  mysql_select_db($this->dbName) or die(mysql_error());
 }
 public function __destruct(){
  @mysql_close();
 }
}
$connection = new dbConnections;

do also note that the mysql_* extension has now been officially deprecated as of php 5.5 and will be removed in the future. you are now recommended to use the PDO or mysqli extensions. Personally I find mysqli slower than mysql, which is annoying. and I tend to use PDO for everything. there are some speed penalties (it is just an abstraction layer after all) but I prefer the usability of the extension.

Getting ready for this migration will become important - it behoves people either to start the process now or, better (in my view) to start coding their applications so that all entities address the database through a common object; thus only one thing needs to be changed when moving database extensions or even database back-ends.
 
jpadie said:
remember also that in your code above, the function is in the function scope

I know it wasn't what you were referring to with this but I placed the db connection inside the function and it worked. Thanks for your tip on closing the connection. I removed that line too.

Kind regards,

Ken
 
then it was the kysql_close causing the problem. by putting the include to connection a php inside the function the connection would have been expressly reopened had it been previously closed. the same effect should occur if you bring the connection back to the global scope and delete the mysql_close line.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top