4 Replies - 1345 Views - Last Post: 12 June 2011 - 01:09 AM Rate Topic: -----

#1 E_Geek  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 45
  • View blog
  • Posts: 236
  • Joined: 20-February 11

MySQLI & Prepared Statements Through Function

Posted 12 June 2011 - 12:39 AM

I've recently (yesterday) dived into the world of mysqli and prepared statements, and i've come up with a problem.
I have wrote a function for checking wether a record exists in a table, to save writing this every 5 minutes in my code. I also want to write functions for inserting and deleting items from a table, and it may be because its 8am on a sunday, or maybe i just really can't do it.

Anyway, i was going on the lines of:
function insert_record($arrayofcolumnnames, $arrayofvalues, $table, $mysqli){
    $stmt = $db->prep_stmt(" {Statement Here} "); 

    $stmt->execute( ) or die ("Could not execute statement");
    $stmt->close();
}



This is where i'm at a loss, How would i go about transferring the array of column names and values to the prepared statement, is there a way to count the array fields and insert the ?'s based on this? If so, how please :) Although the fields are also coming from an array ... Would i use a foreach in here,
$arrayofcolumnnames[i] = ?

Or am i completely at a loss here?

Is This A Good Question/Topic? 0
  • +

Replies To: MySQLI & Prepared Statements Through Function

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3397
  • View blog
  • Posts: 9,607
  • Joined: 08-June 10

Re: MySQLI & Prepared Statements Through Function

Posted 12 June 2011 - 12:49 AM

for the array of column names you can use implode() to convert it into a string, but I have no idea how to feed the values to bind_param()*. … I could tell you how to do that in PDO, though.



PS. you have forgotten to use the bind_param() method for the values.

* - it would be easy in Javascript …

This post has been edited by Dormilich: 12 June 2011 - 12:52 AM

Was This Post Helpful? 1
  • +
  • -

#3 E_Geek  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 45
  • View blog
  • Posts: 236
  • Joined: 20-February 11

Re: MySQLI & Prepared Statements Through Function

Posted 12 June 2011 - 01:01 AM

PDO = php data objects?
I was considering that instead of mysqli, what are the differences?

I wouldn't mind learning PDO if it's going to be an advantage, and especially if it can do what i'm trying to do :)
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3397
  • View blog
  • Posts: 9,607
  • Joined: 08-June 10

Re: MySQLI & Prepared Statements Through Function

Posted 12 June 2011 - 01:04 AM

- its not limited to MySQL
- Prepared Statements are as comfortable to use (when it comes to fetching data) as regular queries
- it has a better error handling (you can use Exceptions)
- PDO implements Iterators
- we have a lot of PDO tutorials
Was This Post Helpful? 1
  • +
  • -

#5 E_Geek  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 45
  • View blog
  • Posts: 236
  • Joined: 20-February 11

Re: MySQLI & Prepared Statements Through Function

Posted 12 June 2011 - 01:09 AM

I'll start reading up on pdo now, then when i'm comfortable enough to try this again i'll come back for help, unless i can figure it out, Thanks :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1