mysql 引用php中變量查詢

沒有變量的sql查詢:

$sql=" select *  from ajaxtesttb where city='chengdu' and state='china' ";

有變量的sql查詢:在字符串中用PHP變量的方法是變量外面用大括號

$sql=" select *  from ajaxtesttb where city='{$city}' and state='{$state}' ";


$host="192.168.21.103";
$sql="select a.host_object_id,a.display_name host_name,a.address,c.display_name,b.state,b.start_time,b.end_time,b.output from nagios_hosts a,nagios_servicechecks b,nagios_services c where a.host_object_id=c.host_object_id and b.service_object_id=c.service_object_id and ( c.display_name='Current Load' or c.display_name='check-load') and a.address='{$host}' order by b.servicecheck_id desc limit 0,38";


整個sql語句要用""雙引號括起來,然後php變量用單引號加大括號的形式:'{$host}'

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章