3

I am using PHP classes to connect to a data base. I am unable to solve a problem -- please help me out regarding this.

I have a function:

function getCampus($cm_id) //returns campus name
{
    $this->query = "select cm_name from campus where cm_id = ".$cm_id.";";
    $rd = $this->executeQuery();
    @$data = $rd->fetch_assoc();
}

and when I remove @ from @$data, it doesn't work. Please help me out: explain what it is what an alternative way would be. Thanks.

jscs
  • 63,694
  • 13
  • 151
  • 195
user667340
  • 531
  • 2
  • 10
  • 21
  • 12
    Define 'it doesnt work' – ySgPjx Mar 22 '11 at 13:05
  • Can you give us the error you are getting? PS: I don't know SQL, but you may have to make some of the query string parts uppercase. – Tanner Ottinger Mar 22 '11 at 13:06
  • 1
    @Tanner I believe the keywords uppercase is a convention and is not strictly enforced. – alex Mar 22 '11 at 13:07
  • possible duplicate of [What does @ mean in PHP?](http://stackoverflow.com/questions/3621215/what-does-mean-in-php) – Shakti Singh Mar 22 '11 at 13:07
  • you should know that `@` only suppresses **warnings** and not fatal errors. – RobertPitt Mar 22 '11 at 13:08
  • @Shakti I semi agree with you, because the question is *Why won't a line of code work with error suppressor removed?* – alex Mar 22 '11 at 13:09
  • Show the execute_query method please. I suspect that your issue is there. – Belinda Mar 22 '11 at 13:12
  • Your API looks like crap to me. Instead of this manually composed stuff (possibly junk, as nothing is preventing that), you should be trying to use placeholders (pg_query_params in Postgres, emulated in Mysql) and you should try to have just one method call with arguments, instead of this assignment followed by a method call. – bart Mar 22 '11 at 13:13

4 Answers4

6

@ is the error suppressor operator. Using it to prefix a line of code will suppress all non fatal errors. It is a bad idea to use it nearly every time.

If you get no output with it removed, try adding error_reporting(E_ALL) in the top of your file or in a bootstrap type file and ensure display_errors = On in php.ini (you can also use ini_set('display_errors', 'on')).

alex
  • 479,566
  • 201
  • 878
  • 984
  • 1
    @thephpdeveloper That edit changed a fair bit of what I said. Now it doesn't say that using `@` is a bad idea, and no mention of `display_errors = on` in `php.ini` which is sometimes an issue. – alex Mar 22 '11 at 13:10
2

The @ symbol in front of commands is used to ignore any errors that happen during the execution.

That line of code still fails when you put a @ in front of it, but you don't see it. Try to figure out what the problem with $rd->fetch_assoc() is. Also, the query looks rather wrong.

BastiBen
  • 19,679
  • 11
  • 56
  • 86
2

@ is used to suppress errors and warnings.

the @ is not your problem

felixsigl
  • 2,900
  • 1
  • 23
  • 16
2

The @ when used in a PHP expression suppresses errors for that expression. So, chances are "it's not working" because $rd->fetch_assoc() is throwing an exception.

Brian Driscoll
  • 19,373
  • 3
  • 46
  • 65