Escaping / encoding data before insert into a database?

You escape on output, what I suspect here is a confusion between escaping sanitizing and validating Sanitise when data arrives. This strips out stuff that shouldn’t be there, e.g. upper case letters in a lower case string, words and letters in a phone number, trailing spaces etc. Sanitising cleans data common sanitising functions include trim, … Read more

Check if row exists before inserting

There are several problems with your function. For example, $tablename points to a table that does not exist. And you don’t need it anyway. If you are trying to create a new user if that email does not exist then try this: function register_user() { global $wpdb; $name = $_POST[‘name’]; $email = $_POST[’email’]; $password = … Read more

greatest() function returns undefined property

I think you want to use get_var(), not get_row() – looking at the documentation, MySQL’s GREATEST just returns a single value. $results will then just be the value of the greatest (be it up or down). If you want the greatest up and the greatest down, use; $wpdb->get_row(“SELECT GREATEST(up) AS ‘up’, GREATEST(down) AS ‘down’ …”)

Query from a different database than the default

I don’t know why the mysql_connect isn’t working inside WordPress. But an easier way of doing it would be to use the $wpdb class. Try replacing your code with this: $second_db = new $wpdb( ‘user’, ‘pass’, ‘dbname’, ‘host’ ); $q = $second_db->get_results( ‘SELECT * FROM vars’ ); var_dump( $q ); and see if you get … Read more

Should I remove transients from the wp_options table?

This is not an amount that is likely to cause performance issues. However if you are concerned about leftover transients it is worth looking into if any code you are using is consistently “leaking” them (creating transients that are never removed). See Are transients garbage collected? for relevant discussion and some code snippets.

Single database for multiple instances

It’s considered best practice to have two separate databases (one for production, one for development) then migrate the database as detailed here. If you, for example, install a database-changing plugin on one but not the other, you risk breaking one of your sites until you duplicate those changes over. If you’re working late tweaking code, … Read more

Add action on delete comments?

Look at the function wp_delete_comment(). It fires an action before the comment is deleted: /** * Fires immediately before a comment is deleted from the database. * * @since 1.2.0 * * @param int $comment_id The comment ID. */ do_action( ‘delete_comment’, $comment_id ); … and one after deletion: /** * Fires immediately after a comment … Read more