Php 从JSON返回结果,插入mysql表,然后通过电子邮件发送结果
我需要帮助,了解如何将JSON数组返回的结果插入sql表,然后通过电子邮件发送返回的结果 下面是迄今为止我的php脚本,它成功地将查询结果放入JSON数组Php 从JSON返回结果,插入mysql表,然后通过电子邮件发送结果,php,mysql,json,sendmail,Php,Mysql,Json,Sendmail,我需要帮助,了解如何将JSON数组返回的结果插入sql表,然后通过电子邮件发送返回的结果 下面是迄今为止我的php脚本,它成功地将查询结果放入JSON数组 <?php // set up the connection variables $db_name = 'dbanme'; $hostname = 'host'; $username = 'username'; $password = 'password'; // connect t
<?php
// set up the connection variables
$db_name = 'dbanme';
$hostname = 'host';
$username = 'username';
$password = 'password';
// connect to the database
$dbh = new PDO("mysql:host=$hostname;dbname=$db_name", $username, $password);
// a query get all the records from the users table
$sql = 'SELECT tabel1.id,table1.type FROM table1 LEFT JOIN table2 ON table1.id=table2.id WHERE table1.id NOT IN ( SELECT table2.id FROM table2)';
// use prepared statements, even if not strictly required is good practice
$stmt = $dbh->prepare( $sql );
// execute the query
$stmt->execute();
// fetch the results into an array
$result = $stmt->fetchAll( PDO::FETCH_ASSOC );
// count number of results returned in the array
$countresults = count($result);
echo $countresults;
if ( $countresults > 0 ) {
// convert to json
$json = json_encode( $result );
// decode the results returned and insert them into table 2
// email the results returned
}
else {
echo ' no results found';
}
?>
更新:
表1结构:
ID INT 10
VARCHAR50型
表2结构:
ID INT 10
VARCHAR50型
我意识到我不需要将结果编码为JSON,但我仍然无法获取代码,以获取从数组返回的结果并将其插入tabl2,然后立即通过电子邮件发送结果。首先,您必须告诉我们为什么您首先将结果转换为JSON。您已经有了一个数组,在您的案例中不需要JSON字符串 无论如何,将JSON字符串转换回如下数组:
$yourArray = json_decode($json)
$sql = 'INSERT INTO table2(id, type) VALUES (:id, :type)';
$sql = 'INSERT INTO table2(id, type) VALUES (:id, :type)';
$stmt->bindParam(':id', $yourArray ['id']);
$stmt->bindParam(':type', $yourArray['type']);
$stmt = $dbh->prepare( $sql );
$stmt->execute();
现在您说要将数据插入表2中。我不知道您的表是什么样子,但如果我看一下您的代码,我想您的sql应该是这样的:
$yourArray = json_decode($json)
$sql = 'INSERT INTO table2(id, type) VALUES (:id, :type)';
$sql = 'INSERT INTO table2(id, type) VALUES (:id, :type)';
$stmt->bindParam(':id', $yourArray ['id']);
$stmt->bindParam(':type', $yourArray['type']);
$stmt = $dbh->prepare( $sql );
$stmt->execute();
因此,您的代码如下所示:
$yourArray = json_decode($json)
$sql = 'INSERT INTO table2(id, type) VALUES (:id, :type)';
$sql = 'INSERT INTO table2(id, type) VALUES (:id, :type)';
$stmt->bindParam(':id', $yourArray ['id']);
$stmt->bindParam(':type', $yourArray['type']);
$stmt = $dbh->prepare( $sql );
$stmt->execute();
如果要通过邮件发送结果,只需使用默认的邮件功能:
$to = 'RECIEVER@MAIL'
$subject = 'SUBJECT';
$message='Your values are: '.$yourArray ['id'].' - '.$yourArray['type'];
mail($to,$subject,$message);
如果默认邮件功能不起作用,出于安全原因,服务器有时可能不允许使用该功能。在这种情况下,我建议使用第三方库,如
您的问题没有透露太多信息,我尽了最大努力,但上面的代码更多的是伪代码。您可以将结果用于电子邮件,并添加一个查询,将行直接插入表2中。如果你要做的就是把它们寄回去,那么似乎没有必要对它们进行解码。谢谢你迄今为止的帮助。您是对的,没有必要将结果编码为JSON。O尝试了您给我的代码,但我得到了一个语法错误-解析错误:语法错误,意外的“$sql”T_变量上面的代码不是一个完整的解决方案,复制/粘贴将不起作用。这是为了把你推向正确的方向。我想我的代码可能有一些错误,尽管我没有看到。如果有人发现错误,请改正。