/ Published in: PHP
This is a public jsonp api. I use it with jQuery Like This:
$.getJSON("http://yourdomain/yourpath/jsonp_api.php?method=getSchema¶ms=your_db_name&jsoncallback=?",
function(data){
//DO SOMETHING WITH THE DATA HERE
}
);
$.getJSON("http://yourdomain/yourpath/jsonp_api.php?method=getSchema¶ms=your_db_name&jsoncallback=?",
function(data){
//DO SOMETHING WITH THE DATA HERE
}
);
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/** * JSON API FOR MYSQL */ //////////////////////////////////////////////////////////////////////////////////////////////////// // CONFIGURATION SETTING VARIABLES FOR DATABASE CONNECTION $HOST="localhost"; $USER="root"; $PASS=""; $DBNAME = 'your_db_name'; /////////////////////////////////////////////////////////////////////////////////////////////////// /** * CONNECT TO MYSQL */ if(!$CON) { } if(!$dataselect) { } /** * EXECUTE THE PASSED IN METHOD WITH OR WITHOUT PARAMETERS * (multiple parameters are specified by a ',' separated string) */ $function = new ReflectionFunction($method); //instantiate the function as an object $function->invokeArgs($opts); //invoke the function with an array of arguments (if given) }else{ //ELSE THE FUNCTION DOES NOT EXIST echo "error the function you called : ".$_GET['method']."(".$_GET['params'].")"." does not exist"; exit; } exit; //////////////////////////////////////////////////////////////////////////////////////////////////////////// /** * FUNCTIONS WHICH RETURN JSONP RESULTS */ /** * fetchTable() * RETURNS AN ENTIRE MYSQL TABLE (JSONP) * @param mixed $dbtable * @return void */ function fetchTable($dbtable){ $sql="SELECT * FROM $dbtable"; $results[] = $row; } } /** * showTables() * * @param mixed $dbname * @return void */ function showTables($dbname){ $sql = "SHOW FULL TABLES IN hskitts_$dbname"; $results[] = $row; } } /** * showColumns() * * @param mixed $dbtable * @return void */ function showColumns($dbtable){ $sql = "SHOW FULL COLUMNS IN $dbtable"; $results[] = $row; } } /** * _show_cols() * internal used by getSchema * SCRIPT UTILITY FUNCTION * @param mixed $dbtable * @return */ function _show_cols($dbtable){ $sql = "SHOW FULL COLUMNS IN $dbtable"; $results[] = $row; } return $results; } /** * getSchema() * GET THE FULL SCHEMA MAP TABLES AND COLUMNS * OF A MYSQL DATABASE. RETURN AS JSONP * @param mixed $dbname * @return void */ function getSchema($dbname){ $sql = "SHOW TABLES IN $DBNAME"; $results[] = $row; } foreach($results[$i] as $key => $value){ $schema[$value] = _show_cols($value); } } }