John
John

Reputation: 13

Why am I getting this? Use of undefined constant connection - assumed 'connection'

I am developing on a Mac OSX 10.5 in the MAMP environment. My PHPadmin shows that the database and table exist just fine, however when I try to execute a connection I get this:

NOTICE: Use of undefined constant connection - assumed 'connection'

Here is what I have for code:

In header before any HTML or white-space

//  1. Create a database connection
$connection = mysql_connect('localhost', 'test', '1234');
if (!connection) {
    die("Database Connection1 Failed: " . mysql_error());
}
//  2. Select a database to use
$db_select = mysql_select_db('widget_corp', $connection);
if (!$db_select) {
    die("Database Connection2 Failed: " . mysql_error());
}

In the body of HTML markup of the database connection page

//  3. Preform database Query
$result = mysql_query('SELECT * FROM subjects'. $connection);
if (!$result) {
    die("Database Connection3 Failed: " . mysql_error());
}
//  4. Use returned data
while ($row = mysql_fetch_array($result)) {
    echo $row["menu_name"]." ".$row["position"]."<br />";
}

After the closing HTML tag

//  5. Close connection
mysql_close($connection);

Upvotes: 1

Views: 8258

Answers (2)

mrpoundsign
mrpoundsign

Reputation: 36

Just expanding on the previous answer. Basically, older versions of PHP allows you to use strings with no spaces without quoting them. There's an explanation here:

https://www.php.net/manual/en/language.types.array.php#language.types.array.foo-bar

Cheers!

Upvotes: 0

Zak
Zak

Reputation: 25205

you said if (!connection) instead of if(!$connection)

Upvotes: 9

Related Questions