From: hack988 hack988 on
Could you post your database's class to here?
I'm use mssql with php for several years and read NULL Fields is never
appear your case.

2009/8/26 David Stoltz <Dstoltz(a)shh.org>:
> I tried that -it's in the first part of my message
>
>
> -----Original Message-----
> From: hack988 hack988 [mailto:hack988(a)dev.htwap.com]
> Sent: Wednesday, August 26, 2009 7:39 AM
> To: David Stoltz
> Cc: Paul M Foster; php-general(a)lists.php.net
> Subject: Re: [PHP] How to output a NULL field?
>
> use is_null() check it
>
> 2009/8/26 David Stoltz <Dstoltz(a)shh.org>:
>> Paul,
>>
>> This all started because when I try this:
>>
>> <?php echo $rs->Fields(22);?>
>>
>> It work fine, as long as there is a non-null value there, otherwise it
>> produces an error.
>>
>> Also, I'm working with a Microsoft SQL 2000 database, not MySQL....not
>> sure if that matters....
>>
>> But "echo $rs->Fields(22)" works perfectly for dumping values out of my
>> $rs recordset...that is, unless the value is NULL is the database - then
>> I get:
>>
>> Catchable fatal error: Object of class variant could not be converted to
>> string in D:\Inetpub\wwwroot\evaluations\lookup2.php on line 176
>>
>>
>> -----Original Message-----
>> From: Paul M Foster [mailto:paulf(a)quillandmouse.com]
>> Sent: Tuesday, August 25, 2009 4:39 PM
>> To: php-general(a)lists.php.net
>> Subject: Re: [PHP] How to output a NULL field?
>>
>> On Tue, Aug 25, 2009 at 02:00:04PM -0400, David Stoltz wrote:
>>
>>> $rs->Fields(22) equals a NULL in the database
>>>
>>> My Code:
>>>
>>> if(empty($rs->Fields(22))){
>>>       $q4 = "";
>>> }else{
>>>       $q4 = $rs->Fields(22);
>>> }
>>>
>>> Produces this error:
>>> Fatal error: Can't use method return value in write context in
>>> D:\Inetpub\wwwroot\evaluations\lookup2.php on line 32
>>>
>>> Line 32 is the "if" line...
>>>
>>> If I switch the code to (using is_null):
>>> if(is_null($rs->Fields(22))){
>>>       $q4 = "";
>>> }else{
>>>       $q4 = $rs->Fields(22);
>>> }
>>>
>>> It produces this error:
>>> Catchable fatal error: Object of class variant could not be converted
>> to
>>> string in D:\Inetpub\wwwroot\evaluations\lookup2.php on line 196
>>>
>>> Line 196 is: <?php echo $q4;?>
>>>
>>> What am I doing wrong?
>>>
>>> Thanks!
>>
>> Just a thought... do you really mean $rs->Fields(22) or do you mean
>> $rs->Fields[22]? The former is a function call and the latter is an
>> array variable.
>>
>> Paul
>>
>> --
>> Paul M. Foster
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>>
>
From: "TG" on
Sorry, didn't follow this whole thread but have you tried print_r() or
var_dump() to see what it looks like when it's type "variant"? I'd be
curious to see what you'd find in it.

Also, I forget about SQL Server, but MySQL has a function ifnull() which
can make sure you never return a null value. This is also handy for
checking for empty fields where you don't know if it's going to be an
empty string or a null:

SELECT * FROM table WHERE ifnull(somefield, '') = ''

If you know which columns are nullable, try seeing if there's an ifnull()
function in SQL Server to adjust the data before it even gets to your DB
class.

-TG

----- Original Message -----
From: "David Stoltz" <Dstoltz(a)SHH.ORG>
To: "Paul M Foster" <paulf(a)quillandmouse.com>, <php-general(a)lists.php.net>
Date: Wed, 26 Aug 2009 07:29:53 -0400
Subject: RE: [PHP] How to output a NULL field?

