[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

why odbc_num_rows returns -1 always?

Posted on 2009-04-28
32
Medium Priority
?
712 Views
Last Modified: 2012-08-13
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

0
Comment
Question by:designersx
  • 15
  • 15
  • 2
32 Comments
 
LVL 14

Expert Comment

by:shobinsun
ID: 24257340
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
 

Author Comment

by:designersx
ID: 24257446
odbc_pconnect() with this same -1 is shown.
oci_connect () it does not recognize this.

0
 
LVL 14

Expert Comment

by:shobinsun
ID: 24257482
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
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 

Author Comment

by:designersx
ID: 24257682
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
 
LVL 14

Expert Comment

by:shobinsun
ID: 24257758
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
 

Author Comment

by:designersx
ID: 24257867
still it don't works.

database is like this:

id  username      password
1  admin             admin


again it says invalid username and password.
0
 
LVL 14

Expert Comment

by:shobinsun
ID: 24257896
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
 

Author Comment

by:designersx
ID: 24258437
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
 
LVL 14

Expert Comment

by:shobinsun
ID: 24258466
Hi,

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

What is the output with this  I mean

echo $row[0];
0
 

Author Comment

by:designersx
ID: 24258494
no output is shown.

my database is

ID   username  password
3     admin        admin
4     admin1      admin1

0
 
LVL 14

Expert Comment

by:shobinsun
ID: 24258528
Hi,

echo "Resource ID:", $result;

What it displays?
0
 
LVL 14

Expert Comment

by:shobinsun
ID: 24258606
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
 

Author Comment

by:designersx
ID: 24258716
<< echo "Resource ID:", $result;

>>Resource id #3 is shown

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

<< echo $count;

>> Array is shown
0
 
LVL 14

Expert Comment

by:shobinsun
ID: 24258752
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
 

Author Comment

by:designersx
ID: 24259154
output:-

Array ( [0] => 5 )

if i have 5 records in the database, it is returning the no. of rows.
0
 

Author Comment

by:designersx
ID: 24259162
can i write like this??

$rows=print_r($count);
if($rows)>0
 { }
0
 
LVL 14

Expert Comment

by:shobinsun
ID: 24259238
Hi,

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

Hope this will help you
0
 

Author Comment

by:designersx
ID: 24261168
it is giving error

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

Expert Comment

by:nplib
ID: 24263747
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
 
LVL 14

Expert Comment

by:shobinsun
ID: 24266813
Hi,

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

use:

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

Author Comment

by:designersx
ID: 24266914
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
 
LVL 14

Expert Comment

by:shobinsun
ID: 24266940
Hi,

$o  is the mistake .Actually it is "$conn"
0
 
LVL 14

Expert Comment

by:shobinsun
ID: 24266943
HI,

Look at this:

http://in2.php.net/odbc_exec
0
 

Author Comment

by:designersx
ID: 24267156
$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
 
LVL 14

Expert Comment

by:shobinsun
ID: 24267235
Hi,

Use this:

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

Author Comment

by:designersx
ID: 24268227
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
 
LVL 14

Accepted Solution

by:
shobinsun earned 100 total points
ID: 24268283
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
 

Author Comment

by:designersx
ID: 24269308
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
 
LVL 14

Expert Comment

by:shobinsun
ID: 24269392
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
 
LVL 17

Expert Comment

by:nplib
ID: 24269920
yes sorry, it should have been $conn.

0
 

Author Comment

by:designersx
ID: 24276781
echo "Resource ID:", $rs;

result is 1
0
 

Author Comment

by:designersx
ID: 24276823
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question