我一直在玩我的PHP编码一段时间了。我试图上传芝加哥熊队名单,以便我以后可以在网页上显示,但我无法让我的代码真正上传excel表到MySQL。
我有运行MySQL和APache的XAMPP。这是我的代码:
$db_roster = mysqli_connect("localhost", "root", "");
if (!$db_roster)
die("Unable to connect: " . mysqli_connect_error()); // die is similar to exit
//creating the database
if (isset($_GET["button"])){
//If create database is clicked
if( $_GET["button"] == "create"){
if (mysqli_query($db_roster, "CREATE DATABASE finalRoster;")){
echo "The database is created <br>"; //Database is created
// else
// echo "unable to create database: " . mysqli_error($db_roster) . "<br>";
mysqli_select_db($db_roster, "finalRoster");
$cmd = "CREATE TABLE sportDatabase ( number int(2) NOT NULL PRIMARY KEY,
playerName char(50),
position varchar(2),
height char(3),
weight int(3),
age int(2),
experience int(2),
collegeName char(60)
);"; //Creates the table
echo "The Database is Ready<br>";
}
}
mysqli_query($db_roster, $cmd);
$cmd = "LOAD DATA LOCAL INFILE 'bearsRoster.csv' INTO TABLE clist FIELDS TERMINATED BY ',';";
mysqli_query($db_roster, $cmd);
echo "<h1>Sports contents</h1>";
$cmd = "SELECT * FROM sportDatabase";
$records = mysqli_query($db_roster, $cmd);
while($row = mysqli_fetch_array($records))
echo( "\t\t" . $row['number'] . $row['playerName'] . $row['position'] . $row['height'] .
$row['weight']. $row['age'] . $row['experience'] . $row['collegeName'] . PHP_EOL );
mysqli_close($db_roster);