User Tools

Site Tools


howto_extract_data_from_db_in_php

Creating a simple PHP SQL database extractor.

Example: Fetching data from X.X.X.X is the ip address of the db server.

HTML

BODY

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

mysql_select_db("databasenname", $con);

$result = mysql_query("SELECT table_name,ordinal_position,column_name,data_type,  is_nullable,character_maximum_length FROM
information_schema.COLUMNS WHERE table_name LIKE '%TABLENAME%' ORDER BY ordinal_position");

while($row = mysql_fetch_array($result))
  {
  echo $result['Here are you results'];
  echo "<br />";
  }

mysql_close($con);
?>

END BODY

http://w3schools.com/php/php_mysql_connect.asp

If you do not know tables you can have the db describe them using the following command:

Definition: The describe SQL command is used to list all of the fields in a table and the data format of each field. 

It is phrased as: describe [TableName];

Examples: This would list all of the fields and their formats for the table called 'Colors':

describe Colors;

DESCRIBE ;

or

SELECT column_name,column_type,is_nullable,column_key,col  umn_default,extra FROM information_schema.COLUMNS WHERE
table_schema='' AND table_name=''


SELECT table_name,ordinal_position,column_name,data_type,  is_nullable,character_maximum_length FROM
information_schema.COLUMNS WHERE table_name LIKE '%TABLENAME%'
ORDER BY ordinal_position
Bookmark and Share

Share this knowledge with your friends!

howto_extract_data_from_db_in_php.txt · Last modified: 2012/03/09 10:43 (external edit)