-1

how can I send multiple data from mysql to a php array and echo the first two entries? In my database I have the columns name, xkoord, ykoord. I have tried with json, but I guess this doesn't work. What I have is the following:

function load_db($var, $xkoord, $ykoord){
global $db;
$result = mysqli_query($db,"SELECT $var FROM armydb WHERE xkoord = '$xkoord' AND ykoord = '$ykoord'") OR DIE ('Fehler!'); 
$json = mysqli_fetch_all ($result, MYSQLI_ASSOC);
echo json_encode($json );
}

I call this function this way:

<?php 
$name_array = json_decode(load_db('name', 1, 0), true);
echo $name_array[0];
echo $name_array[1];
?>

This doesnt work. Any Suggestion?

1
  • If those variables come from user input you should be using prepared statements or you will leave yourself open to SQL injection. Commented Oct 2, 2014 at 15:11

3 Answers 3

1

You need to use return, not echo:

return json_encode($json);

echo just prints its argument, it doesn't return it to the caller of the function.

Also, $name_array will be a 2-dimensional array: the first dimension is the rows of the results, the second dimension is an associative array of the columns. So $array[0] is an array, you can't usefully echo that. You should do:

echo $name_array[0]['name'];
echo $name_array[1]['name'];

Why are you using json_encode() in the first place? Why not just return the array?

0
0

try

function load_db($var, $xkoord, $ykoord){
global $db;
$result = mysqli_query($db,"SELECT $var FROM armydb WHERE xkoord = '$xkoord' AND ykoord = '$ykoord'") OR DIE ('Fehler!'); 
$resultset = mysqli_fetch_all ($result, MYSQLI_ASSOC);
return $resultset;
}

print_r(load_db('name', 1, 0));

you don't have to encode the result to json since you can just return the data and pass it between functions etc.

0

Lets make it easier

<?php
$query = mysql_query("select * from armydb where xkoords = '$xcords' and ycords = '$cords'");
// now if you need 1 row;
$row = mysql_fetch_assoc($query);
echo $row['name']; 

// if u need more then one 
while ($row = mysql_fetch_assoc($query)) {
 echo $row['name'];
}

?>
4
  • If I do so, and the first name in my db is Dasy, i only get a D echoed, not the complete text, why is that and what can I do to get the complete name? Commented Oct 2, 2014 at 15:51
  • @SaschaKrause You would get that if you did echo $row['name'][0]. Indexing a string returns the character at that position. Commented Oct 2, 2014 at 19:30
  • name should be quoted: echo $row['name']. Commented Oct 2, 2014 at 19:31
  • Added quote to name tx @barmar. Sascha be sure you are using exacly code. i thing you use $row['name'][0]; you just have to use $row['name']. If you want to use more then 1 row i suggest use while. Commented Oct 3, 2014 at 8:22

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.