Determining the number of tables that are contained in a MySQL database is very straight-forward, although it is an often asked question. The simplest way to accomplish this is using the following SQL query. In this query, you will provide the database, and the SQL will access MySQL’s internal data scheme (information_schema).
SELECT count(*) as ‘Tables’, table_schema as ‘Database’
WHERE table_schema= ‘The Database Name’
GROUP BY table_schema
If you need to retrieve this information using PHP, use can use the following code. It creates a connection, runs the query, and returns the number of rows retrieved. This query you notice is slightly different than the one above, often there are multiple ways to get the same result!
$conn = mysql_connect(‘localhost’, ‘USERNAME’, ‘PASSWORD’, 1, 65536);
$res = mysql_query( “select table_name from information_schema.tables where table_schema=’test'”, $conn );
echo mysql_num_rows( $res );
And lastly, there is one more way that you can retrieve the number of tables in a database using PHP and the Show Tables command. In this example, you will need to be connected to a server, and have the database set to the one you are querying against.
$tbl_List = mysql_query(“SHOW TABLES”);
while($tables = mysql_tablename($tbl_List,$i))
echo “<br />Table count = $i”;
Want a quick and dirty way to Truncate all the tables in a MySQL DB? You can use the following one line script.
mysqldump -ppassword YourDBName –no-data dumpfile | mysql dumpfile
What this does is uses mysqldump to output all the tables in the database “YourDBName” into a dump file. This dump file contains drop statements, and create table statements for each table in the database. What this effectively does is drops every table, and recreates it WITHOUT any data inside. Be very careful, since this is a very powerful script, it has the ability to completely wipe out your database with no hope of recovery unless you have a current backup.
For additional ways to execute specific commands against all tables in a DB, look into using mk-find in Maatkit, it is an excellent tool for advanced MySQL scripting.
There are multiple ways in which to remove all data from a MySQL Table.
The first is to use the DROP TABLE command, which will remove the entire table with data. This basically removes the entire table, you will not be able to recover the structure or data.
The second is DELETE * FROM Table which will remove the data, leaving the auto-increment values alone, as well as the rest of the table structure. This method is very slow on large tables using Innodb. This is because each record has to be written to the log. In general, you should never use this method to remove all data from a table.
The third method is to issue a TRUNCATE TABLE command which will quickly remove all the data from the table, reset the auto-increment values (but leave them as auto-increment columns, so they will just start at 1 and go up from there again).
Lastly, you can use the nuclear option, which is as follows :
mysqldump -ppassword YourDBName YourTblName –no-data dumpfile | mysql dumpfile
This will drop the table, and then recreate it all from scratch.
SQL injection vulnerabilities are often been described as the most serious threat for Web applications, regardless of what language they are written in . Web applications that are vulnerable to SQL injection may allow an attacker to gain complete access to their underlying databases, regardless of if they are using MySQL Server, SQL Server, or Oracle.
SQL-injection attacks are those in which data provided by the user is included in an SQL query in such a way that part of the user’s input is treated as SQL code that is executed on the server. By using this technique, an attacker can submit SQL commands directly to the database. These attacks are a serious threat to any Web application that receives input from users and passes it into SQL queries to an underlying database server. If user input is not santised properly, web applications may result in SQL Injection attacks that allow hackers to view information from the database and/or even wipe it out.
To defend against SQL Injection attacks, user input must not directly be embedded into SQL statements that are executed on the server. Instead, you must use parameterized statements, and Escaping functions to check user input.
Various resource for addressing SQL Injection are as follows:
Bobby Tables provides real-world practical code for addressing SQL Injection – http://bobby-tables.com/
Michal Daw’s Blog Page outlines various SQL Injection vectors – http://michaeldaw.org/sql-injection-cheat-sheet