Link to home
Start Free TrialLog in
Avatar of designersx
designersx

asked on

why odbc_num_rows returns -1 always?

output is

-1
designersx  rupinder  meghsolutions   rahul

i want to ask you why there is -1.
<html>
<body>
<?php
$conn=odbc_connect('win','','');
$sql="SELECT * FROM customers";
$rs=odbc_exec($conn,$sql);
 
echo $count=odbc_num_rows($rs);// why odbc_row_count always print -1 when there are more than 1 records in the database
echo "<br>";
while (odbc_fetch_row($rs))
{
  $compname=odbc_result($rs,"CompanyName");
  $conname=odbc_result($rs,"ContactName");
  echo "<tr><td>$compname</td>";
  echo "<td>$conname</td></tr>";
}
odbc_close($conn);
echo "</table>";
?>
</body>
</html>

Open in new window

Avatar of shobinsun
shobinsun
Flag of India image

Hi,

Returns the numbers of rows in a result set. In a SELECT statement, this is the number of rows returned from the query. For UPDATE, INSERT, and DELETE statements, this is the number of affected rows. A lot of ODBC drivers don't know how to handle this function properly and return a result of -1.

So try :

odbc_pconnect()

and

oci_connect ()

Regards

Avatar of designersx
designersx

ASKER

odbc_pconnect() with this same -1 is shown.
oci_connect () it does not recognize this.

Hi,

Using odbc_num_rows() to determine the number of rows available after a SELECT will return -1 with many drivers.

So,

When you just need to verify that any rows returned from your query you can use select count(*) as cnt from table and then just get $row['cnt']



Or use a function to do this:

function best_odbc_num_rows($r1)  {

ob_start(); // block printing table with results

(int)$number=odbc_result_all($r1);

ob_clean(); // block printing table with results

return $number;

}


or

function useful_odbc_num_rows($result){

   $num_rows=0;

   while($temp = odbc_fetch_into($result, &$counter))

   {
       $num_rows++;
   }

@odbc_fetch_row($result, 0);   // reset cursor

   return $num_rows;
}


Regards
this is login form, here i am checking the username and password entered by the user and that in the database.

i am applying the concept, if there is any row returned from the database, that means user is successful login otherwise not.

but problem is how to check the no. of rows returned frm the database,
please make it correct. please see line 8.

output is:- Invalid username and password (always) even if i am entering the correct username and password.
$username=$_POST['username'];
$password=$_POST['password'];
 
$con=odbc_connect('fantaay','','');
$query="select * from userloginform where username='$username' and password='$password'";
$result=odbc_exec($con,$query);
$count_rows=odbc_num_rows($result);
if($count_rows>0)
	{
		session_start();
		$_SESSION['username']=$username;
		$_SESSION['password']=$password;
		$_SESSION['logged']=1;//logged=1-> true
		header("Location:submit_form.php");
	}
else
	{
		echo "Invalid username and password";	
		exit;
	}

Open in new window

Hi,

use the following:


$username=$_POST['username'];
$password=$_POST['password'];
 
$con=odbc_connect('fantaay','','');
$query="select count(*) from userloginform where username='$username' and password='$password'";
$result=odbc_exec($con,$query);
//$count_rows=odbc_num_rows($result);
$row = odbc_fetch_array($result);
//echo $row[0];
if($row[0]>0)
        {
                session_start();
                $_SESSION['username']=$username;
                $_SESSION['password']=$password;
                $_SESSION['logged']=1;//logged=1-> true
                header("Location:submit_form.php");
        }
else
        {
                echo "Invalid username and password";   
                exit;
        }
?>

Open in new window

still it don't works.

database is like this:

id  username      password
1  admin             admin


again it says invalid username and password.
Hi,

try with :

$query="select count(*) from userloginform where username='admin' and password='admin'";

What is the output of  echo $row[0];

It should display atleast 1.


Then

$username='"'.$_POST['username'].'"';
$password='"'.$_POST['password'].'"';
 
$con=odbc_connect('fantaay','','');
$query="select count(*) from userloginform where username=$username and password=$password";

no sir there is no output with this, u can check it.

i have records in the database.
<?php
$username=$_POST['username'];
$password=$_POST['password'];
 
$con=odbc_connect('fantaay','','');
$query="select count(*) from userloginform1 where username='$username' and password='$password'";
$result=odbc_exec($con,$query);
 
$row = odbc_fetch_array($result);
echo $row[0];
 
if($row[0]>0)
	{
		session_start();
		$_SESSION['username']=$username;
		$_SESSION['password']=$password;
		$_SESSION['logged']=1;//logged=1-> true
		header("Location:submit_form.php");
	}
else
	{
		echo "Invalid username and password";	
	}		
?>

Open in new window

Hi,

$query="select count(*) from userloginform where username='admin' and password='admin'";

What is the output with this  I mean

