0

I know that this is an insanely basic question, but I'm new to PHP and am trying to write some simple 'getter' functions that are stored in a separate file.

I keep getting this error, and I can't figure out why.

Fatal error: Function name must be a string in D:\Hosting\xxxxxxx\html\mysite\scripts\convert_ids.php on line 15

Here is the script from the calling function:

include 'scripts/convert_ids.php';        
$group = getGroup(2);
echo $group;

And this is the getter function:

convert_ids.php

<?php

    include('connection.php');

    function getGroup($id) {

        $sql = "SELECT group FROM Groups WHERE id=$id";
        $result = mysql_query($sql);

        if(!$result) {
            return -1;
        }

        $row = $mysql_fetch_array($result);    //<--this is line 15
        return $row['group'];
    }
?>
1
  • I'm guessing it has something to do with $mysql_fetch_array. It should be mysql_fetch_array. Please refrain from using these functions as they are deprecated. You can use mysqli/PDO. Jul 16, 2013 at 2:36

2 Answers 2

0
$row = $mysql_fetch_array($result);

Should be

$row = mysql_fetch_array($result);
1
  • Haha no problem, I've asked dumber questions Jul 16, 2013 at 2:38
0

You want to use mysql_fetch_assoc for $row['group']

mysql_fetch_array you will need to use $row[0]

Also don't put a $ in front of mysql_fetch_array

1
  • 2
    Actually, mysql_fetch_array will return both numeric and associative arrays by default. Jul 16, 2013 at 2:39

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.