PHP MySql Web Hosting

PHP and MySQL Web Development

Postprocess mysql’s output. 1.23.3 Discussion In non-interactive mode,

Filed under: MySQL Solutions — webmaster @ 8:46 am

PHP and MySQL Web Development

% mysql cookbook > outputfile However, if you

Filed under: MySQL Solutions — webmaster @ 9:33 pm

% mysql cookbook > outputfile However, if you try to run mysql interactively with the output redirected, you won’t be able to see what you’re typing, so generally in this case you’ll also take query input from a file (or another program): % mysql cookbook < inputfile > outputfile You can also send query output to another program. For example, if you want to mail query output to someone, you might do so like this: % mysql cookbook < inputfile | mail paul Note that because mysql runs non-interactively in that context, it produces tab-delimited output, which the mail recipient may find more difficult to read than tabular output. Recipe 1.22 shows how to fix this problem. 1.22 Selecting Tabular or Tab-Delimited Query Output Format 1.22.1 Problem mysql produces tabular output when you want tab-delimited output, or vice versa. 1.22.2 Solution Select the desired format explicitly with the appropriate command-line option. 1.22.3 Discussion When you use mysql non-interactively (such as to read queries from a file or to send results into a pipe), it writes output in tab-delimited format by default. Sometimes it's desirable to produce tabular output instead. For example, if you want to print or mail query results, tab- delimited output doesn't look very nice. Use the -t (or --table) option to produce tabular output that is more readable: % mysql -t cookbook < inputfile | lpr % mysql -t cookbook < inputfile | mail paul The inverse operation is to produce batch (tab-delimited) output in interactive mode. To do this, use -B or --batch. 1.23 Specifying Arbitrary Output Column Delimiters 1.23.1 Problem You want mysql to produce query output using a delimiter other than tab. 1.23.2 Solution

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Cheap Web Hosting services

PHP and MySQL Web Development

Redirect mysql’s output or use a pipe. 1.21.3

Filed under: MySQL Solutions — webmaster @ 9:12 am

Redirect mysql’s output or use a pipe. 1.21.3 Discussion mysql chooses its default output format according to whether you run it interactively or non- interactively. Under interactive use, mysql normally sends its output to the terminal and writes query results using tabular format: mysql> SELECT * FROM limbs; +————–+——+——+ | thing | legs | arms | +————–+——+——+ | human | 2 | 2 | | insect | 6 | 0 | | squid | 0 | 10 | | octopus | 0 | 8 | | fish | 0 | 0 | | centipede | 100 | 0 | | table | 4 | 0 | | armchair | 4 | 2 | | phonograph | 0 | 1 | | tripod | 3 | 0 | | Peg Leg Pete | 1 | 2 | | space alien | NULL | NULL | +————–+——+——+ 12 rows in set (0.00 sec) In non-interactive mode (that is, when either the input or output is redirected), mysql writes output in tab-delimited format: % echo “SELECT * FROM limbs” | mysql cookbook thing legs arms human 2 2 insect 6 0 squid 0 10 octopus 0 8 fish 0 0 centipede 100 0 table 4 0 armchair 4 2 phonograph 0 1 tripod 3 0 Peg Leg Pete 1 2 space alien NULL NULL However, in either context, you can select any of mysql’s output formats by using the appropriate command-line options. This section describes how to send mysql output somewhere other than the terminal. The next several sections discuss the various mysql output formats and how to select them explicitly according to your needs when the default format isn’t what you want. To save output from mysql in a file, use your shell’s standard redirection capability:

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Cheap Web Hosting services

PHP and MySQL Web Development

[6] The –pager option is not available under

Filed under: MySQL Solutions — webmaster @ 11:42 pm

