[英]Returned results from JSON to insert into mysql table and then email the results
I need help with how to insert the returned results of a JSON array into an sql table and then email the results returned. 我需要有关如何将JSON数组的返回结果插入到sql表中,然后通过电子邮件将返回的结果发送给我的帮助。
Below is my php script so far that successfully gets the results of a query into a JSON array 到目前为止,以下是我的php脚本,可以成功将查询结果获取到JSON数组中
<?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';
}
?> ?>
UPDATE : Table 1 structure : ID INT 10 Type VARCHAR 50 更新:表1结构:ID INT 10类型VARCHAR 50
Table 2 structure : ID INT 10 Type VARCHAR 50 表2结构:ID INT 10类型VARCHAR 50
I realised I dont need to encode result into JSON but I still cant get the code to get the results returned from an array and insert them into tabl2 and then email the results straight after. 我意识到我不需要将结果编码为JSON,但是我仍然无法获取代码来获取从数组返回的结果并将其插入tabl2,然后直接通过电子邮件发送结果。
First you have to tell us why you converted the results to json in the first place. 首先,您必须告诉我们为什么首先将结果转换为json。 You already have an array, you don't need a JSON string in your case. 您已经有一个数组,在这种情况下不需要JSON字符串。
Anyhow, you convert a JSON string back to an array like this: 无论如何,您将JSON字符串转换回这样的数组:
$yourArray = json_decode($json)
Now you say you want to insert the data into table two. 现在,您说您要将数据插入表二中。 I don't know how your table looks like, but if I look at your code, I guess your sql would look something like this: 我不知道你的表是什么样子,但是如果我看一下你的代码,我猜你的sql看起来像这样:
$sql = 'INSERT INTO table2(id, type) VALUES (:id, :type)';
So your code would look something like this: 因此,您的代码将如下所示:
$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();
If you want to send the results via mail, you just use the default mail function: 如果要通过邮件发送结果,则只需使用默认的邮件功能:
$to = 'RECIEVER@MAIL'
$subject = 'SUBJECT';
$message='Your values are: '.$yourArray ['id'].' - '.$yourArray['type'];
mail($to,$subject,$message);
If the default mail function doesn't work, your server may not allow it (sometimes for security reasons). 如果默认邮件功能不起作用,则您的服务器可能不允许使用它(有时出于安全原因)。 In this case I recommend using a third party library like PHPMailer 在这种情况下,我建议使用第三方库,例如PHPMailer
Your question did not reveal many informations, I did the best I could but the code above is still more of a pseudocode. 您的问题并未揭示很多信息,我已尽力而为,但以上代码仍然是伪代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.