Copying tables in MySQL

There are multiple ways this can be done. I will mention two here.

Method 1

In MySQL, run these statements:

/*This will create table2 with the schema of table1*/
CREATE TABLE `table2` LIKE `table1`
 
/*This will copy the data from table1 to table2*/
INSERT INTO `table2` SELECT * FROM `table1`

Method 2

On the command line, run these commands:

# Create a dump from MySQL.
# The -d flag will only copy the structure/schema of the tables. If you want
# to copy the data along with the schema then dont use -d flag
#
# In here, two MySQL commands are piped so the output from mysqldump is sent 
# to mysql command
mysqldump -d -u username -pPassword  db_name | mysql -u username -pPassword db_new_name
Posted in MySQL by Moazzam. No Comments

Get just the fields names of a table in MySQL

If you do:

DESCRIBE `table_name`

Mysql will give you table information which will contain field names, their types, etc. What if you want just the field names and nothing else? You can run this query and do just that:

SELECT column_name
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'tablename'
AND   table_schema = 'dbname';
Posted in MySQL by Moazzam. No Comments

Code Assist in Zend Studio (eclipse based)

Have you noticed there is a delay autocomplete suggestions when you type in a variable name or a function name? My typing speed is decent and I found the code assist delay to be too much. By the time Zend Studio was ready to show me the suggestions, I had already typed the variable name.

By default, Zend Studio has a 200 millisecond delay in showing code assist. You can shorten it if you feel the delay is too long. I set it to 100 milliseconds.

Go to Preferences -> Java -> Editor -> Content Assist -> Auto-Activation and decrease Auto activation delay from 200 to 100 (or whatever suits you better).

Tags: , , ,
Posted in PHP by Moazzam. No Comments

Importing a CSV file into a table in MySQL

Here’s how you would import data into MySQL using a query

 
/*
If the CSV file was created in Windows, then replace '\n' with '\r\n' 
*/
LOAD DATA LOCAL INFILE '/path/to/csv_file.csv' INTO TABLE `table_name`
FIELDS TERMINATED BY ',' 
OPTIONALLY ENCLOSED BY "\"" 
LINES TERMINATED BY '\n' 
(field1, field2, field3);
Tags: ,
Posted in MySQL by Moazzam. No Comments

Cool things you can do with MySQL

Run a query from command line

# Use the -e flag
shell>mysql -e "select * from table"

Tell MySQL to output the results of a query in HTML or XML

# On command line you would write the following to 
# tell MySQL to output the results in HTML format
shell>mysql -e "select * from table" --html
 
# This is for XML
shell> mysql -e "select * from table" --xml
 
# This is for batched results
shell> mysql -e "select * from table" --batch
Tags:
Posted in MySQL by Moazzam. No Comments

Tips for a Speedy Zend Studio

I found a good article on how to speed up your Zend Studio (eclipse based) on Zend’s website. Hopefully everyone else will find it as helpful as I found it to be:

http://kb.zend.com/index.php?View=entry&EntryID=480

Posted in PHP by Moazzam. No Comments

Zend Server and Zend Studio9

If you have Zend Server installed and you have an instance of MySQL that didn’t come with Zend Server, then Zend Studio will try to connect to the one that came with Zend Server. I discovered this the hard way when my PHPUnit tests kept failing in Zend Studio but ran perfectly from the command line (I had PHPUnit installed from PEAR and the one that came with Zend Studio).

So I decided to make Zend Server connect to my default MySQL instance instead of uninstalling Zend Server (because I want to play with it). For those with the same problem, you can do this:

# Rename Zend Studio's socket file to something else
# If you didn't choose the default, installation path then 
# use the path to that location instead
mv /usr/local/zend/mysql/tmp/mysql.sock /usr/local/zend/mysql/tmp/mysql_old.sock
 
# My MySQL installation's socket file is located in /tmp
# If your's is different, then you will have to modify the source path
 
ln -s /tmp/mysql.sock /usr/local/zend/mysql/tmp

Virtual hosts look up taking too long in OSX Lion

If your mac (OSX Lion) is taking too long to get pages with your local web server and you have virtual hosts setup, then it’s probably because it is checking the DNS servers before it checks for it in your /etc/hosts.

The way to resolve this is to have a ::1 entry in your hosts file for every 127.0.0.1 entry. Here’s an example:

127.0.0.1         example.com
::1               example.com

You will notice I have 2 entries for example.com. One is IPv4 and the other is IPv6. My requests to the local web server take almost no time at after I did that. I think Lion tries to use IPv6 before it uses IPv4 and since it doesn’t find any entries for that in your hosts file, it goes to the internet searching for that domain.

Posted in Uncategorized by Moazzam. No Comments

Some information on booking flights with Sabre

A lot of people contacted me asking about how Sabre works, etc. So, I decided to post this here. The way it works is:

  1. You sign up with Sabre for a web services account and you they will give you an IPCC, username and passwrord that you use for communicating with their web services.You will send over the username and password for only authentication request. However, you will use your IPCC in all the requests you make.
  2. Your initial request should be a SessionCreate request. You will provide your username and password in it. Upon success, you will receive a binary token. You will use this token in all the subsequent requests you make.
  3. Your subsequent requests will depend on what you want to do and the services you have access to. Sabre has multiple flight services that you can communicate with. You will need to contact them, explain your needs and ask what service will be best for you. LowFareSearch and BargainFinder are probably the ones that are most used.
  4. After you are done selecting the flight, etc. You will send a request to AirBook (if I remember the name correctly) to book a flight. And, there’s other services you might want to use to pass passenger information, etc.
  5. After you are done, you end the session with a call to SessionCloseRQ.

When you get an account with Sabre, you will be able to look at request and response formats for  the for the web services over here: https://drc.sabre.com/oer/


 

Nexus S and TMobile data connection

Here’s an interesting tidbit. If you go out of the country and come back. Nexus S won’t automatically connect to TMobile’s 3G or edge network. You have to switch it off and start it again for it to connect to the data network. It will, however, let you make phone calls, and send and receive SMS without restarting it.