tags:

views:

60

answers:

4
C:\>mysql -uroot -padmin
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.0.37-community-nt-log MySQL Community Edition (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> show databases
    -> ;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| attendance         |
| fusionchartsdb     |
| mysql              |
| sugarcrm           |
| test               |
+--------------------+
6 rows in set (0.09 sec)

mysql> use mysql;
Database changed
mysql>

So I want to start a process by mysql -uroot -padmin,and in that process I want to run these statements:

 show databases
 use mysql
 insert xxx (..) values (..)

How to do it in PHP?

A: 

You can write your own console or use ready-made one called PHPMyAdmin. If your intention is just to interact with mysql server using web-frontend.

Although writing your own could be tricky. Unfortunately, HTTP servers are not interactive. HTTP is stateless protocol. So, you have to find a way to save a state between requests. One of solutions is to use persistent connection. But with goal being unclear it's hard to advise anything certain.

Col. Shrapnel
A: 

I don't think I follow your question completely...

If you're trying to execute statements in MySQL in PHP, you can use the mysql_* functions.

If you just want to execute a series of commands via the command line, you can use

`mysql -u root -p admin --execute "show databases; use mysql; insert ...."`

If you want to write your own interactive replacement for the MySQL command line tools in PHP, you're going to need a hell of a lot of time and perseverance. Start by reading about PHP's CLI: http://php.net/manual/en/features.commandline.php

meagar
The above is just a simplied example for what I want to do,but the essence is the same. Say what I actually want to do has nothing to do with MySQL.So that kinda hack won't work.
@user198729 it is not interactive though
Col. Shrapnel
The key difficulty is how to stick to a process and get its input and output stream?
The answer is simple: noway for the HTTP
Col. Shrapnel
I haven't mentioned anywhere about HTTP,seems...
uh oh, how discouragingly. well, if you're talking of CLI version of PHP, why not to use CLI version of mysql console itself?
Col. Shrapnel
Hey,take a look at my first comment:)
The link in my answer has information on reading from `STDIN` with PHP on the command line. Check under 'I/O Streams'.
meagar
I notice you edited it. Your task still has no sense. If you can use PHP to interact with console app, you can interact with this app itself.
Col. Shrapnel
A: 

The MySQL client uses the readline() library, which is available to PHP as well.

PHP's PEAR commandline app (invoked when you run pear install <package> processes interactive command line input in a similar manner, but without using the full readline library. There's some of the input-reading source here. The key line being $line = fgets(STDIN, 2048);

Frank Farmer
A: 

You can do so via proc_open.

$bin = 'path/to/mysql.exe';
$cmd = $bin . ' -ulocalonly';
$descriptors = array(
  0 => array('pipe', 'r'), 
  1 => array('pipe', 'w'),
  2 => array('pipe', 'r')
);

$process=proc_open($cmd, $descriptors, $pipes, dirname($bin));
if (is_resource($process)) {
  // ....

}

The tricky part is to parse the output of the other process (from $pipes[0] and $pipes[2]) and react on all possible situations (including error handling). You might want to look into the expect pecl extension for that.

VolkerK
Do you mean `expect` can be used to parse `$pipes[0]` and `$pipes[1]` or do you mean with the help of `expect` I don't even have to bother to use `proc_open` ?
The former.....
VolkerK
I've heard of expect long ago but never know how to get started with it yet:(
Do you mean `$cmd` by **the other process** ?
Sorry, I don't understand the last comment.
VolkerK
You said `The tricky part is to parse the output of the other process`,here do you mean `$cmd` by **the other process**?
yes, the process started by proc_open
VolkerK
I don't see the reason to use `expect`,isn't *regular expression* enough to parse the output?
`expect` is a toolkit for handling those expressions. You don't _have_ to use it. I only said one might be interested in it.
VolkerK