我正在使用函数require_once,但在尝试运行页面时不断收到错误。
实际上,这段代码已经公开运行了。但是,当我将所有代码复制到本地计算机时。此代码不起作用。我使用的是XAMPP 1.7.2 PHP 5.3.0。
这是我的代码:"db\connect.php“
<?
/* Connecting, selecting database */
$link = mysql_connect("localhost", "root", "") or die("Could not connect : " . mysql_error());
mysql_select_db("amr") or die("Could not select database" . mysql_error());
?>
当我从下面的代码中调用函数"connect.php“时:
<?php
require_once('db/connect.php');
$sql="SELECT * from user";
$result=mysql_query($sql);
if ( $result == false ) { die(mysql_error()); }
$row = mysql_fetch_array($result, MYSQL_ASSOC);
?>
我得到了这个错误:
No database selected
请解释一下我的问题是什么。
发布于 2014-10-30 10:09:18
在select_db函数中添加链接标识符。
$link = mysql_connect("localhost", "root", "") or die("Could not connect : " . mysql_error());
mysql_select_db("amr",$link ) or die("Could not select database" . mysql_error());
对您的查询执行相同的操作:
$result=mysql_query($sql,$link);
这可能是因为在一个页面上打开了多个连接。
或者更好的方法是使用PDO。Mysql_已弃用。
发布于 2014-10-30 10:47:07
看起来你的问题出在这一行:
mysql_select_db("amr")
试试这个:
$link = mysql_connect("localhost", "root", "") or die("Could not connect : " . mysql_error());
mysql_select_db("amr", $link) or die("Could not select database" . mysql_error());
https://stackoverflow.com/questions/26643794
复制相似问题