<?php
//Una simple conexión a traves del objeto mysqli de php
$con = new mysqli("localhost","root","","store");$query ="select * from products";//teniendo en cuenta que es tu tabla en tu bd
$result = $con->query($query);
$output = array();
while($row = $result->fetch_array()){ $output[] = array(
"id"=> $row[0],
"nombre_producto"=>$row[1],
"cantidad"=>$row[5]
);
}
/**
* Los metodos _isascii, _httpencode, _chezkoutput
* y Output son sacados de la clase fpdf lecenciada
* bajo GNU license del author
* Author: Olivier PLATHEY
* Contrib:OsChez
*/
function _isascii($s) { // Test if string is ASCII
$nb = strlen($s);
for ($i = 0; $i < $nb; $i++) { if (ord($s[$i]) > 127)
return false;
}
return true;
}
function _httpencode($param, $value, $isUTF8) { // Encode HTTP header field parameter
if (_isascii($value))
return $param . '="' . $value . '"';
if (!$isUTF8)
$value = utf8_encode($value);
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false)
return $param . '="' . rawurlencode($value) . '"';
else
return $param . "*=UTF-8''" . rawurlencode($value);
}
function _checkoutput() { if (PHP_SAPI != 'cli') {}//aquí usaba una funcion global php if (ob_get_length()) { // The output buffer is not empty
if (preg_match('/^(\xEF\xBB\xBF)?\s*$/', ob_get_contents())) { // It contains only a UTF-8 BOM and/or whitespace, let's clean it
ob_clean();
} else
echo "Some data has already been output, can't send JSON data";//auqí llamaba a un metodo de clase
}
}
function Output($output=null,$isUTF8 = false) { // Send to standard output
_checkoutput();
if (PHP_SAPI != 'cli') { // We send to a browser, este es el comentario original
//las siguientes 4 cabeceras son la que permiten la
//petición desde un orígen difernete
header('content-type: application/json; charset=utf-8'); header('Access-Control-Allow-Origin: *'); header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept"); header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE'); }
echo json_encode($output);
return '';
}
Output($output);