Nevarim Nevarim - 3 years ago 107
JSON Question

blank result with - in text in cell in php and json

i have a problem in a file with json estraction:

if in database i have a field with a - inside result for json is null

this is the code

<?php
$host = "****";
$user = "****";
$password = "*****";
$db = "****";
$table=$_GET['table'];


//open connection to mysql db
$connection = mysqli_connect($host,$user,$password,$db) or die("Error " .
mysqli_error($connection));

//fetch table rows from mysql db
$sql = "select * from ".$table.";";
$result = mysqli_query($connection, $sql) or die("Error in Selecting " .
mysqli_error($connection));

//create an array
$emparray = array();
while($row =mysqli_fetch_assoc($result))
{
$emparray[] = $row;
}
echo json_encode($emparray);

//close the db connection
mysqli_close($connection);
?>


thanks
mauro
p.s. the - is inside the cell of a row

for example:
Enemies take –1 on attack rolls and saves against ...

symbols around tables are not useful :(

i know is weak for hackers but for now is private :D

Answer Source

Treat the table name as a STRING using the backtick characters (`) around the table name:

$sql = "select * from `".$table.";`";

Howerver; Do never do things like this on a site reachable via the internet!!!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download