Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. Linux, Apache, MySQL, PHP
  4. problem with a query and not sure why...

problem with a query and not sure why...

Scheduled Pinned Locked Moved Linux, Apache, MySQL, PHP
databasehelpphpmysqlsysadmin
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MacRaider4
    wrote on last edited by
    #1

    This is the code I'm having a problem with:

    //$sql = "SELECT SQL_CALC_FOUND_ROWS * FROM " . TBL_MEMBERS . " ORDER BY $order LIMIT :startRow, :numRows";
    $sql = "SELECT SQL_CALC_FOUND_ROWS * FROM " . TBL_MEMBERS . " ORDER BY $order LIMIT $startRow, $numRows";

    try {
    $st = $conn->prepare( $sql );
    $st->bindValue( ":startRow", $startRow, PDO::PARAM_INT );
    $st->bindValue( ":numRows", $numRows, PDO::PARAM_INT );
    echo "sql string = " . $sql . "
    ";
    echo "just before we hit the execute
    ";
    $st->execute();
    echo "just hit the execute
    ";
    $members = array();
    foreach ( $st->fetchAll() as $row ) {
    echo "pre";print_r($row);echo "/pre";
    $members[] = new Member( $row );
    echo "pre";print_r($members);echo "/pre";
    }

    ok had to remove the <> from the pre tags to makes it easier to read... Ok, if I run the non-commented out version of the SQL it works fine and I get the result I expect, however if I run the commented out version I get the following error: Query failed 1: 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 ''5'' at line 1 I printed this out at the start of the function to check the values: startRow = 0 and numRows = 5 I'm sure it's something stupid as I only spend about 5 - 10 hours a month doing PHP, so thank you in advance! oh yeah: mysql> select version(); +-------------------------+ | version() | +-------------------------+ | 5.5.24-0ubuntu0.12.04.1 | +-------------------------+ 1 row in set (0.00 sec)

    M 1 Reply Last reply
    0
    • M MacRaider4

      This is the code I'm having a problem with:

      //$sql = "SELECT SQL_CALC_FOUND_ROWS * FROM " . TBL_MEMBERS . " ORDER BY $order LIMIT :startRow, :numRows";
      $sql = "SELECT SQL_CALC_FOUND_ROWS * FROM " . TBL_MEMBERS . " ORDER BY $order LIMIT $startRow, $numRows";

      try {
      $st = $conn->prepare( $sql );
      $st->bindValue( ":startRow", $startRow, PDO::PARAM_INT );
      $st->bindValue( ":numRows", $numRows, PDO::PARAM_INT );
      echo "sql string = " . $sql . "
      ";
      echo "just before we hit the execute
      ";
      $st->execute();
      echo "just hit the execute
      ";
      $members = array();
      foreach ( $st->fetchAll() as $row ) {
      echo "pre";print_r($row);echo "/pre";
      $members[] = new Member( $row );
      echo "pre";print_r($members);echo "/pre";
      }

      ok had to remove the <> from the pre tags to makes it easier to read... Ok, if I run the non-commented out version of the SQL it works fine and I get the result I expect, however if I run the commented out version I get the following error: Query failed 1: 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 ''5'' at line 1 I printed this out at the start of the function to check the values: startRow = 0 and numRows = 5 I'm sure it's something stupid as I only spend about 5 - 10 hours a month doing PHP, so thank you in advance! oh yeah: mysql> select version(); +-------------------------+ | version() | +-------------------------+ | 5.5.24-0ubuntu0.12.04.1 | +-------------------------+ 1 row in set (0.00 sec)

      M Offline
      M Offline
      MacRaider4
      wrote on last edited by
      #2

      I figured it out... I checked the type of the variables and it thought the second was a string :doh: so I made some code changes and it's now a INT. Love those descriptive error messages, guess they have learned a few things from M$

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups