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

designersxAsked:
Who is Participating?
 
shobinsunConnect With a Mentor Commented:
Hi,

Use this:

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

Hope this will help you.

Also look at that:

http://www.phpdig.net/ref/rn63re1333.html

Regards
0
 
shobinsunCommented:
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

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

0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
shobinsunCommented:
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
0
 
designersxAuthor Commented:
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

0
 
shobinsunCommented:
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

0
 
designersxAuthor Commented:
still it don't works.

database is like this:

id  username      password
1  admin             admin


again it says invalid username and password.
0
 
shobinsunCommented:
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";

0
 
designersxAuthor Commented:
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

0
 
shobinsunCommented:
Hi,

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

What is the output with this  I mean

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

my database is

ID   username  password
3     admin        admin
4     admin1      admin1

0
 
shobinsunCommented:
Hi,

echo "Resource ID:", $result;

What it displays?
0
 
shobinsunCommented:
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;

0
 
designersxAuthor Commented:
<< echo "Resource ID:", $result;

>>Resource id #3 is shown

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

<< echo $count;

>> Array is shown
0
 
shobinsunCommented:
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.
0
 
designersxAuthor Commented:
output:-

Array ( [0] => 5 )

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

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

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

Hope this will help you
0
 
designersxAuthor Commented:
it is giving error

syntax error, unexpected '>' in C:\wamp\www\fantaay\process_login.php on line 9
0
 
nplibCommented:
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

0
 
shobinsunCommented:
Hi,

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

use:

if((print_r($row[0]))>0)
 { }
0
 
designersxAuthor Commented:
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?
0
 
shobinsunCommented:
Hi,

$o  is the mistake .Actually it is "$conn"
0
 
shobinsunCommented:
HI,

Look at this:

http://in2.php.net/odbc_exec
0
 
designersxAuthor Commented:
$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
0
 
shobinsunCommented:
Hi,

Use this:

$sql=odbc_prepare($conn, "SELECT * FROM customers");
$rs=odbc_exec($sql, $conn);
$count=odbc_num_rows($rs);
0
 
designersxAuthor Commented:
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.
0
 
designersxAuthor Commented:
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.


0
 
shobinsunCommented:
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.


0
 
nplibCommented:
yes sorry, it should have been $conn.

0
 
designersxAuthor Commented:
echo "Resource ID:", $rs;

result is 1
0
 
designersxAuthor Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.