Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Andrew Barber
3,865 PointsConnecting PHP Server
Hi Guys,
I was wondering if someone could help me out and tell me why I cannot display the query which is mentioned below:
http://localhost/TM.php?id=103
The error message I recieve when I try to run the code is as followed:
'The requested URL /TM.php was not found on this server.'
The following is my PHP file:
$host = "localhost"; //Your database host server
$db = "andrewtwo"; //Your database name
$user = "root"; //Your database user
$pass = "root"; //Your password
$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
}catch (Exception $e){
echo "Could not connect to the databse";
$connection = mysql_connect($host, $user, $pass);
//Check to see if we can connect to the server
if(!$connection)
{
echo "Could not connect to the databse";
}
else
{
//Attempt to select the database
$dbconnect = mysql_select_db($db, $connection);
//Check to see if we could select the database
if(!$dbconnect)
{
echo "Unable to connect to TM";
}
else
{
$query = ("SELECT *FROM clothing sizing WHERE clothing.id = "$_GET[id]"");
$resultset = mysql_query($query, $connection);
$records = array();
//Loop through all our records and add them to our array
while($r = mysql_fetch_assoc($resultset))
{
$records[] = $r;
}
return $records;
/*Output the data as JSON
echo json_encode($records); <<------- needed for ios app */
}
}
?>
9 Answers
Crisoforo Gaspar Hernández
18,302 PointsActually it seems like you are saving in a wrong directory the TM.php file.
Where is your localhost directory? Where are you saving the document?
Crisoforo Gaspar Hernández
18,302 PointsWell don't worry this is about learning. You should save the document as you were saying in the htdocs directory of the MAMP folder.
Have you already set up your database? Have you insert some test data in your database?
Andrew Barber
3,865 PointsYep, when I run a simple query in mySQL workbench. It works so I'm sure there is an issue with the code above.
Crisoforo Gaspar Hernández
18,302 PointsMaybe because the query is:
```SELECT * FROM clothing sizing WHERE clothing.id = "
and not
```SELECT *FROM clothing sizing WHERE clothing.id = "
You have the asterisk together with FROM as one word: *FROM
Andrew Barber
3,865 Pointsnope :l
do you know how to get it to display the output data e.g. in a table? i'm not sure if there is a particular way this is done.
Crisoforo Gaspar Hernández
18,302 PointsWell I check it out again but you are returning the output with:
return $records;
Well you could change
print_r($records);
//or
var_dump($records);
/*Output the data as JSON
echo json_encode($records);
//To see if there is any value inside the variable records.
Andrew Barber
3,865 Pointsstill no luck :l
Crisoforo Gaspar Hernández
18,302 PointsWhat is the url are you visiting?
Andrew Barber
3,865 Points http://localhost/TM.php?id=104
It must be the way how I am displaying the values of the query as the connection to the DB shows no error messages. I'm just not sure what part of it is wrong.
Andrew Barber
3,865 PointsHey John W, Could I ask you to take a look at this if you are not too busy please?
John W
21,558 Points// need space between * and FROM
// need comma between clothing and sizing table names
// You need to associate the type_id and brand_id on top of your existing condition for the joint
// need quotes for for id associative key
// need to correct syntax for embedding $_GET['id'] into string
$query = "SELECT * FROM clothing,sizing WHERE clothing.id = {$_GET['id']} AND clothing.type_id=sizing.type_id AND clothing.brand_id=sizing.brand_id";
$resultset = mysql_query($query, $connection);
// Check if the query is actually executed
if (!$resultset) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
$records = array();
//Loop through all our records and add them to our array
while($r = mysql_fetch_assoc($resultset)) {
$records[] = $r;
}
// removed return statement
/*Output the data as JSON*/
echo json_encode($records);
P.S. mysql_query has been deprecated. Consider using PDO instead.
Andrew Barber
3,865 Points Thanks @[John W](https://teamtreehouse.com/anotherjohn) .
After going back through my notes from the PHP tuts I made a few changes to include the PDO as you mentioned. I also chose a shorter SELECT statement as I wanted to test it out with just the id. Sadly, I could not get this working again. Have I really messed things up?
<?php
/*
Andrew's PHP Script for TM! sample URL http://localhost/TM.php?id=103
*/
$host = "localhost"; //Your database host server
$db = "andrewtwo"; //Your database name
$user = "root"; //Your database user
$pass = "root"; //Your password
$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
}catch (Exception $e){
echo "Could not connect to the databse";
$connection = mysql_connect($host, $user, $pass);
//Check to see if we can connect to the server
if(!$connection)
{
echo "Could not connect to the database";
}
else
{
//Attempt to select the database
$dbconnect = mysql_select_db($db, $connection);
//Check to see if we could select the database
if(!$dbconnect)
{
echo "Unable to connect to TM";
}
else
{
$query = "SELECT * FROM clothing WHERE clothing.id = {$_GET['id']}";
} catch (Exception $e) {
echo "Data could not be retrieved from the database.";
exit;
$records = array();
$resultset = $query->fetchAll(PDO::FETCH_ASSOC);
}
/*Output the data as JSON*/
echo json_encode($records); //<<------- needed for ios app
}
?>
John W
21,558 PointsAlmost... But you should review the exception handling syntax, ie how to use try...catch
Andrew Barber
3,865 PointsI made further adjustments and I feel like I might be getting somewhere but for some strange reason I cannot get it to display anything on my URL link. Currently scratching my head as I have been trying to fix this for at least 5hours :l
<?php
try{
$connection = new PDO("mysql:host=localhost;dbname=andrewtwo;port=8889","root","root");
$connection->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$connection->exec("SET NAMES 'utf8'");
} catch (Exception $e) {
echo "Could not connect to the database";
exit;
}
try{
$results = $db->query"SELECT * FROM clothing WHERE clothing.id = {$_GET['id']}";
} catch (Exception $e) {
echo "Data could not be retrieved from the database.";
//$result = $results->fetchAll(PDO::FETCH_ASSOC);
}
/*Output the data as JSON */
echo json_encode($result); //<<------- needed for ios app
John W
21,558 Pointsle sigh... you need to learn how to debug your own code! Clear syntax error here: you left out parentheses for your query call.
$results = $db->query( "SELECT ... ");
Andrew Barber
3,865 PointsApologies! but nothing still happens. I'm becoming slightly confused as I thought this would output the result in json and display it.
Should I be creating a json file or something as theres nothing that tells me that this script is working? :l
Andrew Barber
3,865 PointsAndrew Barber
3,865 Pointsok, this is gonna sound really stupid but I dont actually know where it is :l
as for saving, the php file is on my desktop.
EDIT: I moved into a mamp folder and now it shows an empty screen rather than the query content from my DB