Hi,
Does anyone have a php page snippet to list contents of a specific table in drupal 7.
I want to copy a snippet to the body of a basic page (php enabled)

Thanks

Comments

cord1’s picture

FILE 1 dbspy.php

//Connect to the database
echo "
This program is developed by <a href='http://cord.no'>Ole Roger Dahle</a><br><br>
This program connects to the Drupal database and uses following db commands:<br><br>
SHOW tables<br>
SELECT COUNT(0) FROM (tablename)<br>
SHOW CREATE TABLE (tablename)<br>
SELECT * FROM (tablename)<br>
SHOW COLUMNS FROM (tablename)<br>
<br><br>
This way you can spy on the content of the database
<br><br>
WARNING: For security reasons use it only on a localhost!
<br><br>

<form action='dbspy_list.php' method='post'>
<table>
<tr><td>Hostname (localhost): </td><td><input type='text' name='host' /></td></tr>
<tr><td>Databasename: </td><td><input type='text' name='dbname' /></td></tr>
<tr><td>Username (root): </td><td><input type='text' name='user' /></td></tr>
<tr><td>Password: </td><td><input type='password' name='pw' /></td></tr>

<tr><td colspan='2'><br><input type='submit' /></td></tr>
</table>
</form>
";

FILE 2 dbspy_list.php

$hosten = $_REQUEST['host'];
$useren = $_REQUEST['user'];
$pwen = $_REQUEST['pw'];
$dbnameen = $_REQUEST['dbname'];


//Connect to the database
$db = mysql_connect($hosten, $useren, $pwen) or die("cannot connect");
mysql_select_db($dbnameen, $db) or die("cannot select DB");

// create array for tablenames and number of records
$tables = array();
$records = array();


$sql = "SHOW tables";
$result = mysql_query($sql, $db);

while ($myrow = mysql_fetch_array($result)) {
  array_push($tables, $myrow[0]);
  $navn = $myrow[0];
  $sql2 = "SELECT COUNT(0) FROM $navn";
  $result2 = mysql_query($sql2, $db);
  while ($myrow2 = mysql_fetch_array($result2)) {
    array_push($records, $myrow2[0]);
  }
}
echo "<table border='1' >";
echo "<tr><td>no</td><td>Tablename</td><td>No.of records</td><td>Create</td><td>View table</td></tr>";
for ($i = 0; $i < count($tables); $i++) {
  echo "<tr><td>$i</td><td>".$tables[$i]."</td>";

  echo "<td>".$records[$i]."</td>
  <td><a href='dbspy_create.php?tabell=".$tables[$i]."&host=".$hosten."&user=$useren&pw=$pwen&dbname=$dbnameen'>create info</a></td>
  <td><a href='dbspy_view.php?tabell=".$tables[$i]."&host=".$hosten."&user=$useren&pw=$pwen&dbname=$dbnameen'>view</a></td>
  </tr>";
}
echo "</table>";

FILE 3 dbspy_view.php

$hosten = $_REQUEST['host'];
$useren = $_REQUEST['user'];
$pwen = $_REQUEST['pw'];
$dbnameen = $_REQUEST['dbname'];
$tabellen = $_REQUEST['tabell'];
echo "Table ".$tabellen;
$columnnames = array();
$no_of_columns = 0;

/*
echo "hurra<br />";
echo $hosten."<br />";
echo $useren."<br />";
echo $pwen."<br />";
echo $dbnameen."<br />";
*/

//Connect to the database
$db = mysql_connect($hosten, $useren, $pwen) or die("cannot connect");
mysql_select_db($dbnameen, $db) or die("cannot select DB");

$sql = "SHOW CREATE TABLE $tabellen";
$result = mysql_query($sql, $db);

while ($myrow = mysql_fetch_array($result)) {
  for ($i=1; $i<count($myrow); $i++){
      echo '<pre>' .$myrow[$i]. '</pre>';
  } 
}

// get table columns
$sql = "SHOW COLUMNS FROM $tabellen";
$result = mysql_query($sql, $db);
$no_of_columns = mysql_num_fields($result);

//echo "".$no_of_columns;
echo "<table border='1' width='400'><tr><td colspan='$no_of_columns'>Table content</td></tr><tr>";
while ($myrow = mysql_fetch_array($result)) {
      echo "<td>".$myrow['Field']."</td>";
}

echo "</tr>";

// show table column contents
$sql2 = "SELECT * FROM $tabellen";
$result2 = mysql_query($sql2, $db);
$no_of_columns2 = mysql_num_fields($result2);
//echo "".$no_of_columns;
$l = 0;

while ($myrow2 = mysql_fetch_array($result2)) {
  echo "<tr>";
  for ($i = 0; $i < $no_of_columns2; $i++) {
    echo "<td>".$myrow2[$i]."</td>";
  }
  echo "</tr>";
}
echo "</table>";

weldja’s picture

thank you ;)

d_l’s picture

Thanks for these three useful scripts ..
but have you overlooked posting dbspy_create.php which is called in "create info" links?

cord1’s picture

This is an old script and I beleave create only does the same as first part of dbspy_view.php
Like this:

$hosten = $_REQUEST['host'];
$useren = $_REQUEST['user'];
$pwen = $_REQUEST['pw'];
$dbnameen = $_REQUEST['dbname'];
$tabellen = $_REQUEST['tabell'];
echo "Table ".$tabellen;
$columnnames = array();
$no_of_columns = 0;

//Connect to the database
$db = mysql_connect($hosten, $useren, $pwen) or die("cannot connect");
mysql_select_db($dbnameen, $db) or die("cannot select DB");

$sql = "SHOW CREATE TABLE $tabellen";
$result = mysql_query($sql, $db);

while ($myrow = mysql_fetch_array($result)) {
  for ($i=1; $i<count($myrow); $i++){
      echo '<pre>' .$myrow[$i]. '</pre>';
  } 
}

evita_gr’s picture

and where do u put these files????

cord1’s picture

Create a subdirectory in your site and copy the files there
Then call the script by:
(your site)/(your subdirectory)/dbspy.php