[英]PHP mysql compare table rows with json array
I have a mysql table with a firstname, lastname and email which I want to compare with a google spreadsheet array (json) with the same table headers (firstname, lastname, email). 我有一个具有名字,姓氏和电子邮件的mysql表,我想与具有相同表头(名字,姓氏,电子邮件)的google电子表格数组(json)进行比较。 If the records match to eachother I want to update my table.
如果记录彼此匹配,我想更新表。 For example: I have 'John Doe' in mysql table and 'John Doe' in my google spreadsheet: If this is true I want to update a certain value to this mysql table row.
例如:我在mysql表中有“ John Doe”,而在我的Google电子表格中有“ John Doe”:如果为true,我想为此mysql表行更新某个值。
My code so far, all rows are getting an update because there is one comparison true. 到目前为止,我的代码中所有行都进行了更新,因为有一个比较正确。 I only want the specific rows (Maybe with a foreach?):
我只想要特定的行(也许有foreach?):
$sql = "SELECT * FROM student WHERE form=''"; //select from db
$result = $conn->query($sql);
while($row = mysqli_fetch_array($result)) //fetch it
{
$firstname= $row['firstname'];
$lastname= $row['lastname'];
$email = $row['email'];
$url = 'https://spreadsheets.google.com/feeds/list/*MY-KEY*/od6/public/values?alt=json'; // my spreadsheet (key is inserted with me)
$file= file_get_contents($url);
$json = json_decode($file);
$ress = $json->{'feed'}->{'entry'};
foreach($ress as $res) {
$fname = $res->{'gsx$naam'}->{'$t'};
$lname = $res->{'gsx$achternaam'}->{'$t'};
$mailing = $res->{'gsx$email'}->{'$t'};
}
if ($firstname.$lastname == $fname.$lname || $email == $mailing) {
$sql2 = "UPDATE student SET form=1";
if ($conn->query($sql2) === TRUE) {
echo "inserted";
}
}
}
I think you need to change the for loop and the if statement like this 我认为您需要像这样更改for循环和if语句
foreach($ress as $res) {
$fname = $res->{'gsx$naam'}->{'$t'};
$lname = $res->{'gsx$achternaam'}->{'$t'};
$mailing = $res->{'gsx$email'}->{'$t'};
if ($firstname.$lastname == $fname.$lname || $email == $mailing) {
$sql2 = "UPDATE student SET form=1";
if ($conn->query($sql2) === TRUE) {
echo "inserted";
}
// to break from for loop when the record has been found
break;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.