SQL statement not working
i need to select the variable 'duration' from a database where the eventID equals $idnumber. Im using the query bellow but am not having any luck. Can anyone see any flaws.
$duration = mysql_query("SELECT `bs_res开发者_运维百科ervations`.`duration`FROM bs_reservations WHERE (`bs_reservations`.`eventID` '$idnumber')");
Small change :
$duration = mysql_query("SELECT `bs_reservations`.`duration`FROM bs_reservations WHERE (`bs_reservations`.`eventID` = " . $idnumber . ")");
UPDATE :
$data = mysql_fetch_array($duration );
Try to print this $data......
I assume you're missing an =
when comparing the 2 IDs at the end.
$duration = mysql_query("SELECT `bs_reservations`.`duration`FROM bs_reservations WHERE (`bs_reservations`.`eventID` = '$idnumber')");
Try this:
SELECT `bs_reservations`.`duration`
FROM `bs_reservations`
WHERE `bs_reservations`.`eventID` = '{$idnumber}'
I've added whitespace before the from
, added an =
before idnumber, added a backtick after the where
and for good measure also added the {
and }
though they are not really needed, but good practice.
While the obvious problem is the missing operator "=" (most adequately answered by @Dave IMO), perhaps you're having difficulty using the data once the MySql query has been properly executed. One would think you could then take your variable "$duration" and use it. Not true. $duration is now a resource, and you need to extract the information from it. If you know you're only going to get one piece of data back, consider the following code after your query:
list($duration)=mysql_fetch_array($duration);
This of course resets $duration to the value retrieved by the query and is no longer usable as a mysql resource, but it gets what you're looking for.
If this was not your problem, my answer would be the same as @Dave
Edit: Sorry, after reviewing the questions and answers again, my answer would be as follows:
$duration=mysql_query("select `duration` from `bs_reservations` where `eventID`='$idnumber'");
精彩评论