0

这里是写的代码是RadarsMySql.php

 <?php
 $con = mysql_connect("localhost","root","dacian");


 $db = mysql_select_db("radars");

 $sql = mysql_query("SELECT latitude,longitude,description FROM radars WHERE id > '0'");

 while($row=mysql_fetch_assoc($sql))
 $output[]=$row;
 print(json_encode($output));
 foreach($out["radars"] as $radars) { 
 $latitude = addslashes($radars[latitude]); 
 $longitude= addslashes($radars[longitude]); 
 $description = addslashes($radars[description]);

 mysql_query("INSERT INTO radars (latitude, longitude, description) VALUES('$name', '$ingredients', '$ingredients2')") or die (mysql_error()); 
 }

  mysql_close(); ?>

它给了我这个错误:

Notice: Undefined variable: out in C:\xampp\htdocs\RadarsMySql.php on line 13

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\RadarsMySql.php on line 13

我需要这个,因为我想使用 JSON 从 android 应用程序写入数据。谁能告诉我出了什么问题或给我一些提示?

4

3 回答 3

2
foreach ($out["radars"] as $radars) {}

如果$out["radars"]是一个数组,这很好。如果不是,你会得到一个错误:Invalid argument supplied for foreach.

在你的情况下$out["radars"]根本不存在。事实上$out根本不存在。所以你得到另一个错误:Undefined variable out.

您正在初始化一个变量$output,但随后尝试将其用作$out. 那是行不通的。

要从数据库中提取数据,请将其编码为 JSON,然后输出:

$sql = 'SELECT latitude,longitude,description FROM radars WHERE id>0'
$result = mysql_query($sql);

$rows = array();
while ($row = mysql_fetch_assoc($sql)) $rows[] = $row;
echo json_encode($rows);

并接收发布到服务器的 JSON,对其进行处理,并将其添加到数据库中:

// It's being posted as application/json, not as application/x-www-form-urlencoded, so it won't populate the $_POST array.
if ($json = file_get_contents('php://input')) {
    // Never mind. We'll do it ourselves.
    $a = json_decode($json, true); // Now we have a nice PHP array.
    $insert = '';
    foreach ($a as $v) {
        if ($insert) $insert .= ',';
        $insert .= ' ("' . $d->escape($v['lattitude']) . '", "' . $d->escape($v['longitude']) . '", "' . $d->escape($v['description']) . '")';
    }
    $sql = 'INSERT INTO `radars` (`latitude`, `longitude`, `description`) VALUES' . $insert;
    $d->exec($sql);
}

// I'm assuming you have a database class here, $d.
// $d->exec() could be simply mysql_query() or mysqli_query().
// $d->escape() should be mysql_real_escape_string() or mysqli_real_escape_string(), but both of those functions will fail if a database connection is not currently open.
// So $d->escape() should (a) check whether a connection is currently open, (b) open one if not, (c) perform the escape and return the result.
于 2012-05-11T17:34:06.527 回答
0

你在哪里定义 $out?;)

foreach($out["radars"] as $radars) { 
于 2012-05-11T17:32:47.917 回答
0

利用

     foreach($output["radars"] as $radars) { 

您在打印语句上面创建的

于 2012-05-11T17:36:46.363 回答