views:

166

answers:

9

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

A: 

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.

Bobby Jack
A: 

You (probably) have a mismatched ' or " somewhere before line 44.

pmg
A: 
+1  A: 

You need a { before the start of the function body

function add_item($itemId, $qty)

{ /* HERE */

$result = mysql_query(/* ... */);
pmg
A: 

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

A: 

Try this:

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

Do the same to the mysql_connect() line.

pmg
A: 

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

A: 

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.

Darryl Hein
A: 

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());
/* ... */
pmg