Citat:
Originalno napisao Nemanja Avramović
U svakom programskom jeziku postoji 10 načina da odradiš istu stvar. Umesto MySQL funkcija koristi MySQLi na primer: http://php.net/mysqli
|
Sto bi bilo isti to samo sa MySqli ??? Kod mene radi ovako:
Kôd:
$mysqli =mysqli_connect('localhost', 'baza', 'pass', 'korisnik');
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: ".mysqli_connect_error();
}
$query = mysqli_query($mysqli, 'SELECT * FROM stat');
if (!$query) {
die("Error running $sql: " . mysql_error());
}
$table = array();
$table['cols'] = array(
array('label' => 'ID', 'type' => 'number'),
array('label' => 'Name', 'type' => 'string'),
array('label' => 'Gender', 'type' => 'string'),
array('label' => 'Age', 'type' => 'number'),
array('label' => 'Donuts eaten', 'type' => 'number')
);
$rows = array();
while($r = mysqli_fetch_assoc($query)) {
$temp = array();
// each column needs to have data inserted via the $temp array
$temp[] = array('v' => (int) $r['ID']);
$temp[] = array('v' => $r['Name']);
$temp[] = array('v' => $r['Gender']);
$temp[] = array('v' => (int) $r['Age']);
$temp[] = array('v' => (int) $r['Donuts eaten']); // typecast all numbers to the appropriate type (int or float) as needed - otherwise they are input as strings
// insert the temp array into $rows
$rows[] = array('c' => $temp);
}
// populate the table with rows of data
$table['rows'] = $rows;
// encode the table as JSON
$jsonTable = json_encode($table);
echo $jsonTable;