[6] The –pager option is not available under Windows. % mysql –pager=/usr/bin/less –pager by itself tells mysql to use your default pager, as specified in your PAGER environment variable: % mysql –pager If your PAGER variable isn’t set, you must either define it or use the first form of the command to specify a pager program explicitly. To define PAGER, use the instructions in Recipe 1.9 for setting environment variables. Within a mysql session, you can turn paging on and off using P and n. P without an argument enables paging using the program specified in your PAGER variable. P with an argument enables paging using the argument as the name of the paging program: mysql> P PAGER set to /bin/more mysql> P /usr/bin/less PAGER set to /usr/bin/less mysql> n PAGER set to stdout Output paging was introduced in MySQL 3.23.28. Another way to deal with long result sets is to use a terminal program that allows you to scroll back through previous output. Programs such as xterm for the X Window System, Terminal for Mac OS X, MacSSH or BetterTelnet for Mac OS, or Telnet for Windows allow you to set the number of output lines saved in the scrollback buffer. Under Windows NT, 2000, or XP, you can set up a DOS window that allows scrollback using the following procedure: 1. Open the Control Panel. 2. Create a shortcut to the MS-DOS prompt by right clicking on the Console item and dragging the mouse to where you want to place the shortcut (on the desktop, for example). 3. Right click on the shortcut and select the Properties item from the menu that appears. 4. Select the Layout tab in the resulting Properties window. 5. Set the screen buffer height to the number of lines you want to save and click the OK button. Now you should be able to launch the shortcut to get a scrollable DOS window that allows output produced by commands in that window to be retrieved by using the scrollbar. 1.21 Sending Query Output to a File or to a Program 1.21.1 Problem You want to send mysql output somewhere other than to your screen. 1.21.2 Solution

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Cheap Web Hosting services

PHP and MySQL Web Development

+———-+ +———————+ | NOW( ) | +———————+ |

Filed under: MySQL Solutions — webmaster @ 12:32 pm

+———-+ +———————+ | NOW( ) | +———————+ | 2001-07-04 10:42:22 | +———————+ 1.18.4 See Also By default, results generated by queries that are specified with -e are displayed in tabular format if output goes to the terminal, and in tab-delimited format otherwise. To produce a different output style, see Recipe 1.22. 1.19 Using Copy and Paste as a mysql Input Source 1.19.1 Problem You want to take advantage of your graphical user interface (GUI) to make mysql easier to use. 1.19.2 Solution Use copy and paste to supply mysql with queries to execute. In this way, you can take advantage of your GUI’s capabilities to augment the terminal interface presented by mysql. 1.19.3 Discussion Copy and paste is useful in a windowing environment that allows you to run multiple programs at once and transfer information between them. If you have a document containing queries open in a window, you can just copy the queries from there and paste them into the window in which you’re running mysql. This is equivalent to typing the queries yourself, but often quicker. For queries that you issue frequently, keeping them visible in a separate window can be a good way to make sure they’re always at your fingertips and easily accessible. 1.20 Preventing Query Output from Scrolling off the Screen 1.20.1 Problem Query output zooms off the top of your screen before you can see it. 1.20.2 Solution Tell mysql to display output a page at a time, or run mysql in a window that allows scrollback. 1.20.3 Discussion If a query produces many lines of output, normally they just scroll right off the top of the screen. To prevent this, tell mysql to present output a page at a time by specifying the –pager option.[6] –pager=program tells mysql to use a specific program as your pager:

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost PHP Web Hosting services

PHP and MySQL Web Development

produces output consisting of semicolon-terminated SQL statements can

Filed under: MySQL Solutions — webmaster @ 2:12 am

produces output consisting of semicolon-terminated SQL statements can be used as an input source for mysql. This can be useful in many ways. For example, the mysqldump utility is used to generate database backups. It writes a backup as a set of SQL statements that recreate the database, so to process mysqldump output, you feed it to mysql. This means you can use the combination of mysqldump and mysql to copy a database over the network to another MySQL server: [5] Under Windows, the equivalent would be the “useless use of type award”: % mysqldump cookbook | mysql -h some.other.host.com cookbook Program-generated SQL also can be useful when you need to populate a table with test data but don’t want to write the INSERT statements by hand. Instead, write a short program that generates the statements and send its output to mysql using a pipe: % generate-test-data | mysql cookbook 1.17.4 See Also mysqldump is discussed further in Chapter 10. 1.18 Specifying Queries on the Command Line 1.18.1 Problem You want to specify a query directly on the command line for mysql to execute. 1.18.2 Solution mysql can read a query from its argument list. Use the -e (or –execute) option to specify a query on the command line. 1.18.3 Discussion For example, to find out how many records are in the limbs table, run this command: % mysql -e “SELECT COUNT(*) FROM limbs” cookbook +———-+ | COUNT(*) | +———-+ | 12 | +———-+ To run multiple queries with the -e option, separate them with semicolons: % mysql -e “SELECT COUNT(*) FROM limbs;SELECT NOW( )” cookbook +———-+ | COUNT(*) | +———-+ | 12 |

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost MySQL Web Hosting services

