MySQL Archive

  • I often forget the exact syntax of things like this… So here’s an example… $query1 = 'SELECT * FROM `'.$dbName.'`.`users` WHERE email NOT IN (SELECT email FROM `'.$setupDBName.'`.`users`)';

    SELECT inside a SELECT

    I often forget the exact syntax of things like this… So here’s an example… $query1 = 'SELECT * FROM `'.$dbName.'`.`users` WHERE email NOT IN (SELECT email FROM `'.$setupDBName.'`.`users`)';

    Continue Reading...

  • The following SQL query causes an error SELECT vote_vo_ip, COUNT(*) as times FROM `vote` LEFT OUTER JOIN `voter` ON vote.vote_vo_id = voter.vo_id WHERE vote_co_id = 2 AND times > 1...

    #1054 – Unknown column in ‘where clause’

    The following SQL query causes an error SELECT vote_vo_ip, COUNT(*) as times FROM `vote` LEFT OUTER JOIN `voter` ON vote.vote_vo_id = voter.vo_id WHERE vote_co_id = 2 AND times > 1...

    Continue Reading...

  • This is something you’re likely to do several times in several different scenarios. The common one for me is generating reports from a database. I tend to build a $_SESSION...

    Export to CSV file

    This is something you’re likely to do several times in several different scenarios. The common one for me is generating reports from a database. I tend to build a $_SESSION...

    Continue Reading...

  • I love the ‘LIKE’ SQL command for searching within phpMyAdmin, but to do the job properly on a page, you really need to know how to use ‘Full-text Searching’… This...

    Full Text Search in MySQL Database

    I love the ‘LIKE’ SQL command for searching within phpMyAdmin, but to do the job properly on a page, you really need to know how to use ‘Full-text Searching’… This...

    Continue Reading...

  • When you’ve just added an entry to the database with ‘INSERT’, and you need to get the ID of that row for the next step…. $lastId = mysql_insert_id();

    mysql_insert_id()

    When you’ve just added an entry to the database with ‘INSERT’, and you need to get the ID of that row for the next step…. $lastId = mysql_insert_id();

    Continue Reading...

  • Quick and handy function to purify data before entering into the database function escape_data($data){ $data = mysql_escape_string(trim($data)); $data = htmlspecialchars($data, ENT_QUOTES, 'UTF-8'); return = $data; }

    My Escape Data Function

    Quick and handy function to purify data before entering into the database function escape_data($data){ $data = mysql_escape_string(trim($data)); $data = htmlspecialchars($data, ENT_QUOTES, 'UTF-8'); return = $data; }

    Continue Reading...

  • In a nutshell, it makes a string safe to be entered into a database…. For example: $email = 'myemail@domain.co.uk'; // This could come from a form, etc. $result = mysql_query("SELECT...

    mysql_real_escape_string

    In a nutshell, it makes a string safe to be entered into a database…. For example: $email = 'myemail@domain.co.uk'; // This could come from a form, etc. $result = mysql_query("SELECT...

    Continue Reading...

  • Once you’ve added or edited a database table, it’s good to check that the change/addition was made… then either move on or show an error, etc. Here’s how to do...

    mysql_affected_rows()

    Once you’ve added or edited a database table, it’s good to check that the change/addition was made… then either move on or show an error, etc. Here’s how to do...

    Continue Reading...

  • Here’s a great little script that will back-up your database to a zipped .gz file and store it in a folder on your server…. <?php // Set Vars $dbname =...

    Backup Database with PHP

    Here’s a great little script that will back-up your database to a zipped .gz file and store it in a folder on your server…. <?php // Set Vars $dbname =...

    Continue Reading...

  • Here’s another way to connect to the databse: // Connect with username, password, etc... $link = mysqli_connect('localhost', 'userName', 'passWord'); if (!$link){ $error = 'Unable to connect to the database server.';...

    Connect To Database With mysqli_connect

    Here’s another way to connect to the databse: // Connect with username, password, etc... $link = mysqli_connect('localhost', 'userName', 'passWord'); if (!$link){ $error = 'Unable to connect to the database server.';...

    Continue Reading...

Page 1 of 212