Wednesday, February 9, 2011

Query Parse error in shopping kart application

In my shopping cart app, I execute a query using:

44: @mysql_query("insert into cart(cookieId, itemId, qty) values('" . GetCartId() . "', $itemId, $qty)");

But when I view the webpage, I get the following error: Parse error: parse error, unexpected T_STRING, expecting '{' in H:\Program Files\EasyPHP 2.0b1\www\beta\cart.php on line 44

  • Can you include a bit more context, e.g. lines 40-44? The error may well be earlier on in the code (line 44 looks fine to me). Also, please use pre-formatting if possible.

    From Bobby Jack
  • You (probably) have a mismatched ' or " somewhere before line 44.

    From pmg
  • Sure.

    I have a webpage that lists the products that can be "added to the cart", In it there is a table with this in one of the td:

    < a href="cart.php?action=add_item&id=&qty=1">Add Item

    In the next webpage (cart.php), I have a function to add the item:

    function add_item($itemId, $qty)

    $result = mysql_query("select count from cart where cookieId = '" . GetCartId() . "' and itemId = $itemId");

    $row = mysql_fetch_row($result); $numRows = $row[0];

    if($numRows == 0) { @mysql_query("insert into cart(cookieId, itemId, qty) values('" . GetCartId() . "', $itemId, $qty)"); }

  • You need a { before the start of the function body

    function add_item($itemId, $qty)
    
    { /* HERE */
    
    $result = mysql_query(/* ... */);
    
    From pmg
  • Thanks, I added in the brackets but now I get these errors: They look like database connectivity errors, but I was able to connect to MySQL in a previous page using the same credentials.

    Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in H:\Program Files\EasyPHP 2.0b1\www\beta\cart.php on line 33

    Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in H:\Program Files\EasyPHP 2.0b1\www\beta\cart.php on line 33

    Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in H:\Program Files\EasyPHP 2.0b1\www\beta\cart.php on line 35

  • Try this:

    $result = mysql_query(/* ... */)
        or die('SQL Error @ ' . __FILE__ . ':' . __LINE__ . ' [' . mysql_error() . ']');
    

    Do the same to the mysql_connect() line.

    From pmg
  • I have the same problem, and that didn't solve the problem, can anyone help?

  • You probably need pass or make global the mysql connection into your function as mysql_query() is unable to find an existing connection. I always do this to ensure I have the right connection. I'm not sure what differences there maybe with mysql_query() being able to find the connection from within a function.

  • I have my connection to MySQL in a function in a separate file. That file is require_once'd in every php file that needs to connect to the database. Sometimes I run queries directly from the page, other times I run them from yet another function. It always worked for me from PHP 4.something to PHP 5.2.5

    Configuration

    <?php
    // config.inc.php
    define('CONFIG_DBSERVER', 'myserver');
    define('CONFIG_DBUSER', 'username');
    define('CONFIG_DBPASS', 'password');
    define('CONFIG_DATA', 'database');
    ?>
    

    Connection

    <?php
    // dbfx.inc.php
    function db_connect($server, $user, $pass, $db) {
      $con = mysql_connect($server, $user, $pass);
      if ($con) {
        if (!mysql_select_db($db)) return false;
      }
      return $con;
    }
    /* ... */
    

    Use

    <?php
    require_once 'config.inc.php';
    require_once 'dbfx.inc.php';
    /* ... */
    $con = db_connect(CONFIG_DBSERVER, CONFIG_DBUSER, CONFIG_DBPASS, CONFIG_DATA);
    if (!$con) die('Error: ' . mysql_error());
    /* ... */
    
    From pmg

0 comments:

Post a Comment