this is the source code to convert MYSQL query result into JSON in PHP, it is great for NOSQL data processing.
<?php
/*** mysql hostname ***/
$hostname = 'localhost';
/*** mysql username ***/
$username = '';
/*** mysql password ***/
$password = '';
$db = '';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
/*** echo a message saying we have connected ***/
/*** The SQL SELECT statement ***/
/*** close the database connection ***/
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
<? $var = array();
$STH = $dbh->query("SELECT * FROM u_user WHERE U_ID = '1'");
$STH->setFetchMode(PDO::FETCH_OBJ);
while($row = $STH->fetch()) {
$var[] = $row;
}
echo '{"users":'.json_encode($var).'}';
?>
<?php
/*** mysql hostname ***/
$hostname = 'localhost';
/*** mysql username ***/
$username = '';
/*** mysql password ***/
$password = '';
$db = '';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
/*** echo a message saying we have connected ***/
/*** The SQL SELECT statement ***/
/*** close the database connection ***/
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
<? $var = array();
$STH = $dbh->query("SELECT * FROM u_user WHERE U_ID = '1'");
$STH->setFetchMode(PDO::FETCH_OBJ);
while($row = $STH->fetch()) {
$var[] = $row;
}
echo '{"users":'.json_encode($var).'}';
?>
沒有留言:
張貼留言