Query MySQL to connect 2 databases
I have two tables: one hostgroup_host and the other one hostgroups. The hostgroups represents the names of all the host groups and has 3 important values (hostgroup_id, name and alias). The hostgroup_host makes the connection between the groups and the hosts and has two values (hostgroup_id and host_id). Example: hostgroup_id = 1
and host_id = 3
it means that host which id is number 3 belongs to hostgroup with id = 1
(with a specific name).
I have this:
$name=$_POST['name']; $alias=$_POST['alias']; $address=$_POST['address']; $hostgroup=$_POST['hostgroup'];
mysql_connect('localhost:/usr/local/groundwork/mysql/tmp/mysql.sock', $username ,$password); @mysql_select_db($database) or die( "Unable to select database");
$query = "INSERT INTO hosts (host_id, name, alias, address, hosttemplate_id) VALUES ('','$name','$alias','$address', '1'); INSERT INTO hostgroup_host (hostgroup_id, host_id) VALUES ((select hostgroup_id from hostgroups where name='$hostgroup'), (SELECT host_id from hosts where name = '$name'))"; mysql_query($query);
mysql_close(); ?>
Why doesn't work?
well, just join your insert statements: something like:
INSERT INTO TABLE_A (id,name,alias);
INSERT INTO TABLE_B (id,name);
the ; is seperating commands in my sql, so basicly when you will run the query, both of the insert statements will be run.
I'm a bit confused, but just use more than one link resource.
$linka = mysql_connect('server1', 'mysql_user', 'mysql_password');
$linkb = mysql_connect('server2', 'mysql_user', 'mysql_password');
Then use
$result = mysql_query('INSERT...', $linka);
or
$result = mysql_query('INSERT...', $linka);
But I fear I may have misunderstood your question completely...
Am I missing something or wouldn't it just be better to have the hostgroup_id and the host_id as the option values in the dropdowns?
<select name="hosts">
<option value="1">Host 1</option>
...
</select>
<select name="hostgroups">
<option value="15">Host with 15 as ID</option>
...
</select>
then just insert the post values
链接地址: http://www.djcxy.com/p/69596.html下一篇: 查询MySQL连接2个数据库