0

我创建了一个自定义函数,但出现此错误。我认为我的脚本中没有“解析错误”。

Parse error: syntax error, unexpected ',', expecting '&' or T_VARIABLE in C:\wamp\www\web template\test.php on line 8

<?php
require('global script/php/dbConnect.php');

$varName = 'sd';

echo checkDBDuplicate('account_list', 'memberID', $varName);

function checkDBDuplicate(tableName, dbFieldName, variableName) 
{
    $tableName = tableName;
    $dbFieldName = dbFieldName;
    $variableName = variableName;

    $searchDuplicate = mysql_query('
        SELECT $dbFieldName
        FROM $tableName
        WHERE $dbFieldName = $variableName
        ') or die(mysql_error());
    $countRow = mysql_num_rows($searchDuplicate);

    return $countRow;
}
?>

该脚本的目的是测试我是否可以在 SELECT 语句中使用变量。

4

4 回答 4

6

变量应该有$前缀。所以

function checkDBDuplicate(tableName, dbFieldName, variableName) {

错了,补上

function checkDBDuplicate($tableName, $dbFieldName, $variableName) {

显然,进一步的情况也是如此,所以:

$tableName = tableName;
$dbFieldName = dbFieldName;
$variableName = variableName;

需要同样的待遇

于 2012-04-05T12:38:09.057 回答
3
function checkDBDuplicate(tableName, dbFieldName, variableName) {

应该

function checkDBDuplicate($tableName, $dbFieldName, $variableName) {

在函数体之外:

$tableName = $tableName;
$dbFieldName = $dbFieldName;
$variableName = $variableName;
于 2012-04-05T12:39:05.273 回答
1

你的功能应该是这样的:

<?php
require('global script/php/dbConnect.php');

$varName = 'sd';

echo checkDBDuplicate('account_list', 'memberID', $varName);

function checkDBDuplicate($tableName, $dbFieldName, $variableName) {
     $tableName = $tableName;
     $dbFieldName = $dbFieldName;
     $variableName = $variableName;
     $searchDuplicate = mysql_query("
     SELECT $dbFieldName
     FROM $tableName
     WHERE $dbFieldName = $variableName
     ") or die(mysql_error());
     $countRow = mysql_num_rows($searchDuplicate);
     return $countRow;
}
?>
于 2012-04-05T12:40:06.733 回答
0

变量不需要再次赋值!!!

还有一些 ' 和 ` 缺少 SQL 查询。

<?php
require('global script/php/dbConnect.php');

$varName = 'sd';

echo checkDBDuplicate('account_list', 'memberID', $varName);

function checkDBDuplicate($tableName, $dbFieldName, $variableName) {

     $searchDuplicate = mysql_query("
                        SELECT `$dbFieldName`
                       FROM `$tableName`
                      WHERE `$dbFieldName` = '$variableName'
                      ") or die(mysql_error());
     $countRow = mysql_num_rows($searchDuplicate);
     return $countRow;
}
?>
于 2012-04-05T12:53:10.760 回答