Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9105857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:12:07+00:00 2026-06-17T02:12:07+00:00

I am brand new to PHP and mysql and am trying something here. In

  • 0

I am brand new to PHP and mysql and am trying something here. In my HTML, I have one form where I enter a mileage and it’s passed on to the PHP code and converted to an integer. Then I want to take that and check against the database to isolate the record I want so I can do some computations. In other words, if the mileage entered (I save it in a veriable called $term) is less than or equal to a mileage in the table, I want to use the record that matches this criteria. For example, I enter 45 miles and the table has records at 40 miles and 50 miles, I want to pick the 50 mile level. If the user puts in 51, I want to use the 50 level. Things like that.

It connects to the database nicely but I’m not sure how to structure this at the foreach statement.
I have the following:

$term = (int)$_POST['term'];

try {
    $dbh = new PDO("mysql:host=$hostname;dbname=ratetable", $username, $password);

    /*** The SQL SELECT statement ***/
    $sql = "SELECT * FROM rates WHERE mileage<=term";
    foreach ($dbh->query($sql) as $row)
        {
        print $row['mileage'] .' - '. $row['ratepermile'] . '<br />';
        }

    /*** close the database connection ***/
    $dbh = null;
}

When I do this, I get an invalid argument supplied foreach().

Then I started looking around and saw statements that look like this:

$stmt = $db->prepare( 'SELECT * FROM rates WHERE mileage<=:term' );

where the search parameter is passed and so on.

So now I’m confused about what the correct way is to select a single record in an ascending list of mileages in a database (the last matching record versus all that match) that satisfies a criteria or how to correctly structure the WHERE argument. Any suggestions?

Thank you for looking.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T02:12:09+00:00Added an answer on June 17, 2026 at 2:12 am

    What you would need to do is use a WHERE clause in combination with ORDER BY and LIMIT clauses. For example to get the highest value that is less than or equal to a given search criteria you would do:

    SELECT * FROM rates
    WHERE mileage <= ?
    ORDER BY mileage DESC
    LIMIT 1
    

    To get the lowest value that is greater than or equal to a given search value do:

    SELECT * FROM rates
    WHERE mileage >= ?
    ORDER BY mileage ASC
    LIMIT 1
    

    As far as doing the actual PDO you would do something like this:

    $stmt = $dbh->prepare('SELECT * FROM rates WHERE mileage <= ? ORDER BY mileage DESC LIMIT 1');
    $stmt->execute(array($term));
    $row_object = $stmt->fetchObject();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm brand new to PHP/MYSql and I'm trying to build a form that submits
I'm brand new to ruby and Watir, here's my issue... I have a MySQL
Possible Duplicate: How to parse and process HTML with PHP? I am brand new
Brand new to the Log4Net library and there is something I have not been
im brand new to php (started yesterday) ive got a decent background of html/css
I am brand new to joomla, and am trying to have a few sections,
I'm brand new to php and I'm trying to work with some includes in
I'm brand new to php so struggling to figure this one out from existing
I have a brand new e-commerce website ( http://missfrisette.com/showphotos.php ) each item (hair clip)
Brand new to all of this and trying to install my PHP script. When

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.