开发者

Check whether data is at table

开发者 https://www.devze.com 2023-03-12 01:01 出处:网络
I asked something about in_array() and I already got that working. But now I have a different problem:

I asked something about in_array() and I already got that working. But now I have a different problem:

I have a table that says which services are assigned to hosts: services_hosts(service_id, host_id).

How can I see if the service that is selected is already assigned开发者_Python百科 to that host, also selected? Basically, I want to see if the specific line (service_id, host_id) already exists in that table.

EDIT: The problem is that I want to compare in a separate file that has functions that connect to DB:

    function addServiceToHost($service_name, $host_id)
{
$query = "INSERT INTO monitoring_hosts_services (service_id, host_id) values ((SELECT service_id FROM monitoring_services WHERE name = '".$service_name."'), '".$host_id."')";
$result = @pg_exec($this->conn, $query);
    if ($row = pg_fetch_row($result))
    {
    "blabla error msg"
    exit;
    }
    return $this->parseResultObj($result);
}


I might not unserstand your question correctly but would this do the trick:

SELECT * FROM ServerHostsTable WHERE service_id = '5' AND host_id = '8'


$query = ("SELECT * FROM ServerHostsTable WHERE service_id = '5' AND host_id = '8'")

if(mysql_num_rows($query)>0)
{
     //the item is in the db
}
else
{
     //not in the db
}

hope this helps


your question is not very clear, but from what I understand you want to test if a specific row is inserted into a database table. you could do this like this:

$result=mysql_query("SELECT service_id FROM services_hosts WHERE service_id=$theserviceid AND host=$thehostid");
if($row=mysql_fetch_row($result){
 echo "already in the db";
} else {
 echo "not in the db!";
}


I would run this query.

$sql = "SELECT COUNT(*) AS ret\n";
$sql.= "FROM services_hosts\n";
$sql.= "WHERE service_id = $service_id\n";
$sql.= "AND host_id = $host_id";

The result should be one row with one field (named ret):

  • 0 - the service is not present on the host
  • 1 - service is present on the host
  • enything else - there is a problem with table in database
0

精彩评论

暂无评论...
验证码 换一张
取 消