mysqli_query在文件中不能多次工作

时间:2014-06-17 13:41:17

标签: php mysqli

<!DOCTYPE HTML>

<html>
<head>
<?php include 'connecting.php'; ?>
</head>
<body>



<h1> Welcome to location batch uploading </h1>
<p> Please select a university </p>

<?php

$sql=mysqli_query($con, "SELECT college_ID,college_Name FROM college ORDER BY college_Name"); 
if(mysqli_num_rows($sql)) { 
    $select= '<form method="POST" action="locationBatchUpload.php" enctype="multipart/form-data"><select name="select"><option value = "0"> Please Select University </option>';   
    while($rs=mysqli_fetch_array($sql)){ 
        $select.='<option value="'.$rs['college_ID'].'">'.$rs['college_Name'].'</option>'; 
    } 
} 
$select.='</select>'; 
echo $select;  

?>


<p>Please upload the CSV file that contains location information from one college/university.</p>

    <p><input type ="file" name = "CSVfile"></p>
    <input type = "submit" name = "Submit" value = "Upload!">
</form>


<?php


$row = 0;
$entries = array();
if(isset($_POST['Submit']))
{
    $file = $_FILES["CSVfile"]["tmp_name"];
    $choice = $_POST["select"];
    if ($file && $choice && ($handle = fopen($file, 'r')) !== FALSE) {
        $q = "SELECT location_Name FROM location WHERE college_ID = '".$choice."'";
                 $sql=mysqli_query($con, $q); 

if(mysqli_num_rows($sql)){
echo "here";
}


                  while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {

            $query = "INSERT INTO location (location_ID,location_Name, college_ID) VALUES ('','".$data[0]."','".$choice."')";
echo $query. "<br/>";
            mysqli_query($con,$query);
            $row++;
        }
        fclose($handle);
        echo "<br/> You have entered ". $row." rows in the database"; 
    }
    else if (!$file){
        echo "<br/>No file selected! Please select a file and try again!";
    }
    else if (!$choice){
        echo "<br/>Please select a university! ";
    }
}

?>

</body>
</html>
嘿大家, 我正在尝试从CSV文件中将信息输入我的数据库。当我使用mysqli_query创建我的下拉框时,它工作正常。但是,当我第二次使用它并尝试获取行时,使用mysqli_num_rows,它会给我500个内部服务器错误。我在godaddy.com上托管这个网站,找不到该网站的错误日志。非常感谢有关此问题的任何信息。 $ con是我与数据库的连接,该连接是使用

完成的
new mysqli(host,userName, password,dbName)

1 个答案:

答案 0 :(得分:0)

好吧,如果你使用mysqli对象样式连接启动该过程,那么坚持使用它,不要切换回过程风格

所以假设你这样连接

$con= new mysqli(host,userName, password,dbName);

使用

$result = $con->query("SELECT college_ID,college_Name 
                       FROM college ORDER BY college_Name");

if( $result->num_rows() ){ 
    $select= '<form method="POST" action="locationBatchUpload.php" enctype="multipart/form-data">
              <select name="select">
                  <option value = "0"> Please Select University </option>';   
    while ( $rs = $result->fetch_array(MYSQLI_ASSOC) ) { 
        $select.='<option value="'.$rs['college_ID'].'">'.$rs['college_Name'].'</option>'; 
    } 
}