php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #46429 columnCount() on sqlite3 returns invalid column count
Submitted: 2008-10-30 12:24 UTC Modified: 2008-10-31 11:24 UTC
From: balustefan at gmail dot com Assigned:
Status: Not a bug Package: PDO related
PHP Version: 5.2.6 OS: FreeBSD 7.0-RELEASE i386
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: balustefan at gmail dot com
New email:
PHP Version: OS:

 

 [2008-10-30 12:24 UTC] balustefan at gmail dot com
Description:
------------
Seems like columnCount() returns invelid int value. No mater how many 
results match a query, columnCount() will return int(3). Even for no 
results.
The underlying database is sqlite3-3.5.6

Reproduce code:
---------------
1. create an sql table in a freshly database file:

sqlite> create table hosts(hw char(18),
> ip varchar(16),
> extra text,
> constraint hw_pkey primary key(hw));

2. insert some data into it:

sqlite> insert into hosts values('00:19:21:57:44:f8','89.122.239.55','comment');

sqlite> insert into hosts values('00:e0:4c:20:dc:15','194.102.255.234','some comment');

3. use a php function like this one to fetch a "host":

function fetchHost($hwAddr)
{
	$sqlQuery = "SELECT * FROM hosts WHERE hw = ?";

	if (($sqlStatement = $sqlPDO->prepare($sqlQuery)) === false)
		return (bool) false;

	if ($sqlStatement->execute(array($hwAddr)) === false)
		return (bool) false;

	var_dump($sqlStatement->columnCount());

	if ($sqlStatement->columnCount() < 1)
		return (bool) true;

	return $sqlStatement->fetch(PDO::FETCH_ASSOC);
}



Expected result:
----------------
The output of var_dump() should be 0 if no rows are found, and number of 
rows if all is ok.

Actual result:
--------------
On 0 or more rows returned, columnCount() returns int(3).

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-10-31 01:24 UTC] felipe@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

See:
http://docs.php.net/manual/en/pdostatement.columncount.php

No result set == no query executed.
(like prepare() not followed by execute())
 [2008-10-31 10:49 UTC] balustefan at gmail dot com
Well, are you can see from the fetchHost() function, I call prepare() 
then call execute().

This is the actual function that I use:

function fetchHost($hwAddr)
{
	global
		$appErr,
		$sqlPDO;

	$sqlQuery = "SELECT * FROM hosts WHERE hw = ?";

	var_dump($hwAddr);

	if (($sqlStatement = $sqlPDO->prepare($sqlQuery)) === false) {
		$appErr = $sqlPDO->errorInfo();
		return (bool) false;
	}

	var_dump($sqlStatement);

	if ($sqlStatement->execute(array($hwAddr)) === false) {
		$appErr = $sqlStatement->errorInfo();
		return (bool) false;
	}

	var_dump($sqlStatement);

	var_dump($sqlStatement->columnCount());

	if ($sqlStatement->columnCount() < 1)
		return (bool) true;

	return $sqlStatement->fetch(PDO::FETCH_ASSOC);
}

The results of the var_dump()'s are:

string(17) "00:e0:4c:20:dc:20"

object(PDOStatement)#3 (1) {
  ["queryString"]=>
  string(32) "SELECT * FROM hosts WHERE hw = ?"
}

object(PDOStatement)#3 (1) {
  ["queryString"]=>
  string(32) "SELECT * FROM hosts WHERE hw = ?"
}

int(3)



I certainly don't have 3 entries returned since there are ONLY two 
inserted into the database :) and 00:e0:4c:20:dc:20 really doesn't 
match any the them.
 [2008-10-31 11:04 UTC] felipe@php.net
But the query was executed. This is the requirement to you can get the number of fields.
 [2008-10-31 11:24 UTC] balustefan at gmail dot com
I am sorry, i've confused rowCount() with columnCount(). The bug is 
bogus. However, I am not sure what to use in order to see how many rows 
have been returned by the select command. rowCount() is to be used for 
INSERT, UPDATE and DELETE only.

Is there any way to get the rows returned by SELECT other than 
count(fetchAll()) ?
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 23:01:26 2024 UTC