I have two PHP/SQL functions and I am wondering how I would minify them. I’d like to run the 3 insert commands as a single $import rather than three, but I am not sure how to minify it.
$import9="INSERT into wp_postmeta (meta_id,post_id,meta_key,meta_value) values(',','$data[37]','lower_electric_costs','$data[5]')";
$import10="INSERT into wp_postmeta (meta_id,post_id,meta_key,meta_value) values(',','$data[37]','cheapest_green_electric','$data[6]')";
$import11="INSERT into wp_postmeta (meta_id,post_id,meta_key,meta_value) values(',','$data[37]','contract_type','$data[11]')";
Its the same thing with my PRINT results – how would I minify this down?
mysql_query($import9) or die("mysql_error()");
print $import9."<br>";
mysql_query($import10) or die("mysql_error()");
print $import10."<br>";
mysql_query($import11) or die("mysql_error()");
print $import11."<br>";
I am still learning PHP/SQL and learning how to minify my code is an important part of it I think. If someone can just how me an example of what a minified version would look like, then I can probably take it from there.
Thanks
You could combine it into a single query as follows:
However, you should use
mysqliinstead ofmysqlfunctions, asmysqlfunctions are deprecated.It is also advised that you do not use INSERT queries with
mysqli_querybut rather use Prepared Statements for security reasons. You should check out PDO and Prepared Statements.First, create a PDO instance.
With a prepared statement, you would
prepareyour statement with the server:With an array like:
Next, you’d issue a series of execute statements:
Finally, close the statement so that more statements can be executed: