基于if语句PHP检查MySQL列内容插入
这段代码正在检查我的sql server中“Location”列的内容,若内容是“inside”,则应该将其更改为“outside”,反之亦然。然后,它将字符串“外部/内部”发送到sendText方法,该方法将利用文本消息中的响应 问题是服务器中没有数据被更改,连接没有错误,因此它连接良好,但无法插入数据库 在运行这个PHP文件时,除了OutputReturnCode 1之外,我没有得到任何反馈——但这是来自curl实现的,它告诉我文本没有发送 感谢您的帮助, 干杯 问题是服务器中没有数据被更改,连接没有错误,因此它连接良好,但无法插入数据库 这是因为代码中没有基于if语句PHP检查MySQL列内容插入,php,mysql,curl,Php,Mysql,Curl,这段代码正在检查我的sql server中“Location”列的内容,若内容是“inside”,则应该将其更改为“outside”,反之亦然。然后,它将字符串“外部/内部”发送到sendText方法,该方法将利用文本消息中的响应 问题是服务器中没有数据被更改,连接没有错误,因此它连接良好,但无法插入数据库 在运行这个PHP文件时,除了OutputReturnCode 1之外,我没有得到任何反馈——但这是来自curl实现的,它告诉我文本没有发送 感谢您的帮助, 干杯 问题是服务器中没有数据被更改
mysqli\u query()
解决方案
$con = mysqli_connect($host,$uname,$opw,$odb); //database login details
// Check connection
if (mysqli_connect_errno()) //if you cannot access the database
{
echo "Failed to connect to MySQL: " . mysqli_connect_error(); //useful error message
}
$result = mysqli_query($con,"SELECT * FROM PetLoc");
if (!$result) { // add this check.
die('Invalid query: ' . mysqli_error());
}
while($row = mysqli_fetch_assoc($result))
{
if($row['Location'] == 'inside')
{
$sql = ("INSERT INTO PetLoc (Location) VALUES ('outside')");
sendText("outside");
}
else
{
$sql =("INSERT INTO PetLoc (Location) VALUES ('inside')");
sendText("inside");
}
}
function sendText($petLoc) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "smsserver");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
$data = array(
'mphone' => 'phoneno',
'smstext' => 'Your pet walked through the door flap, it was last seen '.$petLoc.'.',
'username' => 'uname'
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
curl_close($ch);
echo "Output".$output ;
}
mysqli_close($con);
问题是服务器中没有数据被更改,连接没有错误,因此它连接良好,但无法插入数据库
这是因为代码中没有mysqli\u query()
解决方案
$con = mysqli_connect($host,$uname,$opw,$odb); //database login details
// Check connection
if (mysqli_connect_errno()) //if you cannot access the database
{
echo "Failed to connect to MySQL: " . mysqli_connect_error(); //useful error message
}
$result = mysqli_query($con,"SELECT * FROM PetLoc");
if (!$result) { // add this check.
die('Invalid query: ' . mysqli_error());
}
while($row = mysqli_fetch_assoc($result))
{
if($row['Location'] == 'inside')
{
$sql = ("INSERT INTO PetLoc (Location) VALUES ('outside')");
sendText("outside");
}
else
{
$sql =("INSERT INTO PetLoc (Location) VALUES ('inside')");
sendText("inside");
}
}
function sendText($petLoc) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "smsserver");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
$data = array(
'mphone' => 'phoneno',
'smstext' => 'Your pet walked through the door flap, it was last seen '.$petLoc.'.',
'username' => 'uname'
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
curl_close($ch);
echo "Output".$output ;
}
mysqli_close($con);