OO MySQLi help

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
me!
Forum Contributor
Posts: 133
Joined: Sat Nov 04, 2006 8:45 pm

OO MySQLi help

Post by me! »

I am trying to convert from some old MySQL functions to MySQLi

From what I can tell this should work but it is not... ?

Code: Select all

// db connection include is working, but not shown.

// Generate a customer list
$sql='SELECT type, first_name, last_name, city, id FROM customers WHERE type = ?';
$type = 'R';
 
/* Prepare statement */
$stmt = $db->prepare($sql);
if($stmt === false) {
  trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);
}
 
/* Bind parameters. TYpes: s = string, i = integer, d = double,  b = blob */
$stmt->bind_param('s',$type);
 
$stmt->bind_result($type, $first_name, $last_name, $city, $id);
while ($stmt->fetch()) {
    echo $last_name . ' ' . $first_name . ' - '. $city .'<br>';
    }

$db->close();
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: OO MySQLi help

Post by requinix »

You never actually executed the query.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: OO MySQLi help

Post by Celauran »

Also, if you're just getting started on this, do yourself a favour and use PDO instead of MySQLi. You'll thank me later.
me!
Forum Contributor
Posts: 133
Joined: Sat Nov 04, 2006 8:45 pm

Re: OO MySQLi help

Post by me! »

Thank You!

I added

Code: Select all

 $stmt->execute();
and it now works :D
Not sure if it's placement is proper, but it is working.

In regards to PDO, I was going to change it all to that but I hit problems (me not knowing it) and was hoping MySQLi was more like the existing MySQL commands. However since working with it a little it looks like they are not all that different in the way you code things if I am going to use object orientated methods? I know the functions are different but that just requires using Google.


New code with the execute:

Code: Select all

// Generate a customer list
$sql='SELECT type, first_name, last_name, city, id FROM customers WHERE type = ?';
$type = 'R';
 
/* Prepare statement */
$stmt = $db->prepare($sql);
if($stmt === false) {
  trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);
}
 
/* Bind parameters. TYpes: s = string, i = integer, d = double,  b = blob */
$stmt->bind_param('s',$type);

$stmt->bind_result($type, $first_name, $last_name, $city, $id);
$stmt->execute();
while ($stmt->fetch()) {
    echo $last_name . ' ' . $first_name . ' - '. $city .'<br>';
    }

$db->close();
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: OO MySQLi help

Post by Celauran »

Named placeholders and not having to bind results is going to save you a lot of hair pulling. PDO and MySQLi are otherwise quite similar, and the examples in the PDO book should be plenty to get you started.
me!
Forum Contributor
Posts: 133
Joined: Sat Nov 04, 2006 8:45 pm

Re: OO MySQLi help

Post by me! »

Thank You
Post Reply