Page 1 of 1

Noob question re: oo vs procedural concepts

Posted: Fri Jan 02, 2009 11:36 am
by bartolo
Hi All,

I have included 2 sets of code copied right out of the PHP manual for the mysqli extension.
http://us2.php.net/manual/de/mysqli.connect.php

They both do the same thing. The difference is that one is oo styled and the other is procedural in nature.

I cannot get the oo code to work for me and I would really like to understand why.

Object Oriented version: this does NOT work for me.

Code: Select all

<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
 
/* check connection */
if ($mysqli->connect_error) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
 
printf("Host information: %s\n", $mysqli->host_info);
 
/* close connection */
$mysqli->close();
?>
Procedural version: this DOES work for me.

Code: Select all

<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
 
/* check connection */
if (!$link) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
 
printf("Host information: %s\n", mysqli_get_host_info($link));
 
/* close connection */
mysqli_close($link);
?>

I am NOT looking for a workaround, I have that with the procedural code above.
Understanding why this particular oo code does not work for me would greatly help with my learning curve.

Note: I have been able to use other mysqli object members, so I know that mysqli extension is functioning.

Any help would be greatly appreciated.

Thanks in advance,

bartolo

Re: Noob question re: oo vs procedural concepts

Posted: Fri Jan 02, 2009 12:04 pm
by Eran
You need to elaborate on what exactly is not working for you. What errors message are you getting, where does it fail etc.

Re: Noob question re: oo vs procedural concepts

Posted: Fri Jan 02, 2009 12:38 pm
by bartolo
pytrin

Specifically, mysqli->errno and mysqli->error seemingly never return anything.

If I force an error, with a bad password, neither of the methods respond. I do get error messages from PHP, because I have error_reporting on full for development proposes. But I don't get any error message from mysqli.

bartolo

Re: Noob question re: oo vs procedural concepts

Posted: Fri Jan 02, 2009 12:45 pm
by sergio-pro
Hi

On the page you are reffering to, there is an explanation of how to get connection error.
Just read the user comments below the official docs.

http://us2.php.net/manual/de/mysqli.connect.php#83419

Re: Noob question re: oo vs procedural concepts

Posted: Fri Jan 02, 2009 12:50 pm
by Eran
I guess the code you use is taken from the example in the manual. For some reason, that example is incorrect - mysqli will fail if not given the correct credentials in the constructor (throws a PHP warning), and will not process any futher mysqli functions or members (such as connect_error).

If error_reporting is turned on for you, you should have noticed this by now.