PHP mysql类函数插入方法的解析错误


A Parse error with the PHP mysql Class function of insert method

大家好,当我尝试使用Mysql学习PHP OOP时,我遇到了这个问题。当我启动网页时,它显示的错误信息是:解析错误:语法错误,意外T_VARIABLE在/var/www/html/xxx/class_db .php第52行

希望任何人可以帮助和改善我的编码习惯,谢谢!

class_DB.php

 <?php
class ConnDB{
private $host;
private $user;
private $pass;
private $db;
private $link;
private $query;
private $dbConn = 0;
    public function __construct($host, $user, $pass, $db="", $conn = 1){
        $this->host = $host;
        $this->user = $user;
        $this->pass = $pass;
        $this->db_name = $db;
        if (!empty($db)){
            $this -> name = $db;
        }
        if ($conn == 1){
            $this->connect();
        }
    }
    public function __destruct(){
        mysql_close($this->link);
    }
    public function connect(){
        if($this -> link = mysql_connect($this->host, $this->user, $this->pass)){
            if(!empty($this->db_name)){
                if (!mysql_select_db($this->db_name)){
                    $this -> exception("Error: Could not connect to the database!");
                }
            }else{
                $this -> exception("Could not create database connection!");
            }
        }
    }
    public function query($sql){
        if($this->query = mysql_query($sql)){
            return $this->query;
        }else{
            $this->exception("Could not query the database!");
            return false;       
        }
    }
    public function insert_SQL($varNum ){
        $this->query("INSERT INTO testing_Rand (number1, number2, number3, number4, number5, number6, number7, number8, number9, number10) VALUES ("
        $newRand.=implode(',', $varNum)
        $newRand.=")");
    }
    public function close() {
        mysql_close($this->link);
    }
    public function fetch_array($queryID){
        if(empty($queryID)){
            $this->exception("Error: Could not fetch array");
            return false;
        }else{
            $rows = mysql_fetch_array($queryID);
        }
        return $rows;
        //return mysql_fetch_array($this->query, MYSQL_ASSOC);
    }
    public function fetch_array_assoc($queryID){
        if(empty($queryID)){
            $this->exception("Error: Could not fetch array assoc");
            return false;
        }else{
            $rows = mysql_fetch_array($queryID, MYSQL_ASSOC);
        }
        return $rows;
    }
    public function num_rows($queryID){
        if(empty($queryID)){         
            $this->exception("Error: Could not get number of rows");
            return false;
        }else{
            return mysql_num_rows($queryID);
        }
        //return mysql_num_rows($this->query);
    }
    public function get_insert_id(){
        return mysql_insert_id($this->_dbConn);
    } 
    //$conn=mysql_connect($host,$user,$pass,$db);   
}

?>

Rand.php

<?php session_start(); ?>
 <?php
$_SESSION['selectNum'] = $_POST;
$selectNum = $_POST["selectNum"];
?>
<html>
<body>
<?php
require_once("class_DB.php");
$conn = new connDB('localhost', 'chccheung', 'chccheung', 'chccheung');

if (!$conn){
    die('Not connected : ' . mysql_error());
}
$varNum = array();
$varNum = range(0, 9);
?> 
<form action="<?=htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
    <p><b><center>Choose an amount of random numbers in the selection box</center></b></p>
    <p>
        <center>
            <select name="selectNum" id="selectNum">
                <?php
                    /*foreach($varNum as $key => $value):
                    echo '<option value="'.$key.'">'.$value.'</option>';
                    endforeach;*/                       
                ?>
                <option <?php if (isset($_SESSION['selectNum']) && $selectNum =="0") echo "selected";?> value="0">1</option>
                <option <?php if (isset($_SESSION['selectNum']) && $selectNum =="1") echo "selected";?> value="1">2</option>
                <option <?php if (isset($_SESSION['selectNum']) && $selectNum =="2") echo "selected";?> value="2">3</option>
                <option <?php if (isset($_SESSION['selectNum']) && $selectNum =="3") echo "selected";?> value="3">4</option>
                <option <?php if (isset($_SESSION['selectNum']) && $selectNum =="4") echo "selected";?> value="4">5</option>
                <option <?php if (isset($_SESSION['selectNum']) && $selectNum =="5") echo "selected";?> value="5">6</option>
                <option <?php if (isset($_SESSION['selectNum']) && $selectNum =="6") echo "selected";?> value="6">7</option>
                <option <?php if (isset($_SESSION['selectNum']) && $selectNum =="7") echo "selected";?> value="7">8</option>
                <option <?php if (isset($_SESSION['selectNum']) && $selectNum =="8") echo "selected";?> value="8">9</option>
                <option <?php if (isset($_SESSION['selectNum']) && $selectNum =="9") echo "selected";?> value="9">10</option>
            </select>
        </center>
    </p>
    <center><input type="hidden" name="selected_Num" id="selected_Num" value="" /></center>
    <center><input type="Submit" value="submit"></center>
