querying a database

i want to display data in a table based on the user selection from a form (on a previous page).

my query isnt working:

$sth = $conn->prepare (‘SELECT * FROM ROUTE WHERE ORIGIN = $ORIG’);

the $ORIG is the value of the options inside the drop down list. (its the value of one option in the form on the previous page)

Is $ORIG a integer or a string value? You’re using ->prepare but are you then using execute()? Prepare only does exactly what it says, prepares the query, doesn’t actually retrieve the rows. If $ORIG is a string value then you MUST use bind the value with a placeholder instead of the var.
Example
[php]
$sth = $conn->prepare (‘SELECT * FROM ROUTE WHERE ORIGIN = ?’);
$sth->execute(array($ORIG));
[/php]
Lastly, your $ORIG is not evaluated when it’s inside ‘single’ quotes like you have in your prepare statement. This would work fine assuming that $ORIG is a integer value, but then there wouldn’t be any need for the prepare, you could just run a normal query. Also it’s best to only have the query keywords in uppercase, makes it much easier to tell what is doing what.
[php]
$sth = $conn->prepare (“SELECT * FROM route WHERE origin = $ORIG”);
[/php]

Hello,

Thankyou its working now. one more question:

$month = $_POST [‘datemonth’];
$day = $_POST [‘dateday’];
$year = $_POST [‘dateyear’];

$date = $year."/".$month."/".$day;

how can i return the actual day eg “monday” or “tuesday” based on the sql data. atm my table data says 1,2,3,4,5, for mon-fri. how do i return the day not number in php?

Thankyou

[php]$displayDay = date(‘l’, strtotime($date));[/php]

thankyou for your reply, i have done this and its already working. however, when i am displaying the table in my php page it still displays 1 for monday. its not converting the sql data (1,2,3,…) into the day of the week on the page ( monday etc)

i have attached my sql data table - please refer to column “dayOfWeek”
and also my php page table - please refer to column “days”

i want the sql database table to stay the same. BUT the php table to display monday for “1” etc


helpPlease.zip (26.4 KB)

Sponsor our Newsletter | Privacy Policy | Terms of Service