|
Posted by soldier.coder on July 17, 2008, 12:24 pm
Please log in for more thread options
I have 2 requirements that I don't know how to implement in php using
MySql.
1) I want to see if a particular table name exists.
2) I want an arrays with the column names that exist in a table.
can anyone help me out with that please?
Thanks
|
|
Posted by NC on July 17, 2008, 1:33 pm
Please log in for more thread options
wrote:
>
> I have 2 requirements that I don't know how to implement
> in php using MySql.
>
> 1) I want to see if a particular table name exists.
Run a SHOW TABLES query; it will return a list of tables that exist in
the currently selected database. See MySQL documentation:
http://dev.mysql.com/doc/refman/5.1/en/show-tables.html
> 2) I want an arrays with the column names that exist in a table.
Run a SHOW COLUMNS query; it will return a list of fields that exist
in the specified table. See MySQL documentation:
http://dev.mysql.com/doc/refman/5.1/en/show-columns.html
Cheers,
NC
|
|
Posted by Barry on July 17, 2008, 1:39 pm
Please log in for more thread options
>I have 2 requirements that I don't know how to implement in php using
> MySql.
>
> 1) I want to see if a particular table name exists.
>
> 2) I want an arrays with the column names that exist in a table.
>
> can anyone help me out with that please?
rtfm.
both these questions are answered in the mysql manual and are well
documented - not hidden.
|
|
Posted by bs on July 21, 2008, 7:04 am
Please log in for more thread options >I have 2 requirements that I don't know how to implement in php using
> MySql.
>
> 1) I want to see if a particular table name exists.
>
> 2) I want an arrays with the column names that exist in a table.
>
> can anyone help me out with that please?
>
> Thanks
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
something to get you started......
============================================================================================
<?php
echo '<h2>Started</h2>';
error_reporting == E_ALL;
// =======================================================================
// Sample MySQL Database Access Class
// =======================================================================
$DatabaseIO=new DatabaseIO(); // create an instance of the DatabaseIO
class
$conn =$DatabaseIO->connect(); // connect to the database
if ($conn instanceof mysqli)
echo "<br />Connected OK";
if (!$conn == null) // we have a connection, so do the needful
{
echo "<br />Get The List Of Tables<br>";
//
=======================================================================
// get the list of all tables (you have access to...)
//
=======================================================================
$rs=$DatabaseIO->getTablesForSchema($conn, "%");
/* loop through the array, displaying the results */
echo "\n<pre>\n";
echo "\nTable Schema Table Name";
echo "\n---------------- -------------------------------";
foreach ($rs as $row)
{
$tabschema = $row["tabschema"];
$tabschema =str_pad($tabschema, 20);
$tabname =$row["tabname"];
echo "<br />" . $tabschema . $tabname;
}
echo "\n</pre>\n";
echo "<br />Close The Connection";
$conn->close();
}
/* mySQL Database IO Class */
Class DatabaseIO
{
public function __construct() { echo "<br />Class Instanciated - " .
get_class($this); }
//
=======================================================================
// return the list of tables for the specified schema
//
=======================================================================
public function getTablesForSchema(mysqli $conn, $tabschema)
{
echo "<br />getTablesForSchema";
$rs =null; // initialize the results array
/* create a prepared statement */
$sql="SELECT TABLE_SCHEMA, TABLE_NAME FROM TABLES WHERE TABLE_SCHEMA
LIKE ? ORDER BY 1, 2 ASC";
$conn->stmt_init(); // initialize the statement
$stmt=$conn->prepare($sql); // prepare the statement
if ($stmt == FALSE)
{
die ("<br /> => Error preparing statement");
}
/* bind parameters for markers
s = string
i = integer
b = boolean */
echo "<br />bind parameters";
if ($stmt->bind_param("s", $tabschema) == FALSE)
{
die ("<br />=> Could not bind params");
}
echo "<br />Tabschema = " . $tabschema;
/* execute query */
echo "<br />execute query";
if ($stmt->execute() == FALSE)
{
die ("<br />=> Could not execute statement");
}
/* bind result variables */
$stmt->bind_result($tabschema, $tabname);
/* Cycle through results fetching all the rows */
while ($stmt->fetch())
{
// echo "<br />" . $tabschema . " " . $tabname;
$row=array
(
"tabschema" => $tabschema,
"tabname" => $tabname
);
$rs[]=$row; // add the row to the array
}
/* Free result set */
$stmt->free_result();
$stmt->close();
return $rs; // return the data
}
//
=======================================================================
// connect to the database
// - and return the connection object
//
=======================================================================
public function connect()
{
// these should be in a seperate module for security purposes
$site ="localhost";
$user ="your user name"; // put your user
name here !!!!!
$password="your password"; // put your password here
!!!!!
$database="INFORMATION_SCHEMA";
$conn =new mysqli();
//Call the init method to allow setting of options
$conn->init();
// Set mysqli options
$conn->options(MYSQLI_CLIENT_COMPRESS, true);
$conn->connect($site, $user, $password, $database);
if (mysqli_connect_error())
{
echo "<br />Connection Error";
echo "Connection Error = " . mysqli_connect_errno();
printf("Can't connect to MySQL Server. Errorcode: %s\n",
mysqli_connect_error());
return null; // failed to connect - so return NULL
}
else
{
echo "<br />Connected OK<br />";
return $conn; // Houston - we have a connection !!!
}
}
//
=======================================================================
public function __destruct() { echo "<br /> Class Destroyed - " .
get_class($this); }
}
?>
|
| Similar Threads | Posted | | Check if table exists | July 8, 2005, 6:42 pm |
| how to use "if exists" - INSERT INTO IF EXISTS tMyTable ??? | September 2, 2004, 11:05 am |
| mysql: how create a temp table as a copy of a existing table? | July 22, 2004, 8:56 pm |
| Mysql fetch_field gets table alias, not real table name | January 6, 2006, 8:01 am |
| From CSV To MySQL Table | May 14, 2007, 2:56 am |
| PHP MYSQL Table help | March 27, 2008, 3:24 pm |
| reapir mysql table from php | September 20, 2004, 6:04 pm |
| PHP mySQL - table of images | October 26, 2004, 7:46 pm |
| PHP variables from mysql table | February 25, 2005, 6:10 am |
| Problem with MySQL table | June 25, 2005, 2:18 am |
|