Solved

detect/alter MySQL tabe type with PHP

Posted on 2009-04-13
6
366 Views
Last Modified: 2013-12-12
Is there a way to detect the type of table in a MySQL database?

If there is, can the table type be altered? to say INNODB?
0
Comment
Question by:Hube02
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 5

Assisted Solution

by:kevthedude
kevthedude earned 100 total points
ID: 24132445
This script will change anything that is not what $changeTo is and set it to $changeTo
<?php
	$changeTo = "InnoDB";
	$mysqli = new mysqli("localhost", "mysql_user", "mysql_pass", "mysql_database");
	if($result = $mysqli->query("show table status"))
	{
		$tables = array();
		while ($row = $result->fetch_object()) {
			if($row->Engine != $changeTo)
				array_push($tables,$row->Name);
		}
	} else {
		printf("Error message: %s", $mysqli->error);
		exit;
	}
	foreach($tables as $v)
	{
		if($result = $mysqli->query("ALTER TABLE `".$v."` ENGINE = ".$changeTo))
		{
			printf("Changed `%s` to %s \n", $v, $changeTo);
		} else {
			printf("Error message on table `%s`: %s", $v, $mysqli->error);
		}
	}
?>

Open in new window

0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 400 total points
ID: 24132544
Maybe so.  You can show the CREATE TABLE statement with this code.  You can look in the array element called ["Create Table"] and find the string that says "ENGINE=".

Rather than altering it in PHP, I think I might want to create a copy and alter the copy.  You can also do this with phpMyAdmin.

HTH, ~Ray
<?php // RAY_show_tables.php
// INFORMATION FROM THE DATA BASE
// READABILITY
echo "<pre>\n";
 
// CONNECT TO THE DB
$db_host	= "localhost";
$db_name	= "X";
$db_user	= "Y";
$db_word	= "Z";
 
 
// CONNECT TO THE DATA BASE SERVER
if (!$db_connection = mysql_connect("$db_host", "$db_user", "$db_word")) {
	$errmsg	= mysql_errno() . ' ' . mysql_error();
	echo "<br/>! db_connection ";
	echo "<br/> $errmsg <br/>";
}
 
// SELECT THE DATA BASE
if (!$db_sel = mysql_select_db($db_name, $db_connection)) {
	$errmsg	= mysql_errno() . ' ' . mysql_error();
	echo "<br/>! db_sel ";
	echo "<br/> $errmsg <br/>";
	die('NO DATA BASE?');
}
 
// CHOOSE A TABLE
$my_table = "RAY_ANGLERS";
 
// INFORMATION ABOUT THE TABLE
$sql_t  = "SHOW CREATE TABLE $my_table";
if (!$t = mysql_query("$sql_t", $db_connection)) { fatal_query_error($sql_t); }
$show   = mysql_fetch_assoc($t);
 
// PRINT THE INFORMATION
var_dump($show);
 
 
 
 
 
// YOUR EMAIL ADDRESS
$admin_email	= "RPaseur@NationalPres.org";
/* ************************************************************************** */
// LOCAL FUNCTION TO CRAP OUT AND DIE ON FATAL MySQL ERRORS
function fatal_query_error($qstring)
{
	global $admin_email;
	$url	= $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
	$ref	= $_SERVER['HTTP_REFERER'];
	$errmsg	= mysql_errno() . ' ' . mysql_error();
 
	echo "<br /><br /><span class=\"loss\">FATAL MySQL Data Base Error</span> \n";
	echo "<br /><br />The URL for this page is:<br />$url \n";
	echo "<br /><br />The failing MySQL Query contained this:<br />$qstring \n";
	echo "<br /><br />The error message is:<br />$errmsg<br /><br />\n";
	echo "<br /><br />The referrer (if any) is:<br />$ref<br /><br />\n";
 
	if (!get_magic_quotes_gpc()) { $qstring	= addslashes($qstring); }
	$qstring	.= "\n\n";
	$qstring	.= "$errmsg \n\n";
	$subj		= ' !! FATAL MySQL Error in ' . "$url";
	mail ( $admin_email, $subj, $qstring);
	die();
}
/* ************************************************************************** */
 
?>

Open in new window

0
 
LVL 18

Author Comment

by:Hube02
ID: 24133378
Thanks kevthedude, however the application I'm working with is written in PHP4, so the mysqli functions will not work for me.
 
 If I do the altering I will need to go with Ray's suggestion.
 
 What I have is a small application that is installed on several websites. The application was built to use transactions, however, for whatever reason, when the database and tables were created they were done with MyISAM tables and not with InnoDB.
 
 Anyway, I was hoping do automate the process of correcting the error rather than using phpMyAdmin and dealing with every table in every database manually.
 
 Is there some danger using php to alter that table engine that I should be aware of?
 
 If I need to do this manually I will, but I would rather avoid it if I can.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 110

Expert Comment

by:Ray Paseur
ID: 24133522
No, I can't think of any danger, so long as you knock the site down during actual changes.  My strategy would be to try copying the tables and making the alterations in the copied tables.  But (insert large caveat here) I have never done this, so I would be all about backups and testing.  

You can use SHOW TABLES to get a list of table names.  Then you can iterate over the list.  

Best of luck with it, ~Ray
0
 
LVL 18

Author Comment

by:Hube02
ID: 24133551
I'll definitely do some testing first, it may be less time consuming to do it manually, depends on my my testing reveals.

Thanks for the help.
0
 
LVL 18

Author Closing Comment

by:Hube02
ID: 31569636
Thanks again, and the mysqli information will also com in handy when I finally get to stop worrying about PHP4 compatability.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ability to create nested list on the UI using PHP, HTML, Javascript 7 57
PHP Mail error 3 42
Make login page safer 3 30
php subtract from date 2 26
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question