/    Sign up×
Community /Pin to ProfileBookmark

execute() function

Please, someone explain what this function is!

I can’t figure out [U][B]the lines of code[/B][/U] inside this function, as it is used in the following script (last line):

[code=php]
$fields = array(‘first_name1′,’last_name1′,’tel_number1’);
$placeholders = array();
$values = array();
foreach($fields as $field) {
$placeholders[] = ‘?’; // One placeholder per field;
$values[] = $_POST[$field]; // Assume the data is coming from a form;
}
$query = “INSERT INTO table ( ‘implode(‘,’, $fields) ‘)
VALUES (‘ implode (‘,’, $placeholders) ‘ )”;
$query ->execute($values);
[/code]

Thank you.

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@NogDogDec 28.2007 — I don't know enough about your code to tell you for sure what is needed, but there is some obvious confusion here. In the second-to-last line you assign a SQL string to the variable [b]$query[/b]. But in the next line you reference [b]$query[/b] as though it is an object, call its member method [b]execute()[/b] (the "->" operator means that [b]execute()[/b] is a member of object [b]$query[/b].)

Presumably, [b]$query[/b] would have been defined earlier as an instance of some class via the [b]new[/b] keyword. If so, then you need to use a different variable name for the query string. What I suspect you want is:
[code=php]
$sql = "INSERT INTO table ( 'implode(',', $fields) ')
VALUES (' implode (',', $placeholders) ' )";
$query->execute($sql);
[/code]

But [i]caveat emptor[/i], as this is just a best guess based on incomplete data.
Copy linkTweet thisAlerts:
@vsspDec 28.2007 — I think this will add some class file top of the page. So send me the include file then only explain the code
Copy linkTweet thisAlerts:
@andkhlauthorDec 28.2007 — Thank you for your responses, guys! I am trying to adapt the code for building queries programmatically from the book "PHP Cookbook" (2-nd edition, page 315). Nowhere in the book does the author explain what the [B]execute()[/B] function is. Same goes for [B]prepare()[/B]. [B]They are both in the original code [/B]as follows:
[code=php]
$fields = array('symbol','planet','element');
$placeholders = array();
$values = array();
foreach($fields as $field) {
$placeholders[] = '?'; // One placeholder per field;
$values[] = $_POST[$field]; // Assume the data is comeing from a form;
}
$st = $db->prepare("INSERT INTO table ( 'implode(',', $fields) ')
VALUES (' implode (',', $placeholders) ' )");
$st ->execute($values); // Execute the query
[/code]

That was the [U]complete[/U] code above. I undestand every line except for the last line. I know that each $field must be coupled with the corresponding $value to be inserted into the database, however, I don't undestand how it's done in the example above, because I can't figure out the lines of code for the [B]execute()[/B] function.

Please don't tell me to contact the author of this because that way I will never get anything done! Thank you for your understanding and help.

P.S. If this doesn't work, is there any other way to accomplish what I am doing here?
Copy linkTweet thisAlerts:
@Ware_WorkDec 28.2007 — The last line is executing the prepared sql statement passing the values to put into the placeholders. This is the correct way to do this to avoid SQL Injection issues.

Your original code has an issue because you lost the database object. you can replace $st with $query if you would like but you need to do the $db->prepare() to get a sql statement object which can be executed.
Copy linkTweet thisAlerts:
@andkhlauthorDec 28.2007 — The last line is executing the prepared sql statement passing the values to put into the placeholders. [/QUOTE]

Yes, but what [I][B]is[/B][/I] the mechanism? How do I pass the values into the placehoders? Thanks! ? ?
Copy linkTweet thisAlerts:
@NogDogDec 28.2007 — I am assuming that [b]$db[/b] is a mysqli object, either the result of a [b]mysqli_connect()[/b] or a [b]new mysqli()[/b] call. If so, see the following manual pages for explanations and examples of using this interface to the database:

http://www.php.net/mysqli_prepare

http://www.php.net/mysqli_bind_param

http://www.php.net/mysqli_execute
×

Success!

Help @andkhl spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...