• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2333
  • Last Modified:

ODBC equivalent to mysql_num_rows()

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;
0
generali
Asked:
generali
  • 3
  • 2
1 Solution
 
Muhammad WasifCommented:
This function will return -1 on error. Your sql query is correct?

For details see http://www.php.net/odbc_num_rows
0
 
Muhammad WasifCommented:
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);
0
 
generaliAuthor 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.
0
 
Muhammad WasifCommented:
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);
}
?>
0
 
generaliAuthor 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
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now