Noob question re: oo vs procedural concepts

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bartolo
Forum Newbie
Posts: 3
Joined: Thu Jan 01, 2009 3:17 pm

Noob question re: oo vs procedural concepts

Post 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
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Noob question re: oo vs procedural concepts

Post 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.
bartolo
Forum Newbie
Posts: 3
Joined: Thu Jan 01, 2009 3:17 pm

Re: Noob question re: oo vs procedural concepts

Post 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
User avatar
sergio-pro
Forum Commoner
Posts: 88
Joined: Sat Dec 27, 2008 12:26 pm

Re: Noob question re: oo vs procedural concepts

Post 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
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Noob question re: oo vs procedural concepts

Post 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.
Post Reply