0

我试图让这个命中计数器与 pdo 一起工作,但它一直在抱怨 Parse 错误:

语法错误,意外的 '(',第 37 行中需要标识符 (T_STRING) 或变量 (T_VARIABLE) 或 '{' 或 '$'。

我不确定它是否是索引,因为我的数据库的名称是 group2,带有一个表计数器和一个名为 counter 的列。我也尝试使用 mysql 程序方法,但我对这两种方法都不够熟悉,而且我使用的每个指南仍然出现这些错误。我究竟做错了什么。

*编辑新代码:

<?php
try
{
$dsn = "mysql:host=xxxxxxxxxxxxxx"; // Missing semi colon (;)
$conn = new PDO($dsn,'group2', 'xxxxxxxxxxxx');  //connection
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOexception $e)
{
Print($e->getMessage());
Exit;
}


$sql = ("SELECT * FROM counter");
$conn->query($sql);
$row = $conn->query($sql);
$counter =$row['counter'];

if(empty($counter))
{

$counter = 1; 
$insert = $conn->query("INSERT INTO counter VALUES('$counter')");  
}

$add= $counter+1;
$insertNew = $conn->("UPDATE counter SET counter = '$add'"); 
echo $counter;

?>


<?php $conn = null; ?>
4

1 回答 1

1

There is a lot wrong with your code.

  1. There is no function mysqle_select_db(). There is one called mysql_select_db(), but since you're using PDO, you probably want to use $conn->query('use group2'), if at all.
  2. You are running a query with a variable that's not set yet. $conn->query($sql); just two lines further down won't work.
  3. Square brackets are not valid in table names, neither is + a valid query column. SELECT + FROM counter[counter] just won't work. You probably meant to do SELECT * FROM counter.
  4. Your query won't return anything, but I'm pretty sure it won't return a counter[counter] column. You probably meant to just do $counter=$row['counter']; (yes you can trick MySQL into using square brackets in column names, but that's most probably not whats intended here)
  5. Last but not least, your UPDATE query is not only issued twice (once using PDO, once though the mysql_* lib), but also wrong. It should most probably read $insertNew = $conn->("UPDATE counter SET counter = '$add'");, assuming that your counter table has a counter column.

Edit:

With a few minor details, your script looks okay.

  1. you probably shouldn't post your database password.
  2. One of your database queries is not doing much: $conn->query($sql); - you do that again on the next line.
  3. Your counter will start with 2 instead of 1, because you're updating after you inserted (the UPDATE is not in the else part of your if statement, so it will run in all cases)

If it still doesn't work, you should try and put echo/print_r/var_dump statements into your code to see where it is breaking - e.g.:

print_r($conn->errorInfo()); will give you information on errors inside PDO. echo "$counter"; will give you the result of your select.

print_r($conn->errorInfo());

于 2013-03-17T21:51:57.223 回答