我想只在互联网工作时才更新mysql字段..... 要检查互联网,我的脚本是
<?php
//function to check if the local machine has internet connection
function checkConnection()
{
//Initiates a socket connection to www.itechroom.com at port 80
$conn = @fsockopen("www.google.com", 80, $errno, $errstr, 30);
if ($conn)
{
$status = "Connection is OK";
fclose($conn);
}
else
{
$status = "NO Connection<br/>\n";
$status .= "$errstr ($errno)";
}
return $status;
}
echo checkConnection();
?>
如果连接正常,我想运行此查询,否则不运行它
<?php
/////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////
$con=mysqli_connect("localhost","root","","ex_smartcard2013");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_query($con,"UPDATE fess SET status=1 WHERE status=0 order by id desc limit 1");
mysqli_close($con);
?>
怎么做............
答案 0 :(得分:2)
使用这样的功能。
<?php
//function to check if the local machine has internet connection
function checkConnection()
{
//Initiates a socket connection to www.itechroom.com at port 80
$conn = @fsockopen("www.google.com", 80, $errno, $errstr, 30);
if ($conn)
{
$status = true;
}
else
{
$status = false;
//$status .= "$errstr ($errno)";
}
fclose($conn);
return $status;
}
if(checkConnection()) {
$con=mysqli_connect("localhost","root","","ex_smartcard2013");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_query($con,"UPDATE fess SET status=1 WHERE status=0 order by id desc limit 1");
mysqli_close($con);
}
?>
答案 1 :(得分:0)
修改Salim's answer和JTC's notation,我们得到
function checkConnection() {
return $conn = @fsockopen("www.google.com", [80|443]);
}
if (checkConnection()) {
$con = mysqli_connect(...);
// the rest is yours
}
如果您希望如上所述使用自己的checkConnection()
,那么只需测试适当的条件。你期望它回归什么?
$hasInternet = checkConnection();
if ($hasInternet == "...") {
// ...
}
另外还有一个注意事项:如果您的互联网连接已启动,但您无法访问DNS服务,则@fsockopen
会对您不利。出于这个原因,我总是喜欢4.2.2.4
来ping某事,看看我能不能......