2 回答

TA貢獻(xiàn)1982條經(jīng)驗(yàn) 獲得超2個(gè)贊
請(qǐng)參閱添加的評(píng)論以解釋我所做的修改
<?php
// this needs a password, I assume yours in blank
$conn = mysqli_connect('localhost','root', '');
if (!$conn) {
die(mysqli_error());
}
// this creates a database
$status = mysqli_query($conn,"CREATE DATABASE IF NOT EXISTS monthly");
if ($status){
echo "Database created";
} else {
echo "Database not created: " . mysqli_error($conn);
}
mysqli_select_db($conn, "monthly");
$ct = mysqli_query($conn,"CREATE TABLE IF NOT EXISTS `month1`(
`week1` INT(4) NOT NULL,
`week2` INT(4) NOT NULL,
`week3` INT(4) NOT NULL,
`week4` INT(4) NOT NULL
)");
if ($ct){
echo "Table created";
} else {
echo "table not created: " . mysqli_error($conn);
}
$open = fopen('/xampp/htdocs/month1.txt','r');
while (!feof($open))
{
$getTextLine = fgets($open);
$explodeLine = explode(',',$getTextLine, 4);
if(count($explodeLine) !=4) {
continue;
}
$week1 = $explodeLine[0];
$week2 = $explodeLine[1];
$week3 = $explodeLine[2];
$week4 = $explodeLine[3];
list($week1,$week2,$week3,$week4) = $explodeLine;
// Column and databse names are wrapped in backticks
// Text data is wrapped in single quotes
// integer data CAN be wrapped in single quote, but does not have to be
$qry = "insert into `month1` (`week1`,`week2`,`week3`,`week4`)
values($week1,$week2,$week3,$week4)"
// This is placing a query in a string variable
// so this die() is nonsense and anyway it meeds a parameter in the mysqli_error()
// like this mysqli_error($conn)
// or die(mysqli_error());
// this execues the query above
mysqli_query($conn,$qry);
// if you made the above line into
// $res = mysqli_query($conn,$qry);
// you could check if it actually worked like this
/*
if ( !$res ) {
mysqli_error($conn);
}
*/
}
fclose($open);
mysqli_close($conn);
?>

TA貢獻(xiàn)1789條經(jīng)驗(yàn) 獲得超8個(gè)贊
我正在更改您代碼上部的幾行,請(qǐng)檢查并比較,有一些小錯(cuò)誤,例如
if (mysqli_query($conn,$db)) and if (mysqli_query($conn,$ct)){
the above lines have no meaning.
請(qǐng)?jiān)谠撐恢没蚰奈恢锰砑右韵麓a。
<?php
$conn = mysqli_connect('localhost','root','');
if (!$conn) {
die(mysqli_error());
}
$db = mysqli_query($conn,"CREATE DATABASE IF NOT EXISTS monthly");
if ($db){
echo "Database created";
} else {
echo "Database not created: " . mysqli_error($conn);
}
mysqli_select_db($conn, "monthly");
$ct = mysqli_query($conn,"CREATE TABLE IF NOT EXISTS `month1`(
`week1` INT(4) NOT NULL,
`week2` INT(4) NOT NULL,
`week3` INT(4) NOT NULL,
`week4` INT(4) NOT NULL
)");
if ($ct){
echo "Table created";
} else {
echo "table not created: " . mysqli_error($conn);
}
- 2 回答
- 0 關(guān)注
- 174 瀏覽
添加回答
舉報(bào)