PHP/MySQL

From dbawiki
Revision as of 23:15, 31 January 2015 by Stuart (talk | contribs)
Jump to: navigation, search

Excellent tutorial on Object Oriented PHP

www.killerphp.com

Using prepared statements to avoid SQL injection

Using this method of writing SQL removes the necessity of attempting to clean the input with mysql_real_escape_string()

$dbPreparedStatement = $db->prepare('INSERT INTO table (postId, htmlcontent) VALUES (:postid, :htmlcontent)');
$dbPreparedStatement->bindParam(':postid', $userId, PDO::PARAM_INT);
$dbPreparedStatement->bindParam(':htmlcontent', $yourHtmlData, PDO::PARAM_STR);
$dbPreparedStatement->execute();

Fill your boots on PDO here

CSV tables - equivalent of External tables in Oracle

Run a shell script with an html button

<?php
if ($_GET['run']) {
  # This code will run if ?run=true is set.
  exec("/path/to/name.sh");
}
?>
<!-- This link will add ?run=true to your URL, myfilename.php?run=true -->
<button type="button" onclick="?run=true">Click Me!</button>

Start MySQL at boot time

To start mysqld at boot time you have to copy support-files/mysql.server to the right place for your system

Set a root password after installing MySQL

/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h <hostname> password 'new-password'

Alternatively you can run:

/usr/bin/mysql_secure_installation

which will also give you the option of removing the test databases and anonymous user created by default. This is strongly recommended for production servers.

Start the MySQL daemon

cd /usr ; /usr/bin/mysqld_safe &

You can test the MySQL daemon with:

cd /usr/mysql-test ; perl mysql-test-run.pl

Reset a forgotten MySQL root password