echo $row[0];
no output is shown.

my database is

ID   username  password
3     admin        admin
4     admin1      admin1

Hi,

echo "Resource ID:", $result;

What it displays?
Hi,

$query="select count(*) from userloginform where username='admin' and password='admin'";
$result = odbc_exec($con, $query);
odbc_fetch_into($result, $count, 1);
echo $count;

also check :

$query="select count(*) from userloginform ";
$result = odbc_exec($con, $query);
odbc_fetch_into($result, $count, 1);
echo $count;

<< echo "Resource ID:", $result;

>>Resource id #3 is shown

-------------------------

<< echo $count;

>> Array is shown
Hi,

Ok..

Check one more thing:

print_r($count);

I think this willl display the count within an array.

what is displaying?

Please copy the exact result.
output:-

Array ( [0] => 5 )

if i have 5 records in the database, it is returning the no. of rows.
can i write like this??

$rows=print_r($count);
if($rows)>0
 { }
Hi,

if(print_r($row[0]))>0
 { }

Hope this will help you
it is giving error

syntax error, unexpected '>' in C:\wamp\www\fantaay\process_login.php on line 9
quote from PHP.net ODBC functions

"After minutes of frustration, I realized why odbc_num_rows was not returning the number of affected rows on a prepared update query.  I'm using ODBC to connect to Microsot SQL Server 2005.

My corrected code:

<?php
$query = odbc_prepare($conn, 'UPDATE table SET cat = ? WHERE id = 1');
$result = odbc_execute($query, $category);
$affected = odbc_num_rows($query);
?>

This code works.  I was frustrated that odbc_num_rows($result) didn't work as I expected, but instead required me to pass the original prepared query to this function."

maybe try this.
<html>
<body>
<?php
$conn=odbc_connect('win','','');
$sql=odbc_prepare($conn, "SELECT * FROM customers");
$rs=odbc_exec($sql, $o);
$count=odbc_num_rows($rs);
echo $count;
echo "<br>";
while (odbc_fetch_row($rs))
{
  $compname=odbc_result($rs,"CompanyName");
  $conname=odbc_result($rs,"ContactName");
  echo "<tr><td>$compname</td>";
  echo "<td>$conname</td></tr>";
}
odbc_close($conn);
echo "</table>";
?>
</body>
</html>

Open in new window

Hi,

syntax error, unexpected '>' in C:\wamp\www\fantaay\process_login.php on line 9

use:

if((print_r($row[0]))>0)
 { }
firstly thanks for all of the guys of experts- exchange helping such a wide community of the people.

see this line sir,$rs=odbc_exec($sql, $o);

what is $o?
Hi,

$o  is the mistake .Actually it is "$conn"
$sql=odbc_prepare($conn, "SELECT * FROM customers");
$rs=odbc_exec($sql, $o);
$count=odbc_num_rows($rs);

giving errors

Warning: odbc_exec() [function.odbc-exec]: SQL error: [Microsoft][ODBC Microsoft Access Driver] Invalid SQL statement; expected 'DELETE', 'INSERT', 'PROCEDURE', 'SELECT', or 'UPDATE'., SQL state 37000 in SQLExecDirect in C:\wamp\www\fantaay\php_with_mdb\1\1.php on line 22

Warning: odbc_num_rows(): supplied argument is not a valid ODBC result resource in C:\wamp\www\fantaay\php_with_mdb\1\1.php on line 23

Warning: odbc_fetch_row(): supplied argument is not a valid ODBC result resource in C:\wamp\www\fantaay\php_with_mdb\1\1.php on line 25
Hi,

Use this:

$sql=odbc_prepare($conn, "SELECT * FROM customers");
$rs=odbc_exec($sql, $conn);
$count=odbc_num_rows($rs);
after this also,

sql=odbc_prepare($conn, "SELECT * FROM customers");
$rs=odbc_exec($sql, $conn);
$count=odbc_num_rows($rs);


error is same, really i am also confused why this error is coming.
ASKER CERTIFIED SOLUTION
Avatar of shobinsun
shobinsun
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
we use odbc_execute with odbc_prepare, by this atleast one error gets removed but rest of the two errors comes with this and also that u have told me to do.


Hi,

I think the following two errors remaining:

Warning: odbc_num_rows(): supplied argument is not a valid ODBC result resource in C:\wamp\www\fantaay\php_with_mdb\1\1.php on line 23

Warning: odbc_fetch_row(): supplied argument is not a valid ODBC result resource in C:\wamp\www\fantaay\php_with_mdb\1\1.php on line 25


right?


Then check

echo "Resource ID:", $rs;


The above erros means that there is no result to be fetched.


yes sorry, it should have been $conn.

echo "Resource ID:", $rs;

result is 1
sir this is not done. i think we should leave it.

is there any other way to check whether the username entered by the user and in the database is same or not.