为什么我会得到这个?使用未定义的常量连接 - 假定“连接”
我正在 MAMP 环境中的 Mac OSX 10.5 上进行开发。我的 PHPadmin 显示数据库和表存在得很好,但是当我尝试执行连接时,我得到以下信息:
注意:使用未定义的常量连接 - 假定“连接”
以下是我的代码:
In header before任何 HTML 或空格
// 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());
}
在数据库连接页面的 HTML 标记正文中
// 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 />";
}
结束 HTML 标记之后
// 5. Close connection
mysql_close($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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你说的是
if (!connection)
而不是if(!$connection)
you said
if (!connection)
instead ofif(!$connection)
只是扩展之前的答案。基本上,旧版本的 PHP 允许您使用不带空格的字符串而不用引号引起来。这里有一个解释:
https ://www.php.net/manual/en/language.types.array.php#language.types.array.foo-bar
干杯!
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!