</form> 
<?php   
//Check the amount to generate random numbers   
if(isset($_POST['selectNum']) ){
//if(isset($selectNum) ){
    $arrayRand=intval($_POST['selectNum']);    
    for($i=0;$i<=$arrayRand;$i++){ 
        $varNum[$i]=rand(1,10000);
    }
        for($i=9;$i>$arrayRand;$i--){
            $varNum[$i] = 0;            
        }
    //Insert values into Database after implementing the function of generate random number
    if(count($varNum)){  
        $rand_id = mysql_insert_id ();
        $connInsert = new connDB();
        $connInsert->insert_SQL($newRand);
        mysql_query($newRand);
    }
    /*$content = $content. '
        <tr>
          <th>Random Numbers</th>
          <th>'.$row[0].'</th>
          <th>'.$row[1].'</th>
          <th>'.$row[2].'</th>
          <th>'.$row[3].'</th>
          <th>'.$row[4].'</th>
          <th>'.$row[5].'</th>            
          <th>'.$row[6].'</th>
          <th>'.$row[7].'</th>
          <th>'.$row[8].'</th>
          <th>'.$row[9].'</th>
        </tr>'."'n";
        $content .= "</table>'n";
        return $content;*/
    //Show the results by submitting the values of selection box
    echo "<center><h3><b>The random Numbers:</b></h3></center>";
    echo "<table border='1' align='center'>
          <th>Number 1</th>
          <th>Number 2</th>
          <th>Number 3</th>
          <th>Number 4</th>
          <th>Number 5</th>
          <th>Number 6</th>
          <th>Number 7</th>
          <th>Number 8</th>
          <th>Number 9</th>
          <th>Number 10</th>";
        echo "<tr>
        ";
    foreach($varNum as $row){
        $_SESSION['selectNum']=$row;
        if($_SESSION['selectNum']!=null){
            echo "<td style='text-align:center'>".$row."</td>
            ";
        }
    }   
        echo "</tr>
        ";
    echo "</table>";
}   
//Show the results by selecting the Database    
$sqlDBrand = "SELECT id, number1, number2, number3, number4, number5, number6, number7, number8, number9, number10 FROM chccheung.testing_Rand";
$showRand = $conn->query($sqlDBrand);
echo "<center><h3><b>Select the random numbers from Database:</h3></center>";
echo "<table border='1' align='center'>
      <tr>
      <th>ID number</th>
      <th>Number 1</th>
      <th>Number 2</th>
      <th>Number 3</th>
      <th>Number 4</th>
      <th>Number 5</th>
      <th>Number 6</th>
      <th>Number 7</th>
      <th>Number 8</th>
      <th>Number 9</th>
      <th>Number 10</th>";
if($conn->num_rows($showRand) > 0){
    while($rows = $conn->fetch_array_assoc($showRand)){ 
        echo "<tr>
        ";
        echo "<td style='text-align:center'>".$rows['id']."</td>
        ";
        echo "<td style='text-align:center'>".$rows['number1']."</td>
        ";
        echo "<td style='text-align:center'>".$rows['number2']."</td>
        ";
        echo "<td style='text-align:center'>".$rows['number3']."</td>
        ";
        echo "<td style='text-align:center'>".$rows['number4']."</td>
        ";
        echo "<td style='text-align:center'>".$rows['number5']."</td>
        ";
        echo "<td style='text-align:center'>".$rows['number6']."</td>
        ";
        echo "<td style='text-align:center'>".$rows['number7']."</td>
        ";
        echo "<td style='text-align:center'>".$rows['number8']."</td>
        ";
        echo "<td style='text-align:center'>".$rows['number9']."</td>
        ";
        echo "<td style='text-align:center'>".$rows['number10']."</td>
        ";
        echo"</tr>
        ";          
    }
}
echo "</table>";
unset($_SESSION['selectNum']);
?>
</body>

这行代码不关闭:

$this->query("INSERT INTO testing_Rand (number1, // etc

,您在这行之后缺少一个;:

$newRand.=implode(',', $varNum)

如果我明白了,你的意思是这样写:

public function insert_SQL($varNum )
{
    $this->query("INSERT INTO testing_Rand 
        (number1, number2, number3, number4, number5, number6, number7, number8, number9, number10) 
        VALUES (".implode(',', $varNum).")");
}