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 9319563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T03:24:58+00:00 2026-06-19T03:24:58+00:00

I have the below PHP for my book keeping application. It uses PDO. if

  • 0

I have the below PHP for my book keeping application. It uses PDO.

if (isset($_POST['lesson'])AND isset($_POST['page']))
{
   try {
      $options_pdo[PDO::ATTR_ERRMODE]=PDO::ERRMODE_EXCEPTION ;
      $DB= new PDO('mysql:host=localhost;dbname=mydb','jamie','admin',$options_pdo);
      $statement=$DB->query("SELECT data FROM teach_books where lesson=".$_POST['lesson']."AND page=".$_POST['page'] );

      while($results = $statement->fetch()){
         $results['data'];
         echo "<br>";
      }

   } catch(Exception $e) {
    die ('ERROR: '.$e->getMessage());
    exit;
  }

}

However when I run the code it displays the below error:

ERROR: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'page=dsas' at line 1

Could anybody help please?

  • 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-19T03:24:59+00:00Added an answer on June 19, 2026 at 3:24 am

    A couple of things:

    1) DO NOT INSERT RAW QUERY STRINGS:

    This code is extremely suseptable to SQL Injection. PDO has a feature called ‘prepared statements’. This is waht you should be using for you SQL queries. Do not just inject some POST parameters into the query string as the result will be a security hole. The quotes you have accidentally inserted into the query may well have come from a malicious user trying a SQL attack.

    2) MISSING SPACE:

    You have a missing space right before the AND. The parser does not know what to make of the term 2AND and so produces the error. The SQL by iteslf expands to something like.

    SELECT data FROM teach_books where lesson=2AND page=24;
    

    3) MISSING QUOTE MARKS:

    If you were to use something like the above you will need to add some closing quote marks at the end of the query. You also need quotes around the string params that you give inside the select.

    4) ECHO DATA:

    You are not actually printing out anything in the loop. Simply having a statement sitting inside PHP will not print it out. You need echo command.

    echo $results['data'];
    

    5) ITERATE OVER OBJECT:

    You do not need to keep calling fetch(), you could use fetchAll() and then iterate over that result set.
    Really you should not call any “fetch” method unless you just need the rows in an array.
    The result set object is iterable and can be looped over.

    $statement->execute(); 
    foreach ($statement as $row) { 
        ... 
    }  
    

    6) TRY-CATCH:

    You could probably remove the ‘try-catch’ code because what you are doing inside there is what the exception would do anyway.

    Additionally I hope ‘admin’ is not your actual password.

    Sorry to have kept adding to my answer. Just wanted to post the 6 points by themselves and then expand on them.

    Hope that helps

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using face book API and i have this below code: include(../src/facebook.php); //
I have an Array like below in PHP array( (int) 0 => array( 'id'
I have se the below date format in the codeignitor config.php $config['log_date_format'] = 'd/m/Y';
Below is an old PHP function I have for a dropdown list for users
I have a basic PHP question, take the code below for example, let's say
I have a real basic form (code below) with a bunch of back-panel PhP.
Possible Duplicate: php/Mysql query with inserting date fails I have a datepicker code below:
I have a small script which im using to test PHP mail(), as below:
Tech used: PHP 5.3.10 Hi, I have an array (example below) I need to
Am using below third party API in my project development http://undesigned.org.za/2007/10/22/amazon-s3-php-class I have done

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.