<?php
$dbc=mysqli_connect('localhost','root','password','beauty') OR die('Could not connected to MySQL: '.mysqli_error());
print_r($dbc);
echo "\n";
if($handle = opendir('D:/all-pics/tmp')){
echo "Directory handle:$handle\n";
echo "Files:\n";
while (false !== ($file = readdir($handle))) {
echo "$file\n";
$sql="insert into stu (stu, picture) values ('{$file}','{$file}')";
echo "$sql\n";
if($file!='.'&&$file!='..'){
$res=mysqli_query($dbc,$sql);
}
var_dump($res);
echo "\n";
}
$dbc=mysqli_connect('localhost','root','password','beauty') OR die('Could not connected to MySQL: '.mysqli_error());
print_r($dbc);
echo "\n";
if($handle = opendir('D:/all-pics/tmp')){
echo "Directory handle:$handle\n";
echo "Files:\n";
while (false !== ($file = readdir($handle))) {
echo "$file\n";
$sql="insert into stu (stu, picture) values ('{$file}','{$file}')";
echo "$sql\n";
if($file!='.'&&$file!='..'){
$res=mysqli_query($dbc,$sql);
}
var_dump($res);
echo "\n";
}
}
这是一段遍历文件夹并取出所有文件名,然后插入数据库的代码。现在我的数据库是beauty,里面有一张表stu;
起初mysqli_connect没有设置database,导致mysqli_query一直false ,因此需要注意,必须选择数据库不可