background preloader

MySQL

Facebook Twitter

'MySQLi' for Beginners. Introduction Nearly every site that you visit nowadays has some sort of database storage in place, many sites opt to use MySQL databases when using PHP.

'MySQLi' for Beginners

However, many people haven't yet taken the step to interacting with databases properly in PHP. Here we guide you through what you should be doing - using PHP's MySQLi class - with a hat-tip to the one way that you definitely shouldn't be doing it. The Wrong Way If you're using a function called mysql_connect() or mysql_query() you really need to take note and change what you're doing. Any of the functions that are prefixed with mysql_ are now being discouraged by PHP themselves as visible on this doc page, instead you should look to use one of the following: Each has its advantages, PDO for example will work with various different database systems, where as MySQLi will only work with MySQL databases. PHP MySQLi Connecting Obviously, the database name is optional and can be omitted. Querying Output query results Number of returned rows <? <? Mysqlfuncchart_1180.jpg (JPEG Image, 1684×1180 pixels) - Scaled (59%) - (Private Browsing)

Top 20+ MySQL Best Practices. Database operations often tend to be the main bottleneck for most web applications today.

Top 20+ MySQL Best Practices

It's not only the DBA's (database administrators) that have to worry about these performance issues. We as programmers need to do our part by structuring tables properly, writing optimized queries and better code. In this article, I'll list some MySQL optimization techniques for programmers. Before we start, be aware that you can find a ton of useful MySQL scripts and utilities on Envato Market. Most MySQL servers have query caching enabled. The main problem is, it is so easy and hidden from the programmer, most of us tend to ignore it. The reason query cache does not work in the first line is the usage of the CURDATE() function. Using the EXPLAIN keyword can give you insight on what MySQL is doing to execute your query. The results of an EXPLAIN query will show you which indexes are being utilized, how the table is being scanned and sorted etc...

After adding the index to the group_id field: Introduction to Databases for the Web. Common MySQL Queries. Common MySQL Queries Basic aggregation Last updated 05 Jan 2013 Aggregate across columns Last updated 09 Sep 2009 Aggregates across multiple joins Given a parent table and two child tables, a query which sums values in both child tables, grouping on a parent table column, returns sums that are exactly twice as large as they should be.

Common MySQL Queries

CREATE TABLE packageCredit ( packageCreditID INT, packageCreditItemID INT, Last updated 22 Feb 2013 Aggregates excluding leaders You have a table of grouped ranks ... Last updated 21 May 2009 Aggregates of specified size Find the values of a table column c1 for which there are a specified number of listed values in another column c2. All X for which all Y are Z You have an election database with tables for candidates, parties and districts.

CREATE TABLE districts ( district char(10) DEFAULT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1; INSERT INTO districts VALUES ('Essex'),('Malton'),('Riverdale'),('Guelph'),('Halton'); Avoiding repeat aggregation Cascading aggregates.