PHPSuperBlog.com

Use PHP to change MySQL field size

In this example, I am going to show you how to use PHP to change a MySQL database’s field size.

I have created a MySQL database with a table called “test” and a field called “testing” size varchar(100).

I have created a PHP file called “changesize.php” and I would like to use it to change the field size from varchar(100) to varchar(200).

For the “changesize.php”:

First we have to connect to the database:

mysql_connect("Host Name", "User Name", "User Password") or die("Connection Failed");
mysql_select_db("DataBase Name")or die("Connection Failed");

Then we create a query which is for changing the MySQL field’s size from varchar(100) to varchar(200) and the query is stored into a variable $query:

$query ="alter test modify testing varchar(200)";

where test is the table name and testing is the field name.

Then we run the query and to see if it works, a conditional statement if,else is added.

if(mysql_query($query)){
echo "Field size is changed";}
else{
echo "fail";}

All together we have for “changesize.php” file:

<?php
mysql_connect("Host Name", "User Name", "User Password") or die("Connection Failed");
mysql_select_db("DataBase Name")or die("Connection Failed");
$query ="alter test modify testing varchar(200)";
if(mysql_query($query)){
echo "Field size is changed";}
else{
echo "fail";}
?>
VN:F [1.9.3_1094]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)

Similar Posts:

Leave a comment