[英]Query json_extract with PDO and SQLIte db
Try to adopt JSON in database because i have data not fixed.尝试在数据库中采用 JSON,因为我的数据未修复。 i can query well from terminal, and need to write same query to php script.
我可以从终端很好地查询,并且需要将相同的查询写入 php 脚本。 i have spent a lot of time before ask.
我花了很多时间才问。 example:
例子:
sqlite> select json_extract(events.interni, '$') from events WHERE id='35';
output
[{"student_id":"12","student_name":"Lisa Ochoa"},{"student_id":"21","student_name":"Rafael Royal"}]
where id = 35 will become a variable of $ _POST ['id']其中 id = 35 将成为 $ _POST ['id'] 的变量
what I tried:我尝试了什么:
$result2 = $db->query("select json_extract(events.interni, '$') from events WHERE id='35'");
var_dump($result2->fetchAll(PDO::FETCH_ASSOC));
return [] <- empty array
i want instead = [{"student_id":"21","student_name":"Rafael Royal"}]
where did I go wrong?我在哪里 go 错了?
I followed this answer on SO https://stackoverflow.com/a/33433552/1273715 but i need to move the query in php for an ajax call我在 SO https://stackoverflow.com/a/33433552/1273715上遵循了这个答案,但我需要在 php 中移动查询以获取 Z2705A83A5A0659CCE3458A 调用
possibile another help.可能的另一个帮助。
Can the result fron $ajax call can be usable as key value or remain string? $ajax 调用的结果可以用作键值还是保留字符串? in other hands i can convert string to object like students = new Object()?
在其他方面,我可以将字符串转换为 object,如学生 = new Object()?
eaxaple of what i need in js environment - count objects in array - and loop key value我在 js 环境中需要的 eaxaple - 计算数组中的对象 - 并循环键值
var data = [{"student_id":"12","student_name":"Lisa Ochoa"},{"student_id":"21","student_name":"Rafael Royal"}]
consolle.log(JSON.Stringify(data));
here I would like to avoid the backslash在这里我想避免使用反斜杠
consolle.log(JSON.Stringify(data.lenght));
in this phase the desired data is = 2
any possible help is largely appreciated非常感谢任何可能的帮助
UPDATE
leave json_extract() function i have solved the second problem, so now i can work whit object property, and finally important to count objects in array:离开 json_extract() function 我已经解决了第二个问题,所以现在我可以使用 object 属性,最后对数组中的对象进行计数很重要:
<?php
try {
$db = new PDO('sqlite:eventi.sqlite3');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e) {
echo "I'm sorry, Dave. I'm afraid I can't do that.";
echo $e->getMessage();
}
$risultato = $db->query("SELECT * FROM events WHERE id = '35'", PDO::FETCH_ASSOC);
$result = array();
foreach ($risultato as $row) {
$result[] = $row;
}
// echo "Results: ", json_encode($result), "\n"; this produced backslash
echo $result[0]['interni'];
?>
js part js部分
var num='';
$.ajax({
url: "sqlitedb/test-con.php",
type: 'POST',
dataType: 'json',
success:function(result){
console.log(result[0].student_id+ " - "+ result[0].student_name); // output here is good: 12 - Lisa Ochoa
counter(Object.keys(result).length);
}});
function counter (numero){
console.log("num2: =" + numero);
}
//out put here: 2
perfect!
odd behaviour:奇怪的行为:
console.log(result[0].student_id+ " - "+ result[0].student_name);
12 - Lisa Ochoa
outup is right but outup 是对的,但是
console.log(result.lenght);
output is null
You are surrounding you query with double quotes but inside the query there is an unescaped $
.你用双引号包围你的查询,但在查询中有一个未转义的
$
。
Try escaping it:试试 escaping 它:
$result2 = $db->query("SELECT json_extract(events.interni, '\$') FROM events WHERE id='35'");
var_export($result2->fetchAll(PDO::FETCH_ASSOC));
You can try something like this.你可以尝试这样的事情。 and since you said in the comment about approaching it with ajax.
并且由于您在评论中说要使用 ajax 来接近它。 I have included that also.
我也包括在内。
I also include php mysql backend workability for clarity.为了清楚起见,我还包括 php mysql 后端可操作性。 so Yo have now two options
所以你现在有两个选择
1.) PHP WITH MYSQL 1.) PHP 与 MYSQL
2.) PHP WITH SQLITE as you requested 2.) PHP 与 SQLITE根据您的要求
index.html索引.html
<script src="jquery-3.1.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
type: 'get',
url: 'data.php',
dataType: 'JSON',
cache:false,
success: function(data){
var length = data.length;
for(var s=0; s<length; s++){
var student_id = data[s].student_id;
var student_name = data[s].student_name;
var res = "<div>" +
"<b>student_id:</b> " + student_id + "<br>" +
"<b>student_name:</b> " + student_name + "<br>" +
"</div><br>";
$("#Result").append(res);
}
}
});
});
</script>
<body>
<div id="Result" ></div>
</body>
In mysql database you can do it this way.在mysql 数据库中,您可以这样做。
<?php
$host = "localhost";
$user = "ryour username";
$password = "your password";
$dbname = "your bd name";
$con = mysqli_connect($host, $user, $password,$dbname);
// Check connection
if (!$con) {
echo "cannot connect to db";
}
$return_arr = array();
$query = "SELECT id, student_id, student_name FROM events where id='35'";
$result = mysqli_query($con,$query);
while($row = mysqli_fetch_array($result)){
$student_id = $row['student_id'];
$student_name = $row['student_name'];
$return_arr[] = array("student_id" => $student_id,
"student_name" => $student_name);
}
// Encoding array in JSON format
echo json_encode($return_arr);
?>
So with sqlitedb something like this will work for you所以使用sqlitedb这样的东西对你有用
$return_arr = array();
$result2 = $db->query("SELECT id, student_id, student_name FROM events where id='35'");
$result2->execute(array());
//$result2 = $db->query("SELECT * FROM events where id='35'");
//$result =$result2->fetchAll(PDO::FETCH_ASSOC));
while($row = $result2->fetch()){
$student_id = $row['student_id'];
$student_name = $row['student_name'];
$return_arr[] = array("student_id" => $student_id,
"student_name" => $student_name);
}
// Encoding array in JSON format
echo json_encode($return_arr);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.