- PHP: Hypertext Preprocessor is a server-side scripting language designed for web development. It was originally created by Rasmus Lerdorf in 1994; the PHP reference implementation is now produced by The PHP Group
- for PHP implementation you need to download and install “xampp” software in your PC first.
- Download link : https://www.apachefriends.org/download.html
Contents
MySQL
Image Source
Functions
- SQL Server
- mysql_connect() – Open a connection to a MySQL Server
- Data Base
- mysql_db_name() –
- mysql_select_db() – Select a MySQL database
- Table
- mysql_tablename – Get table name of field
Data Base
Show the all Database in server
<?php // Usage without mysql_list_dbs() $link = mysql_connect('localhost', 'mysql_user', 'mysql_password'); $res = mysql_query("SHOW DATABASES"); while ($row = mysql_fetch_assoc($res)) { echo $row['Database'] . "\n"; } // Deprecated as of PHP 5.4.0 $link = mysql_connect('localhost', 'mysql_user', 'mysql_password'); $db_list = mysql_list_dbs($link); while ($row = mysql_fetch_object($db_list)) { echo $row->Database . "\n"; } ?>
Table
Show the table name in db
<?php mysql_connect("localhost", "mysql_user", "mysql_password"); $result = mysql_list_tables("mydb"); $num_rows = mysql_num_rows($result); for ($i = 0; $i < $num_rows; $i++) { echo "Table: ", mysql_tablename($result, $i), "\n"; } mysql_free_result($result); ?>
Finding Table existence
Using if Loop
if(mysql_num_rows(mysql_query("SHOW TABLES LIKE '".$table."'"))==1) echo "Table exists"; else echo "Table does not exist";
Using function
function TableExists($tablename, $db) { // Get a list of tables contained within the database. $result = mysql_list_tables($db); $rcount = mysql_num_rows($result); // Check each in list for a match. for ($i=0;$i<$rcount;$i++) { if (mysql_tablename($result, $i)==$tablename) return true; } return false; }