Asked By: Anonymous
I am trying to follow this: http://www.9bitstudios.com/2013/05/restful-backbone-js-implementation-using-slim-api/ and this http://coenraets.org/blog/2011/12/backbone-js-wine-cellar-tutorial-part-1-getting-started/. I have this code so far – I have only just begun trying to understand the slim PHP framework:
<?php
require 'Slim/Slim.php';
SlimSlim::registerAutoloader();
$app = new SlimSlim();
// GET route with parameter
$app->get('/users', function () use ($app) {
// get all users
$host="localhost"; // Host name
$password=""; // Mysql password
$db_name="backbone"; // Database name
$tbl_name="Users"; // Table name
// Connect to server and select databse.
$mysqli = new mysqli("$host", "root", "$password", "$db_name")or die("cannot connect");
$sql = $mysqli->prepare("SELECT username FROM $tbl_name");
$sql->execute();
$sql->bind_result($namesie);
while ($sql->fetch()) {
echo json_encode($namesie);
}
$sql->close();
$mysqli->close();
});
// GET route with parameter
$app->get('/user/:username', function () use ($app) {
$request = (array) json_decode($app->request()->getBody()); // WHAT IS THIS LINE DOING?
var_dump($request);
$host="localhost"; // Host name
$password=""; // Mysql password
$db_name="backbone"; // Database name
$tbl_name="Users"; // Table name
// Connect to server and select databse.
$mysqli = new mysqli("$host", "root", "$password", "$db_name")or die("cannot connect");
$sql = $mysqli->prepare("SELECT * FROM $tbl_name WHERE username=:username");
var_dump($sql);
$sql->execute();
$sql->bind_result($dauser);
echo json_encode($dauser);
});
The first route works as it should, displaying all the users like this: "eamon""joe"
.
I did a var_dump
on the $request
variable in the second GET
route (/user/:username
) – it is an empty array…what is getting stored in this variable? Everything after var_dump($request)
is just my attempt at getting the correct user based on username. Also var_dump($sql)
prints out bool(false)
meaning that the user is not being correctly found in the database. I have a database table called Users
and I populated the database with two users: “joe” and “eamon”. When I type localhost/user/joe
into my browser, I get an error (along with the var_dump
‘s) saying:
Fatal error: Call to a member function execute() on a non-object
I think I am getting this error because nothing has been stored in $sql
. Can anyone point me in the right direction? I am eventually going to be using backbone.js – I have been modeling the code in my routes after this project: http://coenraets.org/blog/2011/12/backbone-js-wine-cellar-tutorial-part-1-getting-started/. He declares the route and then uses a function like getWine($id)
which is essentially what I am trying to implement – but for a user database instead. I am also using mysqli
where as he is using PDO
I think.
UPDATE
My code now looks like this:
$app->get('/user/:username', function ($username) use ($app) {
$host="localhost"; // Host name
$password=""; // Mysql password
$db_name="backbone"; // Database name
$tbl_name="Users"; // Table name
// Connect to server and select databse.
$mysqli = new mysqli("$host", "root", "$password", "$db_name")or die("cannot connect");
$sql = $mysqli->prepare("SELECT * FROM $tbl_name WHERE username=?");
$sql->bind_param("s",$username);
$sql->execute();
$sql->bind_result($profile);
if($sql->fetch()) {
echo json_encode($profile);
}
$sql->close();
$mysqli->close();
});
When I type in localhost/user/joe
(“joe” is a user that I inserted manually into the database) I get this error:
mysqli_stmt::bind_result(): Number of bind variables doesn't match number of fields in prepared statement
Isn’t there only one field? The username
field?
Solution
Answered By: Anonymous
This accomplishes what I set out to do with the /user/:username
route:
$app->get('/user/:username', function ($username) use ($app) {
$host="localhost"; // Host name
$password=""; // Mysql password
$db_name="backbone"; // Database name
$tbl_name="Users"; // Table name
// Connect to server and select databse.
$mysqli = new mysqli("$host", "root", "$password", "$db_name")or die("cannot connect");
$sql = $mysqli->prepare("SELECT username, email FROM $tbl_name WHERE username=?");
$sql->bind_param("s",$username);
$sql->execute();
$sql->bind_result($username1,$email);
if($sql->fetch()) {
echo $username1;
echo $email;
}
$sql->close();
$mysqli->close();
});
To get rid of the last error I just needed to insert the names of the columns that I needed instead of using *
.