Jump to content

Mysqli->connect_errno


niche

Recommended Posts

I'm working to better understand references on the web. So, I have questions about this page: http://us.php.net/manual/en/mysqli.connect-errno.php.Specifically:<?php$mysqli = @new mysqli('localhost', 'fake_user', 'my_password', 'my_db');if ($mysqli->connect_errno) { die('Connect Error: ' . $mysqli->connect_errno);}?>Why is the the mysqli->connect_errno command demonstrated as $mysqli->connect_errno?How can you test a condition with an if statement without an equal sign?What's the difference between $mysqli->connect_errno and $mysqli->connect_errno()?Thanks

Link to comment
Share on other sites

There are 2 styles being demonstrated, object-oriented and procedural. In the object-oriented style, connect_errno is not a command or a function, it's a property of the mysqli object. In the procedural style it is a function.

How can you test a condition with an if statement without an equal sign?
An if statement evaluates the condition. If the condition evaluates to a "truthy" value, then the if statement executes its code block. These values are considered false:
the boolean FALSE itself the integer 0 (zero) the float 0.0 (zero) the empty string, and the string "0" an array with zero elements an object with zero member variables (PHP 4 only) the special type NULL (including unset variables) SimpleXML objects created from empty tags
All other values are considered to be "true". Writing a condition like this:if ($condition)is the same as writing this:if ($condition == true)just like these are the same:if (!$condition)if ($condition == false)You can see what the return value is for the connect_errno property or the mysqli_connect_errno function:
An error code value for the last call to mysqli_connect(), if it failed. zero means no error occurred.
So it will be 0 if no error occured. 0 is considered a false value. So this statement:
if ($mysqli->connect_errno) {die('Connect Error: ' . $mysqli->connect_errno);}

will only execute the die statement if the error number is non-zero (true), i.e. if an error happened.

Link to comment
Share on other sites

You said: There are 2 styles being demonstrated, object-oriented and procedural. In the object-oriented style, connect_errno is not a command or a function, it's a property of the mysqli object. In the procedural style it is a function.Object Oriented Programming (OOP) has been a challanging concept for me, but I've heard it explained using classes.Is OOP and class in CSS the same basic concept?Thank-you

Link to comment
Share on other sites

You can use the === operator to find out if two things that equate to FALSE are really the same. For example, if you are doing some multiplication and one operand equals 0, then the result of the multiplication will be 0, but that isn't the same as the operation being false for other reasons: for example, if $x = "3x" and $y = " ", then PHP automatically converts the strings to their numeric value if you try to multiply $x * $y, which will result in a false outcome. If you want to run some code depending on a value of 0 but not FALSE, you might write this:if(($x * $y) === 0) {then }else if(($x * $y) === FALSE) {then a variable isn't set}

Link to comment
Share on other sites

strpos works the same way, strpos will return the position of a substring inside a larger string. If it is not found, it will return false. If it returns 0 that means the string starts at the beginning (at position 0). So if you're using strpos to check if something exists you need to compare type in addition to value. e.g.:if (strpos($text, $search) === false)

Is OOP and class in CSS the same basic concept?
No, CSS doesn't have anything to do with programming. CSS is not a programming language, it's just used for formatting.
Link to comment
Share on other sites

I'm getting it.So, getting back to the first thread, I have a few more questions about:<?php$mysqli = @new mysqli('localhost', 'fake_user', 'my_password', 'my_db');if ($mysqli->connect_errno) {die('Connect Error: ' . $mysqli->connect_errno);}?>$mysqli is simply a variable in this example right?What is @new mysqli?What is $mysqli->connect_errno (I would say it's a property if it wasn't for the $)?Thank-you

Link to comment
Share on other sites

In object oriented programming, you define an instance of an object. So, $mysqli is a new connect object, which has methods. In procedural style, you would simply define $mysqli = mysqli_connect(blah blah blah) and then you use different functions such as mysqli_fetch_array, etc. The @ sign silences any errors that might come from the statement. It's not the best error handling, but it will stop people seeing errors from that statement.edit: I realise that the first part of this post retreads old ground, but the last part is all new!

Link to comment
Share on other sites

Right, the new keyword is used for creating a new instance of a class. An instance of a class is referred to as an object. So there's a class called the mysqli class, and this statement:$mysqli = @new mysqli('localhost', 'fake_user', 'my_password', 'my_db');creates a new instance of the mysqli class. So mysqli is a general class, and $mysqli is a specific object of the mysqli class. You can check the manual for the mysqli class here:http://www.php.net/manual/en/book.mysqli.phpThat shows references for the mysqli class, the mysqli_stmt class, the mysqli_result class, and the mysqli_driver class.

What is $mysqli->connect_errno (I would say it's a property if it wasn't for the $)?
That's a reference to the connect_errno property of the $mysqli object. Think of a class like a blueprint, and an object like a specific building. You can make several similar buildings from the same blueprint, but each building is still a separate object.
Link to comment
Share on other sites

I'm obviously going to have to read-up on OOP and PHP/MYSQL.In the meantime, with regards to that 1st line: Why $mysqli instead of $abc?Where does "new mysqli" come from? I don't recognize it as a property of the object (no {}) or a function? Or, should I and if so how/why?Thanks for your help.

Link to comment
Share on other sites

Why $mysqli instead of $abc?
The variable name is arbitrary, it doesn't matter what it is.
Where does "new mysqli" come from?
Like I said, the new keyword lets you create a new instance of a class. New isn't a property or a function, it's an operator like +, -, !, &&, etc. This is the general syntax:$variable_name = new class_name([arguments]);http://www.php.net/manual/en/language.oop5....oop5.basic.new
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...