Tuesday, December 31, 2013

List All Database Names under a Server

<?php
$con = mysql_connect("localhost", "root", "");
if (!$con)
{
  die('Could not connect: ' . mysql_error());
}

$db_list = mysql_list_dbs($con);

while ($db = mysql_fetch_object($db_list))
{
  echo $db->Database . "<br />";
}
mysql_close($con);
?>

Use of REGEXP operator.

Consider we have a table called person_tbl and it's having a field called name:
Query to find all the names starting with 'st'
mysql> SELECT name FROM person_tbl WHERE name REGEXP '^st';
Query to find all the names ending with 'ok'
mysql> SELECT name FROM person_tbl WHERE name REGEXP 'ok$';
Query to find all the names, which contain 'mar'
mysql> SELECT name FROM person_tbl WHERE name REGEXP 'mar';
Query to find all the names starting with a vowel and ending with 'ok'
mysql> SELECT name FROM person_tbl WHERE name REGEXP '^[aeiou]|ok$';