php - How to query and echo all data in column using Slim Framework -
i working on simple highscore system in slim framework (still beginner @ it), , stuck @ querying user datas. need query 2 columns in table (user, level) , sort level (desc).
user ----------------------- id | name | level | ----------------------- 1 | tom | 10 |
code in pure php
$result = mysql_query("select name, level user"); $storearray = array(); while ($row = mysql_fetch_array($result, mysql_assoc)) { $storearray[] = $row['name']; }
could more experienced provide me sample of code, can need ?
the php slim code user resource be:
<?php $app = new \slim\slim(); $app->get('/api/users', function () { $sql_query = "select id,name, level user order level desc"; try { $conn = new pdo('mysql:host=localhost;dbname=test', 'user', 'password'); $conn->setattribute(pdo::attr_errmode, pdo::errmode_exception); $rst = $conn->query($sql_query); $users= $rst->fetchall(pdo::fetch_obj); echo '{"users": ' . json_encode($users) . '}'; } catch(pdoexception $e) { echo '{"error":{"text":'. $e->getmessage() .'}}'; } }); $app->run();
i change mysql code pdo because it's better security against sql inyections.
if access yourdomain.com/api/users users lists.
Comments
Post a Comment