So as many already know the $wpdb class in Wordpress can't deal with inserts and updates involving NULL values. This is a major headache, and there doesn't seem to be a good way around it.
So I'm wondering what the next best way is to insert/update tables in Wordpress? I liked WPDB cause of the prepared statements, so I was hoping to have something where I could continue with that. Unfortunately, no PDO support?
Any suggestions?
Here is a solution for Null values, just create a new class by extending the wpdb class and use two new insert and update functions and to do this you can create a db.php file in your wp-content directory and drop this content in the file.
Not tested but you may give it a try.
Related
I'm using the latest version of Laravel to connect to multiple data sources. One of those sources is an old Oracle database that contains a lot of white space thanks to some ancient software requiring string length. Despite this constraint, the fields have since been edited by new software with different requirements, and the columns are all varying length (and thus unknown).
On account of this, I need to edit the SQL that accesses it in order to wrap some pieces of the queries in trim()s. For instance,
$customer = Customer::whereRaw("RTRIM(\"ID\") = TO_CHAR($id)")->get();
I'd like to be able to merely call the find method:
$customer = Customer::find($id)
This is just one example. Pretty much all of the default functions are broken because the queries need some kind of trim prepended to them. I understand how I can affect the dynamic portion of the query, but I need to edit the column ahead of that. Sorry if this is a dumb question and I've just missed something in the documentation.
The easiest way to do this is to create a class that extends Eloquent, with the function for the query. Then modify all of your models to extend the new class instead of Eloquent. This way the models will have all the functionality of Eloquent as well as the functions you create.
I was wondering how does you guys write your classes when they need tables in the database to function or some other things to do before it functions?
Do you use a method to insert the tables in the database or do you write the class to require some columns in a table which can be define at some settings variable?
Whilst the answer to this question is a matter of opinion, I would generally create them in the class constructor (checking whether they exist first or not), however I would allow the user to specify their own names and prefixes to avoid clashes and data loss within their database.
The reason being is that you have total control over how the tables are constructed as opposed to trusting the user to generate them correctly.
This is assuming your class will be used by other people, if it is solely for your own use it is safe enough to assume you are able to build the tables yourself and could remove that overhead from the class.
It also depends what your class is for, if it is a plugin then you should definitely create these for the user so they don't have to touch the underlying code, following any of the database naming conventions of the system your plugin is integrating with.
This is just my personal style and is by no means a standard or necessarily the optimum way.
I don't want to use drupal db_ rules.. I'd rather use my own pdo techniques for database queries and updates etc.. But I've managed to be able to get pdo to work, the only issue is that I have to use a connect on every custom page, some users in my community can edit the pages and will be able to see the connection information.. require & require_once do not seem to work.
Has anyone else had this issue and knows how to avoid it?
You could create your own module and use the init hook:
https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_init/7
function yourmodulename_init() {
// This code will be executed on every page
}
However you should try to use the drupal database mechanics as it comes with a lot of best practices and powerful tools. E.g. views integration:
https://api.drupal.org/api/views/views.api.php/function/hook_views_data/7
In Drupal 7, Database::getConnection allows you to retrieve DatabaseConnection objects for all configured databases. And DatabaseConnection extends PDO so if your really want to bypass Drupal's Database API (which is itself based on PDO and either re-use or extends PDO classes all over the place), you can use it as you would use your own PDO object.
But if user of your community are able to edit your custom page, I'm guessing that your are actually embedding PHP code inside Drupal nodes using the PHP filter. Which is itself a very bad idea (worse than ignoring Drupal's Database API). You should instead consider writing custom modules tailored for your needs.
I'm pretty new to FuelPHP.
My first question is what is the best place to put custom functions? Right now, I just made a new file, func.php, inside the app/classes folder and access it like $func = new Func; $func->function_name(); - I don't think this is the best way to do it.
Secondly, for something I'm making, I'll be needing a lot of custom queries. Normally, I'd just use PDO's prepared statements and execute them, but I'm unsure how I'd go about sanitizing user input with DB::query(), without having to sanitize each individual variable.
Autoloading function is not possible with PHP, so many people use classes, like you did... More information is available in this SO anwser: Autoloader for functions
For prepared statements, you got that: http://docs.fuelphp.com/classes/database/usage.html#binding
You still can use plain old PDO if you want, it's totally possible.
So I have been developing plugins that create interact and delete to a database by using the get_option functions.
I have now seen some tutorials showing how to use the global $wpdb way of getting values from the database.
What is the difference between the two, and is one better than the other?
For storing plugin options or light weight data related to posts, get_option(), get_post_meta(), and their related functions are ideal. For relational database activity, $wpdb is best choice. Here's why:
$wpdb is a class based on the ezSQL PHP class for interacting with the database. Some features include:
1) provides SQL injection protection using the $wpdb->prepare(), $wpdb->insert(), and $wpdb->update() methods. get_option() is a helper function that allows you to do a Key => Value pair.
2) $wpdb is easy to use. It can return record sets in various forms: $wpdb->get_results($sql, ARRAY_A) an Array or Associative Arrays containing the returned rows with the column names being the keys. $wpdb->get_results($sql) would return an array of object with the column name as properties of the object. $wpdb->get_var($sql) would return a scalar result (the first column of the first row of the data set from the query). $wpdb->get_row($sql) would return a single row as an object.
3) $wpdb allows you to interact with any table in the database, even performing free form queries using $wpdb->query($sql)
4) WordPress will likely insure that your interactions with $wpdb will not need to change if they add support for databases other than MySQL. The original ezSQL class was intended to provide some cross database support.
So, if you're needing to deal with data in a relational way, $wpdb is really an excellent choice for WordPress.
get_option() and get_post_meta() provide an easy way of dealing with small amounts of data, either relating to a specific post in the case of get_post_meta() or as a Key => Value pair with get_option().
One of the nice things about these is that you can save a serialized array or object and get that data back out as either an array or object. This gives you a very easy way to deal with fields of data as if you had a database table. However, this doesn't work well if you need to relate data between tables or do summing, counting, or other database calculations on the serialized data. I those cases, a fully fledged table and $wpdb would better serve.
Using WordPress helper functions (not limited to get_option()) will ensure your plug in to be forward compaitable when newer version of WordPress made changes that may potentially effect your code.
You are recommanded to understand and use their helpers before considering coding your own.
The global $wpdb variable is more powerful than the get_option() function as it allows you to manipulate and retrieve data from any table in wordpress, including all plugin tables. This gives you more power than simply gaining access to the options table.
WPDB reference
You should use get_option() when you are specifically retrieving an option from the options table, used for plugin options. Other variations of this function are add_option, update_option, and delete_option. All of the related functions should be used specifically for plugin options.
I've used both before. If you can use the helper functions, you should do so. If you need to do something very custom you can use the global. I only use the global if I just must write my own query
I use both in my development.
get_option() is far easier for static or single info bites
$wpdb is better for storing multi-dimensional information
The primary difference is that for grab-and-go information (like license keys, expiration dates, and static info) get_option() is really handy. It's trim, fast, and really easy to use.
But, I develop plugins that manage user data and form submissions, and in cases like this get_option() doesn't offer the versatility I need without writing a lot of compound arrays or trying to track multiple options. For multi-dimensional information, or for cross-referencing related data entries for a plugin, $wpdb is much better - you can structure your own tables and sort/organize how you like.
:)