> Paul,
>
> This all started because when I try this:
>
> <?php echo $rs->Fields(22);?>
>
> It work fine, as long as there is a non-null value there, otherwise it
> produces an error.
>
> Also, I'm working with a Microsoft SQL 2000 database, not MySQL....not
> sure if that matters....
>
> But "echo $rs->Fields(22)" works perfectly for dumping values out of my
> $rs recordset...that is, unless the value is NULL is the database - then
> I get:
>
> Catchable fatal error: Object of class variant could not be converted to
> string in D:\Inetpub\wwwroot\evaluations\lookup2.php on line 176
>
>
> -----Original Message-----
> From: Paul M Foster [mailto:paulf(a)quillandmouse.com]
> Sent: Tuesday, August 25, 2009 4:39 PM
> To: php-general(a)lists.php.net
> Subject: Re: [PHP] How to output a NULL field?
>
> On Tue, Aug 25, 2009 at 02:00:04PM -0400, David Stoltz wrote:
>
> > $rs->Fields(22) equals a NULL in the database
> >
> > My Code:
> >
> > if(empty($rs->Fields(22))){
> > $q4 = "";
> > }else{
> > $q4 = $rs->Fields(22);
> > }
> >
> > Produces this error:
> > Fatal error: Can't use method return value in write context in
> > D:\Inetpub\wwwroot\evaluations\lookup2.php on line 32
> >
> > Line 32 is the "if" line...
> >
> > If I switch the code to (using is_null):
> > if(is_null($rs->Fields(22))){
> > $q4 = "";
> > }else{
> > $q4 = $rs->Fields(22);
> > }
> >
> > It produces this error:
> > Catchable fatal error: Object of class variant could not be converted
> to
> > string in D:\Inetpub\wwwroot\evaluations\lookup2.php on line 196
> >
> > Line 196 is: <?php echo $q4;?>
> >
> > What am I doing wrong?
> >
> > Thanks!
>
> Just a thought... do you really mean $rs->Fields(22) or do you mean
> $rs->Fields[22]? The former is a function call and the latter is an
> array variable.
>
> Paul
>
> --
> Paul M. Foster
>

From: Stuart on
2009/8/26 David Stoltz <Dstoltz(a)shh.org>:
> Paul,
>
> This all started because when I try this:
>
> <?php echo $rs->Fields(22);?>
>
> It work fine, as long as there is a non-null value there, otherwise it
> produces an error.
>
> Also, I'm working with a Microsoft SQL 2000 database, not MySQL....not
> sure if that matters....
>
> But "echo $rs->Fields(22)" works perfectly for dumping values out of my
> $rs recordset...that is, unless the value is NULL is the database - then
> I get:
>
> Catchable fatal error: Object of class variant could not be converted to
> string in D:\Inetpub\wwwroot\evaluations\lookup2.php on line 176

That error indicates that you're not getting NULL since NULL is easily
converted to a string.

Try this...

$var = $rs->Fields(22);
var_dump($var);

....and tell us what you get.

-Stuart

--
http://stut.net/

> -----Original Message-----
> From: Paul M Foster [mailto:paulf(a)quillandmouse.com]
> Sent: Tuesday, August 25, 2009 4:39 PM
> To: php-general(a)lists.php.net
> Subject: Re: [PHP] How to output a NULL field?
>
> On Tue, Aug 25, 2009 at 02:00:04PM -0400, David Stoltz wrote:
>
>> $rs->Fields(22) equals a NULL in the database
>>
>> My Code:
>>
>> if(empty($rs->Fields(22))){
>>       $q4 = "";
>> }else{
>>       $q4 = $rs->Fields(22);
>> }
>>
>> Produces this error:
>> Fatal error: Can't use method return value in write context in
>> D:\Inetpub\wwwroot\evaluations\lookup2.php on line 32
>>
>> Line 32 is the "if" line...
>>
>> If I switch the code to (using is_null):
>> if(is_null($rs->Fields(22))){
>>       $q4 = "";
>> }else{
>>       $q4 = $rs->Fields(22);
>> }
>>
>> It produces this error:
>> Catchable fatal error: Object of class variant could not be converted
> to
>> string in D:\Inetpub\wwwroot\evaluations\lookup2.php on line 196
>>
>> Line 196 is: <?php echo $q4;?>
>>
>> What am I doing wrong?
>>
>> Thanks!
>
> Just a thought... do you really mean $rs->Fields(22) or do you mean
> $rs->Fields[22]? The former is a function call and the latter is an
> array variable.
>
> Paul
>
> --
> Paul M. Foster
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
From: "David Stoltz" on
This:
$var = $rs->Fields(22);
var_dump($var);

