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 trialMayur Pande
Courses Plus Student 11,711 PointsQuery not returning results
My query I run below returns $booked as null.
public function get_student_driver_booked($driveremail,$studentemail,$starttime){
$driveremail = mysqli_real_escape_string($this->link, $driveremail);
$studentemail = mysqli_real_escape_string($this->link, $studentemail);
$starttime = mysqli_real_escape_string($this->link, $starttime);
$result = mysqli_query($this->link,"select (driveremail),(studentemail),(starttime) from studentdriverdetails where starttime='$starttime' and studentemail='$studentemail'");
while($row = mysqli_fetch_row($result)){
$booked = $row;
}
var_dump($booked);
exit;
}
However when I run the similar query it returns the count for each select item.
public function get_student_driver_booked($driveremail,$studentemail,$starttime){
$driveremail = mysqli_real_escape_string($this->link, $driveremail);
$studentemail = mysqli_real_escape_string($this->link, $studentemail);
$starttime = mysqli_real_escape_string($this->link, $starttime);
$result = mysqli_query($this->link,"select count(driveremail),count(studentemail),count(starttime) from studentdriverdetails where starttime='$starttime' and studentemail='$studentemail'");
while($row = mysqli_fetch_row($result)){
$booked = $row;
}
var_dump($booked);
exit;
}
Is there a reason for this?
2 Answers
Simon Coates
28,694 Pointsyou probably don't want the brackets for fields. If you want to select names, you'd run SELECT names FROM table. THe use of brackets is more for SQL functions (count, max etc.). If you have PHPmyadmin, you can run queries in that before you try and use them from PHP. Apart from this, you might want to take a look at prepared statements, which are the recommended way to bind values into SQL statements.
Simon Coates
28,694 Points"Warning: mysqli_error() expects exactly 1 parameter, 0 given..." means that somewhere you've called mysqli_error(), but haven't given it a connection parameter - which it requires. It's discussed here.
Mayur Pande
Courses Plus Student 11,711 PointsMayur Pande
Courses Plus Student 11,711 PointsThank you for information about brackets, I simply copied and pasted from the where I displayed the count of the attributes (as shown above in question) that is why I had the brackets there.
I have tested in mysql-workbench previously as well.
I have now changed my query to this which also worked in mysql-workbench;
However I get the error;
Warning: mysqli_error() expects exactly 1 parameter, 0 given in /var/www/html/southlondontutors.com/src/provider/TutorServiceProvider.php
But not sure why this is the case as mentioned it works fine on mysql-workbench
Here is my controller code
here is my function too add_student_driver