2 Replies - 335 Views - Last Post: 26 June 2012 - 04:53 PM Rate Topic: -----

#1 aklo  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 229
  • Joined: 23-January 09

About mysqli->connect_errno and if statement

Posted 26 June 2012 - 07:34 AM

Basically I have this connection function and it works but i'm just confused why

if($mysqli->connect_errno) works the way it is working now?

From what I know, $mysqli->connect_errno will return an integer and the last time I checked, you cannot

if(string){
do something
}else{
do another thing
}

isn't it boolean only intead of int...

	public function conn(){
		$mysqli = new mysqli(self::HOST, self::USERNAME, self::PASSWORD, self::DB_NAME);	
		if($mysqli->connect_errno){
		
			echo $mysqli->connect_errno;	
		}else{
		
			echo "Connection success";
		}
	}


This post has been edited by aklo: 26 June 2012 - 07:37 AM


Is This A Good Question/Topic? 0
  • +

Replies To: About mysqli->connect_errno and if statement

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 2889
  • View blog
  • Posts: 7,535
  • Joined: 08-June 10

Re: About mysqli->connect_errno and if statement

Posted 26 June 2012 - 07:38 AM

you forget that PHP converts the variable values to the appropriate data type, if necessary. and within integers, 0 equals false.

btw. that is one reason I prefer PDO, if there is a connection error, PDO will emit an Exception. hence I do not need to do any conditional tests, whether the connection failed or not (with the appropriate setting, PDO emits an Exception on any error it encounters).
Was This Post Helpful? 3
  • +
  • -

#3 aklo  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 229
  • Joined: 23-January 09

Re: About mysqli->connect_errno and if statement

Posted 26 June 2012 - 04:53 PM

thanks! how could I have missed that.

Yup i've read about pdo too but I guess i'll just stick with 1 and try pdo the next time...i'm a bit rusty on this haven't been touching php since 2-3 years ago.

Edit on second thoughts, i'll learn PDO. Seems like the better choice to use as it works with any db.

This post has been edited by aklo: 26 June 2012 - 04:55 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1