I have created a custom function and I am getting this error. I dont think there is a "parse error" in my script.
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;
}
?>
The purpose of this script is to test if I can use a variable in a SELECT statement.
asked Apr 5, 2012 at 12:35
JohnSmith
1,6072 gold badges19 silver badges27 bronze badges
4 Answers 4
Variables should have a $ prefix. so
function checkDBDuplicate(tableName, dbFieldName, variableName) {
is wrong, make it
function checkDBDuplicate($tableName, $dbFieldName, $variableName) {
obviously, further on the same is true, so:
$tableName = tableName;
$dbFieldName = dbFieldName;
$variableName = variableName;
needs the same treatment
answered Apr 5, 2012 at 12:38
Nanne
64.4k16 gold badges123 silver badges167 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
JohnSmith
Ah thank you. It's working now. I thought I wasn't doing anything wrong because Javascript didn't need "var" in the function parameters.
Nanne
Check. Just a remark: 'var' doesn't mean "i'm making a variable here"! It is never actually needed to declare a variable, it means "this variable should be global". Therefore it's logical it shouldn't go in a function. You could compare it with the php keyword "global", which also never goes into a function declaration. The
$ means "a variable starts here" in php. There is no equivalent in Javascript, but certainly not the var keyword :)function checkDBDuplicate(tableName, dbFieldName, variableName) {
Should be
function checkDBDuplicate($tableName, $dbFieldName, $variableName) {
and iside the function body:
$tableName = $tableName;
$dbFieldName = $dbFieldName;
$variableName = $variableName;
answered Apr 5, 2012 at 12:39
Muhammad Zeeshan
8,86610 gold badges47 silver badges55 bronze badges
Comments
Your function should be like this:
<?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;
}
?>
answered Apr 5, 2012 at 12:40
web-nomad
6,0133 gold badges38 silver badges49 bronze badges
Comments
The variables do not need to be assigned again !!!
Also some ' and ` are missing the the SQL query.
<?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;
}
?>
Comments
default
mysql_real_escape_string