We help IT Professionals succeed at work.

ODBC equivalent to mysql_num_rows()

generali
generali asked
on
Medium Priority
2,541 Views
Last Modified: 2013-12-12
Does anybody out there Know the equivalent odbc function to mysql_num_rows(). I tried to use odbc_num_rows but it returns -1 when i replace the mysql function below. Or is there an alternative solution?

if (odbc_num_rows($result) == -1) {
        // the user id and password match,  
        // set the session
        $_SESSION['db_is_logged_in'] = true;
        // after login move to the main page
        header('Location: main1.php');
        exit;
Comment
Watch Question

This function will return -1 on error. Your sql query is correct?

For details see http://www.php.net/odbc_num_rows
also check the user comments on the page, specially the first one i.e.
odbc_num_rows for access does return -1. I use count($resultset);

Author

Commented:
Yeah id had a look at that link youve provided and gathered that it returned -1 for access. Im trying to set up a basic login system where my uid and pswd are stored in access, just converting the code from some ihave used from mysql. i tried the count($result) but this just lets me login with any password. the code is below:

<?php
// start the session
session_start();
$errorMessage = '';
if (isset($_POST['txtUserId']) && isset($_POST['txtPassword'])) {
    $cnx = odbc_connect( 'MultiSport' , 'root', '' );
    if (!$cnx) {
        Error_handler( "Error in odbc_connect" , $cnx );
    }
     
    $userId   = $_POST['txtUserId'];
    $password = $_POST['txtPassword'];
     
    // check if the user id and password combination exist in database
    $sql = "SELECT user_id  
            FROM user_auth
            WHERE user_id = '$userId' AND password = '$password'";
     
    $result = odbc_exec( $cnx, $sql);
      if (!$result) {
        Error_handler( "Error in odbc_exec( no cursor returned ) " , $cnx );
    }
     
    if (odbc_num_rows($result) == 1) {
        // the user id and password match,  
        // set the session
        $_SESSION['db_is_logged_in'] = true;
        // after login we move to the main page
        header('Location: main1.php');
        exit;
    } else {
        $errorMessage = 'Sorry, wrong user id / password';
    }
      
     
    odbc_close( $cnx);
}
?>

have no end of problems with odbc, much prefer php/mysql.
I have removed odbc_num_rows() and comparing the returned password from the DB.

Code is not tested, hope it will work. Change the code in this way

<?php
// start the session
session_start();
$errorMessage = '';
if (isset($_POST['txtUserId']) && isset($_POST['txtPassword'])) {
    $cnx = odbc_connect( 'MultiSport' , 'root', '' );
    if (!$cnx) {
        Error_handler( "Error in odbc_connect" , $cnx );
    }
     
    $userId   = $_POST['txtUserId'];
    $password = $_POST['txtPassword'];
     
    // check if the user id and password combination exist in database
    $sql = "SELECT user_id  
            FROM user_auth
            WHERE user_id = '$userId' AND password = '$password'";
     
    $result = odbc_exec( $cnx, $sql);
     if (!$result) {
        Error_handler( "Error in odbc_exec( no cursor returned ) " , $cnx );
    }
   
      $rs = odbc_fetch_array($result);
    if ($userId==$rs["user_id"] && $password==$rs["password"]) {
        // the user id and password match,
        // set the session
        $_SESSION['db_is_logged_in'] = true;
        // after login we move to the main page
        header('Location: main1.php');
        exit;
    } else {
        $errorMessage = 'Sorry, wrong user id / password';
    }
     
    odbc_close( $cnx);
}
?>

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Works fine, thanks a lot. I just had to add the 'password' field to the select query which took me a while to figure out being the amatuer that i am!!

Thanks again
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.