0

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
2
  • what is on line 8 of test.php Commented Apr 5, 2012 at 12:37
  • in your sql statement make sure to escape the strings using mysql_real_escape_string Commented Apr 5, 2012 at 12:43

4 Answers 4

6

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
Sign up to request clarification or add additional context in comments.

2 Comments

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.
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 :)
3
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

Comments

1

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

Comments

0

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;
}
?>
answered Apr 5, 2012 at 12:53

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.