Produces this:
object(variant)#3 (0) { }

-----Original Message-----
From: Stuart [mailto:stuttle(a)gmail.com]
Sent: Wednesday, August 26, 2009 8:47 AM
To: David Stoltz
Cc: Paul M Foster; php-general(a)lists.php.net
Subject: Re: [PHP] How to output a NULL field?

2009/8/26 David Stoltz <Dstoltz(a)shh.org>:
> Paul,
>
> This all started because when I try this:
>
> <?php echo $rs->Fields(22);?>
>
> It work fine, as long as there is a non-null value there, otherwise it
> produces an error.
>
> Also, I'm working with a Microsoft SQL 2000 database, not MySQL....not
> sure if that matters....
>
> But "echo $rs->Fields(22)" works perfectly for dumping values out of my
> $rs recordset...that is, unless the value is NULL is the database - then
> I get:
>
> Catchable fatal error: Object of class variant could not be converted to
> string in D:\Inetpub\wwwroot\evaluations\lookup2.php on line 176

That error indicates that you're not getting NULL since NULL is easily
converted to a string.

Try this...

$var = $rs->Fields(22);
var_dump($var);

...and tell us what you get.

-Stuart

--
http://stut.net/

> -----Original Message-----
> From: Paul M Foster [mailto:paulf(a)quillandmouse.com]
> Sent: Tuesday, August 25, 2009 4:39 PM
> To: php-general(a)lists.php.net
> Subject: Re: [PHP] How to output a NULL field?
>
> On Tue, Aug 25, 2009 at 02:00:04PM -0400, David Stoltz wrote:
>
>> $rs->Fields(22) equals a NULL in the database
>>
>> My Code:
>>
>> if(empty($rs->Fields(22))){
>>       $q4 = "";
>> }else{
>>       $q4 = $rs->Fields(22);
>> }
>>
>> Produces this error:
>> Fatal error: Can't use method return value in write context in
>> D:\Inetpub\wwwroot\evaluations\lookup2.php on line 32
>>
>> Line 32 is the "if" line...
>>
>> If I switch the code to (using is_null):
>> if(is_null($rs->Fields(22))){
>>       $q4 = "";
>> }else{
>>       $q4 = $rs->Fields(22);
>> }
>>
>> It produces this error:
>> Catchable fatal error: Object of class variant could not be converted
> to
>> string in D:\Inetpub\wwwroot\evaluations\lookup2.php on line 196
>>
>> Line 196 is: <?php echo $q4;?>
>>
>> What am I doing wrong?
>>
>> Thanks!
>
> Just a thought... do you really mean $rs->Fields(22) or do you mean
> $rs->Fields[22]? The former is a function call and the latter is an
> array variable.
>
> Paul
>
> --
> Paul M. Foster
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
From: hack988 hack988 on
My code for mssql
please enable the php's mssql extentions.
it used like so many mysql class that you can find by google
------------------------------------------------------------------------------------------------------------------
<?php
if(!defined('IN_WEB')) {
exit('Access Denied');
}
ini_set('mssql.datetimeconvert',0);//php>4.2.0 disable php's automatic
datetime convert
Class DB {
var $querynum=0;
var $mssql_link;
var $conn_link;
var $sp_link;
var $sp_name='';
var $error_stop=0;
var $show_error=0;
var $dbhost;
var $dbuser;
var $dbpw;
var $dbname;
var $pconnect;
var $var_type=array();
var $fields_name=array();
var $last_error_msg='';
var $phprunversion='';
function DB() {
//define type for sp
$this->var_type['sp_bit']=SQLBIT;
$this->var_type['sp_tinyint']=SQLINT1;
$this->var_type['sp_smallint']=SQLINT2;
$this->var_type['sp_int']=SQLINT4;
$this->var_type['sp_bigint']=SQLVARCHAR;
$this->var_type['sp_real']=SQLFLT4;
$this->var_type['sp_float']=SQLFLT8;
$this->var_type['sp_float-null']=SQLFLTN;
$this->var_type['sp_smallmoney']=SQLFLT8;
$this->var_type['sp_money']=SQLFLT8;
$this->var_type['sp_money-null']=SQLFLT8;
$this->var_type['sp_char']=SQLCHAR;
$this->var_type['sp_varchar']=SQLVARCHAR;
$this->var_type['sp_text']=SQLTEXT;
$this->var_type['sp_datetime']=SQLINT4;
$this->phprunversion=phpversion();
//end
}
/*>=php4.4.1,>=php5.1.1
a new paramate for if use newlink for connect,pconnect
*/
function rconnect($newlink=false){//2007.03.01 by hack988 fix phpversion check
if($this->phprunversion >= '4.4.1' && $this->phprunversion < '5.0.0'
|| $this->phprunversion >= '5.1.1'){
return $this->rconnect4p($newlink);
}else{
return $this->rconnect3p();
}
}
function rconnect3p(){
$this->mssql_link = $this->pconnect==0 ?
mssql_connect($this->dbhost, $this->dbuser, $this->dbpw) :
mssql_pconnect($this->dbhost, $this->dbuser, $this->dbpw);
if(!$this->mssql_link){
$this->halt("connect
($this->pconnect)MSSQL($this->dbhost,$this->dbuser)failed!");
return false;
}else{
$this->conn_link=$this->mssql_link;
if($this->dbname) {
if (!@$this->select_db($this->dbname,$this->conn_link)){
$this->halt('can not use database '.$this->dbname);
return false;
}else{
return true;
}
}else{
return true;
}
}
}
function rconnect4p($newlink=false){
$this->mssql_link = $this->pconnect==0 ?
mssql_connect($this->dbhost, $this->dbuser, $this->dbpw , $newlink) :
mssql_pconnect($this->dbhost, $this->dbuser, $this->dbpw, $newlink);
if(!$this->mssql_link){
$this->halt("reconect($this->pconnect)MSSQL($this->dbhost,$this->dbuser)failed");
return false;
}else{
$this->conn_link=$this->mssql_link;
if($this->dbname) {
if (!@$this->select_db($this->dbname,$this->conn_link)){
$this->halt('can not use database '.$this->dbname);
return false;
}else{
return true;
}
}else{
return true;
}
}
}

function connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect =
0,$auto_conn=0 ,$newlink=false) {
$this->dbhost=$dbhost;
$this->dbuser=$dbuser;
$this->dbpw=$dbpw;
$this->dbname=$dbname;
$this->pconnect=$pconnect;
if($auto_conn){
return $this->rconnect($newlink);
}else{
return true;
}
}

function close() {
if($this->conn_link){
$result=mssql_close($this->conn_link);
}else{
$result=true;
}
$this->mssql_link='';
$this->sp_link='';
$this->conn_link='';
return $result;
}

function select_db($dbname){
$this->mssql_link=mssql_select_db("[".$dbname."]");
return $this->mssql_link;
}

function query($SQL,$method='') {
if($method=='UNBUFFERED'){
mssql_query("SET NOCOUNT ON",$this->conn_link);
$this->mssql_link = mssql_query($SQL,$this->conn_link);
mssql_query("SET NOCOUNT OFF",$this->conn_link);
}else{
$this->mssql_link = mssql_query($SQL,$this->conn_link);
}

if (!$this->mssql_link) $this->halt('SQL query error: ' . $SQL);
$this->querynum++;
return $this->mssql_link;
}

function get_one($sql,$prefix=""){
$query=$this->query($sql,'UNBUFFERED');
if(strlen($prefix)>0){
$this->get_fields_name();
$rs=$this->fetch_duplicate_array($query);
}else{
$rs = $this->fetch_array($query, MSSQL_ASSOC);
}
return $rs;
}

function seek($num,$link=''){
$link = empty($link) ? $this->mssql_link : $link;
return mssql_data_seek($link,$num);
}

function fetch_array($query, $result_type = MSSQL_ASSOC) {
return mssql_fetch_array($query, $result_type);
}

function fetch_duplicate_array($query, $prefix="dup_") {
if(count($this->fields_name)<1) return false;
$fields=$this->fetch_array($query, MYSQL_NUM);
if(!$fields) return false;
$reternfields=array();
foreach($fields AS $key=>$value){
if(isset($reternfields[$this->fields_name[$key]]))
$reternfields[$prefix.$this->fields_name[$key]]=$value;
else
$reternfields[$this->fields_name[$key]]=$fields[$key];
}
return $reternfields;
}

function affected_rows($link='') {
$link= empty($link) ? $this->conn_link :$link;
return mssql_rows_affected($link);
}

function get_fields_name($link=''){
$link= empty($link) ? $this->mssql_link :$link;
$fieldscount=$this->num_fields($link);
for($i=0;$i<$fieldscount;$i++){
$field[$i]=mssql_field_name($this->mssql_link,$i);
}
$this->fields_name=$field;
}

function num_rows($link='') {
$link = empty($link) ? $this->mssql_link : $link;
$rows = mssql_num_rows($link);
return $rows;
}

function num_fields($query) {
return mssql_num_fields($query);
}

function sp_init($sp_name){
$this->sp_link=mssql_init($sp_name,$this->conn_link);
!$this->sp_link && $this->halt('Init PROCEDURE Failed :' . $sp_name);
$this->sp_name=$sp_name;
return $this->sp_link;
}

function sp_bind($parameter,&$var,$type,$if_out=false,$if_null=false){
if(!$this->sp_link){
$this->halt('Can not bind var for PROCEDURE' . $parameter);
return false;
}else{
if(!mssql_bind($this->sp_link,$parameter,$var,$this->var_type[$type],$if_out,$if_null)){
$this->halt('PROCEDURE var binding failed' .
$parameter.$this->var_type[$type]);
return false;
}else{
return true;
}
}
}

function sp_execute($skipout,$sp_link=''){
$this->sp_link=$sp_link? $sp_link : $this->sp_link;
$this->mssql_link=mssql_execute($this->sp_link,$skipout);
if (!$this->mssql_link) $this->halt('exec PROCEDURE failed: ' . $SQL);
$this->querynum++;
return $this->mssql_link;
}

function sp_free_statement($sp_link=''){
$this->sp_link=$sp_link? $sp_link : $this->sp_link;
if(!mssql_free_statement($this->sp_link)){
$this->halt('free memory failed (PROCEDURE)£º'.$this->sp_name);
return false;
}else{
$this->sp_link='';
return true;
}
}

function free_result($query) {
return mssql_free_result($query);
}

function insert_id() {
$rs = $this->get_one("SELECT @@IDENTITY AS [insertid]");
if($rs)
return $rs['insertid'];
else
return false;
}

function halt($msg='') {
if($this->show_error){
echo date("Y-m-d H:i:s",time())."<br/>\n";
echo $msg."<br/>\n";
$this->last_error_msg="SQL Server
Msg:".nl2br(mssql_get_last_message())."<br/>\n";
echo $this->last_error_msg;
$this->last_error_msg = $msg.$this->last_error_msg;
}
$this->error_stop && exit;
}
}
?>