PHP and MySQL Web Development

Then create a script file loop.sql that contains

Filed under: MySQL Solutions — webmaster @ 4:12 pm

Then create a script file loop.sql that contains the following lines (be sure each line ends with a semicolon): UPDATE counter SET depth = depth + 1; SELECT depth FROM counter; SOURCE loop.sql; Finally, invoke mysql and issue a SOURCE command to read the script file: % mysql cookbook mysql> SOURCE loop.sql; The first two statements in loop.sql increment the nesting counter and display the current depth value. In the third statement, loop.sql sources itself, thus creating an input loop. You’ll see the output whiz by, with the counter display incrementing each time through the loop. Eventually mysql will run out of file descriptors and stop with an error: ERROR: Failed to open file ‘loop.sql’, error: 24 What is error 24? Find out by using MySQL’s perror (print error) utility: % perror 24 Error code 24: Too many open files 1.17 Telling mysql to Read Queries from Other Programs 1.17.1 Problem You want to shove the output from another program into mysql. 1.17.2 Solution Use a pipe. 1.17.3 Discussion An earlier section used the following command to show how mysql can read SQL statements from a file: % mysql cookbook < limbs.sql mysql can also read a pipe, to receive output from other programs as its input. As a trivial example, the preceding command is equivalent to this one: % cat limbs.sql | mysql cookbook Before you tell me that I've qualified for this week's "useless use of cat award,"[5] allow me to observe that you can substitute other commands for cat. The point is that any command that

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Cheap Web Hosting services

PHP and MySQL Web Development

INSERT INTO limbs (thing,legs,arms) VALUES(’tripod’,3,0); INSERT INTO limbs

Filed under: MySQL Solutions — webmaster @ 4:44 am

INSERT INTO limbs (thing,legs,arms) VALUES(’tripod’,3,0); INSERT INTO limbs (thing,legs,arms) VALUES(’Peg Leg Pete’,1,2); INSERT INTO limbs (thing,legs,arms) VALUES(’space alien’,NULL,NULL); To execute the statements in this SQL script file in batch mode, change directory into the tables directory of the recipes distribution where the table-creation scripts are located, then run this command: % mysql cookbook < limbs.sql You'll note that the script contains a statement to drop the table if it exists before creating it anew and loading it with data. That allows you to experiment with the table without worrying about changing its contents, because you can restore the table to its baseline state any time by running the script again. The command just shown illustrates how to specify an input file for mysql on the command line. As of MySQL 3.23.9, you can read a file of SQL statements from within a mysql session by using a SOURCE filename command (or . filename, which is synonymous). Suppose the SQL script file test.sql contains the following statements: SELECT NOW( ); SELECT COUNT(*) FROM limbs; You can execute that file from within mysql as follows: mysql> SOURCE test.sql; +———————+ | NOW( ) | +———————+ | 2001-07-04 10:35:08 | +———————+ 1 row in set (0.00 sec) +———-+ | COUNT(*) | +———-+ | 12 | +———-+ 1 row in set (0.01 sec) SQL scripts can themselves include SOURCE or . commands to include other scripts. The danger of this is that it’s possible to create a source loop. Normally you should take care to avoid such loops, but if you’re feeling mischievous and want to create one deliberately to find out how deep mysql can nest input files, here’s how to do it. First, issue the following two statements manually to create a counter table to keep track of the source file depth and initialize the nesting level to zero: mysql> CREATE TABLE counter (depth INT); mysql> INSERT INTO counter SET depth = 0;

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost PHP MySQL Web Hosting services

PHP and MySQL Web Development

By default, the mysql program reads input interactively

Filed under: MySQL Solutions — webmaster @ 6:09 pm

