0

Here is the code is written is 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(); ?>

It gives me this error :

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

I need this because i want to write data from android app using JSON. Could anyone tell me what is wrong or to give me some tips ?

4
  • 1
    Maybe you wanted to use $output instead of $out ? Commented May 11, 2012 at 17:34
  • 1
    The error messages PHP gives you are helpful, clear, and informative. Did you try reading them? Commented May 11, 2012 at 17:35
  • From what I see, $out is not defined as an array anywhere. What should $out be? Commented May 11, 2012 at 17:35
  • 2
    Care with SQL injections. Commented May 11, 2012 at 17:40

3 Answers 3

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

If $out["radars"] is an array, this is fine. If it isn't, you'll get a bug: Invalid argument supplied for foreach.

In your case $out["radars"] doesn't exist at all. In fact $out doesn't exist at all. So you get another bug: Undefined variable out.

You're initialising a variable $output but then trying to use it as $out. That won't work.

To pull data out of the database, encode it as JSON, and output it:

$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);

And to receive JSON posted to the server, process it, and add it to the database:

// 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.
Sign up to request clarification or add additional context in comments.

9 Comments

Notice: Undefined index: radars in C:\xampp\htdocs\RadarsMySql.php on line 13 Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\RadarsMySql.php on line 13
that's what i get after i modified out with ouput. It's ok ?
@stanga. I don't know what $output is. Try print_r($output); exit; and add that to the question.
Actually, scrap that. I'm going to completely rewrite my answer.
Ok. I don't really understand what you say but I wonder if you know a tutorial or you could say me what should I do / write there the enstablish a connection between android application and mysql database ?
|
0

Where are you defining $out? ;)

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

1 Comment

... nowhere. I'm a newbie in mysql/php programming. Could you help me with this ? I have 3 columns latitude , longitude , description . How could i write the php to allow me posting from android app ?
0

use

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

which you have created above print statement

1 Comment

it's ok if I only receive a notice and a warning ? no errors ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.