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
 
<!-- AddThis Button BEGIN -->
<a class="addthis_button" href="http://addthis.com/bookmark.php?v=250&amp;username=xa-4beeaaab61b514bf"><img src="http://s7.addthis.com/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a><script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#username=xa-4beeaaab61b514bf"></script>
<!-- AddThis Button END -->
<br>
<br>

Share this knowledge with your friends!

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