我一直收到No database selected
错误。我不确定是否有办法检查某些东西是否被封锁。
这是我的PHP代码:
<?php
if(isset($_POST['submit']))
{
$fname = $_FILES['sel_file']['name'];
$chk_ext = explode(".",$fname);
if(strtolower($chk_ext[1]) == "csv")
{
$filename = $_FILES['sel_file']['tmp_name'];
$handle = fopen($filename, "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
$sql = "INSERT into thingy(thingr,thing1,thing2) " .
"values('$data[0]','$data[1]','$data[2]')";
mysql_query($sql) or die(mysql_error());
}
fclose($handle);
echo "Successfully Imported";
}
else
{
echo "Invalid File";
}
}
?>
<form action='' method='post' enctype='multipart/form-data' >
Import File : <input type='file' name='sel_file' size='20'>
<input type='submit' name='submit' value='submit'>
</form>
答案 0 :(得分:1)
要检查的显而易见的事情是,您的数据库是否实际使用mysql_connect
连接。
答案 1 :(得分:1)
首先需要连接到mysql,然后在开始执行sql查询之前选择一个数据库
试试这个。
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Not connected : ' . mysql_error());
}
// make foo the current db
$db_selected = mysql_select_db('foo', $link);
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}
if(isset($_POST['submit']))
{
$fname = $_FILES['sel_file']['name'];
$chk_ext = explode(".",$fname);
if(strtolower($chk_ext[1]) == "csv")
{
$filename = $_FILES['sel_file']['tmp_name'];
$handle = fopen($filename, "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
$sql = "INSERT into thingy(thingr,thing1,thing2) values('$data[0]','$data[1]','$data[2]')";
mysql_query($sql) or die(mysql_error());
}
fclose($handle);
echo "Successfully Imported";
}
else
{
echo "Invalid File";
}
}
?>
<form action='' method='post' enctype='multipart/form-data' >
Import File : <input type='file' name='sel_file' size='20'>
<input type='submit' name='submit' value='submit'>
</form>
答案 2 :(得分:1)
试试这个,
<?php
$conn_str = mysql_connect('localhost', 'yout_user_id', 'your_user_password');
if (!$conn_str) {
die('Not connected to the database: ' . mysql_error());
}
$db_selected = mysql_select_db('your_database_name', $conn_str);
if (!$db_selected) {
die ("Can\'t use your_database_name : " . mysql_error());
}
if(isset($_POST['submit']))
{
$fname = $_FILES['sel_file']['name'];
$chk_ext = explode(".",$fname);
if(strtolower($chk_ext[1]) == "csv")
{
$filename = $_FILES['sel_file']['tmp_name'];
$handle = fopen($filename, "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
$sql = "INSERT into thingy(thingr,thing1,thing2) " .
"values(\"$data[0]\",\"$data[1]\",\"$data[2]\")";
mysql_query($sql) or die(mysql_error());
}
fclose($handle);
echo "Successfully Imported";
}
else
{
echo "Invalid File";
}
}
?>
<form action='' method='post' enctype='multipart/form-data' >
Import File : <input type='file' name='sel_file' size='20'>
<input type='submit' name='submit' value='submit'>
</form>
答案 3 :(得分:0)
您需要mysql_select_db("your_dbName", $link_identifier)
来选择数据库..
Find more details about mysql_select_db
at php.net
此外,开始使用 mysqli 函数或 PDO 语句..