Jump to content
Larry Ullman's Book Forums

Recommended Posts

I'm using PHP Data Objects on my project, and I ran into some difficulty with the WHERE clause.

Here's an example of my code:

$state = $_GET['state'];
$sql = "SELECT id, address, city, state, zip, phone, website, FROM Table01 WHERE 'state'=:state ORDER BY city ASC";
$host = "localhost";
$user = "me";
$pass = "mypassword";
$databasename = "my_db";
try {
$dbh = new PDO("mysql:host=$host;dbname=$databasename", $user, $pass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $dbh->query($sql);
$stmt->bindParam("state", $_GET['state']);
$cities = $stmt->fetchAll(PDO::FETCH_OBJ);
$dbh = null;
echo '{"items":'. json_encode($cities) .'}';
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}

When I run it, I get nothing in the Firefox console. But when the "WHERE" clause is taken out, it outputs everything from the database. What am I doing wrong?

Link to comment
Share on other sites

Thanks for the tip, Larry. Unfortunately, it still didn't output.

 

I think I will approach it from a different angle in my js. If I run into more problems, I will ask for help.

 

BTW, any chance of a jQuery book with emphasis on Node.js? I really like your writing style and appreciate this forum.

Link to comment
Share on other sites

Well, it is now outputting the JSON(.php page) correctly, but I am getting this message on my HTML: "TypeError: (name of variable) is undefined"

 

This question is more about js than PHP, but since it's germane to the original post, I'd like to ask it here. What's the correct way to run a while...loop with PDO? The error message I'm getting in the console could be from my js or my PHP.

$sql = "SELECT Table1.state_id, Table1.address, Table1.city, Table1.state, Table1.zip FROM Table1 INNER JOIN States ON Table1.state_id = States.state_id WHERE Table1.state_id=:id GROUP BY Table1.city ORDER BY Table1.city";

$host = "localhost";
$user = "me";
$pass = "mypassword";
$databasename = "my_db";
try {
$dbh = new PDO("mysql:host=$host;dbname=$databasename", $user, $pass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $dbh->prepare($sql);
$stmt->bindParam(":id", $_GET[id]);
$stmt->execute();
$city = $stmt->fetchAll(PDO::FETCH_OBJ);

$dbh = null;
echo '{"items":'. json_encode($city) .'}';
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}

 

And here's part of my js:

$.getJSON(URL + 'mysite.php?id='+id, displayCity);
});
function displayCity(data) {
var city = data.item;
console.log(city);

$('#actionList').append('<li><a href="mydetails.html?id=' + city.state_id + '">' +
'<div class="city" style="color:rgb(102,102,102); font-size:1.0em;">' + city.city + '</div>' +
'<div class="church" style="font-size:0.75em;">' + city.church + '</div>' +
'<div class="church" style="font-size:0.75em;">' + city.address + '</div>' +
'<div class="church" style="font-size:0.75em;">' + city.city + '</div>' +
'<div class="city" style="font-size:0.75em;">' + city.state + '</div>' +
'<div class="zip" style="font-size:0.75em;">' + city.zip + '</div>' +
'</a></li>');
$('#actionList').listview('refresh');
}

Link to comment
Share on other sites

 Share

×
×
  • Create New...