By default, the mysql program reads input interactively from the terminal, but you can feed it queries in batch mode using other input sources such as a file, another program, or the command arguments. You can also use copy and paste as a source of query input. This section discusses how to read queries from a file. The next few sections discuss how to take input from other sources. To create a SQL script for mysql to execute in batch mode, put your statements in a text file, then invoke mysql and redirect its input to read from that file: % mysql cookbook < filename Statements that are read from an input file substitute for what you'd normally type in by hand, so they must be terminated with semicolons (or g), just as if you were entering them manually. One difference between interactive and batch modes is the default output style. For interactive mode, the default is tabular (boxed) format. For batch mode, the default is to delimit column values with tabs. However, you can select whichever output style you want using the appropriate command-line options. See the section on selecting tabular or tab- delimited format later in the chapter (Recipe 1.22). Batch mode is convenient when you need to issue a given set of statements on multiple occasions, because then you need not enter them manually each time. For example, batch mode makes it easy to set up cron jobs that run with no user intervention. SQL scripts are also useful for distributing queries to other people. Many of the examples shown in this book can be run using script files that are available as part of the accompanying recipes source distribution (see Appendix A). You can feed these files to mysql in batch mode to avoid typing queries yourself. A common instance of this is that when an example shows a CREATE TABLE statement that describes what a particular table looks like, you'll find a SQL batch file in the distribution that can be used to create (and perhaps load data into) the table. For example, earlier in the chapter, statements for creating and populating the limbs table were shown. The recipes distribution includes a file limbs.sql that contains statements to do the same thing. The file looks like this: DROP TABLE IF EXISTS limbs; CREATE TABLE limbs ( thing VARCHAR(20), # what the thing is legs INT, # number of legs it has arms INT # number of arms it has ); INSERT INTO limbs (thing,legs,arms) VALUES('human',2,2); INSERT INTO limbs (thing,legs,arms) VALUES('insect',6,0); INSERT INTO limbs (thing,legs,arms) VALUES('squid',0,10); INSERT INTO limbs (thing,legs,arms) VALUES('octopus',0,8); INSERT INTO limbs (thing,legs,arms) VALUES('fish',0,0); INSERT INTO limbs (thing,legs,arms) VALUES('centipede',100,0); INSERT INTO limbs (thing,legs,arms) VALUES('table',4,0); INSERT INTO limbs (thing,legs,arms) VALUES('armchair',4,2); INSERT INTO limbs (thing,legs,arms) VALUES('phonograph',0,1);

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost MySQL Web Hosting services

PHP and MySQL Web Development

+——–+ To set a variable explicitly to a

Filed under: MySQL Solutions — webmaster @ 6:39 am

+——–+ To set a variable explicitly to a particular value, use a SET statement. SET syntax uses = rather than := to assign the value: mysql> SET @sum = 4 + 7; mysql> SELECT @sum; +——+ | @sum | +——+ | 11 | +——+ A given variable’s value persists until you assign it another value or until the end of your mysql session, whichever comes first. Variable names are case sensitive: mysql> SET @x = 1; SELECT @x, @X; +——+——+ | @x | @X | +——+——+ | 1 | NULL | +——+——+ SQL variables can be used only where expressions are allowed, not where constants or literal identifiers must be provided. Although it’s tempting to attempt to use variables for such things as table names, it doesn’t work. For example, you might try to generate a temporary table name using a variable as follows, but the result is only an error message: mysql> SET @tbl_name = CONCAT(’tbl_’,FLOOR(RAND( )*1000000)); mysql> CREATE TABLE @tbl_name (int_col INT); ERROR 1064 at line 2: You have an error in your SQL syntax near ‘@tbl_name (int_col INT)’ at line 1 SQL variables are a MySQL-specific extension, so they will not work with other database engines. 1.16 Telling mysql to Read Queries from a File 1.16.1 Problem You want mysql to read queries stored in a file so you don’t have to enter them manually. 1.16.2 Solution Redirect mysql’s input or use the SOURCE command. 1.16.3 Discussion

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost PHP Web Hosting services

Next Page »

Powered by PHP MySQL Web Hosting