PHP-mysqli不能给出所有结果吗?

时间:2018-07-08 13:24:48

标签: php mysqli

我正在尝试获取所有行。但是它不会返回所有行。 当我添加Sub Test() Dim r As Long Dim a As Long Dim row1 As Range, row2 As Range, row3 As Range Dim Val1 As Long, val2 As Long, val3 As Long Dim nextFree As Long For r = 26 To 34 For a = 1 To 9 Set row1 = Range(Cells(r, 4), Cells(r, 12)).Find(What:=a) If row1 Is Nothing Then Val1 = 0 Else Val1 = row1.Value Set row2 = Range(Cells(r + 1, 4), Cells(r + 1, 12)).Find(What:=a) If row2 Is Nothing Then val2 = 0 Else val2 = row2.Value Set row3 = Range(Cells(r + 2, 4), Cells(r + 2, 12)).Find(What:=a) If row3 Is Nothing Then val3 = 0 Else val3 = row3.Value If Val1 = a And val3 = a And val2 = 0 Then If WorksheetFunction.CountA(Range(Cells(r + 1, 4), Cells(r + 1, 6))) = 2 Then nextFree = Range(Cells(r + 1, 4), Cells(r + 1, 6)).Cells.SpecialCells(xlCellTypeBlanks).Column Worksheets("Sheet1").Cells(r + 1, nextFree).Value = a End If ElseIf Val1 = a And val2 = a And val3 = 0 Then If WorksheetFunction.CountA(Range(Cells(r + 2, 4), Cells(r + 2, 6))) = 2 Then nextFree = Range(Cells(r + 2, 4), Cells(r + 2, 6)).Cells.SpecialCells(xlCellTypeBlanks).Column Worksheets("Sheet1").Cells(r + 2, nextFree).Value = a End If ElseIf val2 = a And val3 = a And Val1 = 0 Then If WorksheetFunction.CountA(Range(Cells(r + 2, 4), Cells(r + 2, 6))) = 2 Then nextFree = Range(Cells(r + 2, 4), Cells(r + 2, 6)).Cells.SpecialCells(xlCellTypeBlanks).Column Worksheets("Sheet1").Cells(r + 2, nextFree).Value = a End If End If Next a r = r + 2 Next r End Sub 时有效,但limit 1无效。

limit 2 or 10 etc etc

编辑:未显示任何输出

<?php
if ( !class_exists( 'DB' ) ) {
    class DB {
        public function __construct($user, $password, $database, $host = 'localhost') {
            $this->user = $user;
            $this->password = $password;
            $this->database = $database;
            $this->host = $host;
        }
        protected function connect() {
            return new mysqli($this->host, $this->user, $this->password, $this->database);
        }
        public function query($query) {
            $db = $this->connect();
            $result = $db->query($query);

            while ( $row = $result->fetch_object() ) {
                $results[] = $row;
            }

            return $results;
        }
        public function insert($table, $data, $format) {
            // Check for $table or $data not set
            if ( empty( $table ) || empty( $data ) ) {
                return false;
            }

            // Connect to the database
            $db = $this->connect();

            // Cast $data and $format to arrays
            $data = (array) $data;
            $format = (array) $format;

            // Build format string
            $format = implode('', $format); 
            $format = str_replace('%', '', $format);

            list( $fields, $placeholders, $values ) = $this->prep_query($data);

            // Prepend $format onto $values
            array_unshift($values, $format); 

            // Prepary our query for binding
            $stmt = $db->prepare("INSERT INTO {$table} ({$fields}) VALUES ({$placeholders})");

            // Dynamically bind values
            call_user_func_array( array( $stmt, 'bind_param'), $this->ref_values($values));

            // Execute the query
            $stmt->execute();

            // Check for successful insertion
            if ( $stmt->affected_rows ) {
                return true;
            }

            return false;
        }


        public function update($table, $data, $format, $where, $where_format) {
            // Check for $table or $data not set
            if ( empty( $table ) || empty( $data ) ) {
                return false;
            }

            // Connect to the database
            $db = $this->connect();

            // Cast $data and $format to arrays
            $data = (array) $data;
            $format = (array) $format;

            // Build format array
            $format = implode('', $format); 
            $format = str_replace('%', '', $format);
            $where_format = implode('', $where_format); 
            $where_format = str_replace('%', '', $where_format);
            $format .= $where_format;

            list( $fields, $placeholders, $values ) = $this->prep_query($data, 'update');

            //Format where clause
            $where_clause = '';
            //$where_values = '';
      $where_values = null;
            $count = 0;

            foreach ( $where as $field => $value ) {
                if ( $count > 0 ) {
                    $where_clause .= ' AND ';
                }

                $where_clause .= $field . '=?';
                $where_values[] = $value;

                $count++;
            }

            // Prepend $format onto $values
            array_unshift($values, $format);
            $values = array_merge($values, $where_values);

            // Prepary our query for binding
            $stmt = $db->prepare("UPDATE {$table} SET {$placeholders} WHERE {$where_clause}");

            // Dynamically bind values
            call_user_func_array( array( $stmt, 'bind_param'), $this->ref_values($values));

            // Execute the query
            $stmt->execute();

            // Check for successful insertion
            if ( $stmt->affected_rows ) {
                return true;
            }

            return false;
        }
        public function select($query, $data, $format) {
            // Connect to the database
            $db = $this->connect();

            //Prepare our query for binding
            $stmt = $db->prepare($query);

            //Normalize format
            $format = implode('', $format); 
            $format = str_replace('%', '', $format);

            // Prepend $format onto $values
            array_unshift($data, $format);

            //Dynamically bind values
            call_user_func_array( array( $stmt, 'bind_param'), $this->ref_values($data));

            //Execute the query
            $stmt->execute();

            //Fetch results
            $result = $stmt->get_result();

            //Create results object
            while ($row = $result->fetch_object()) {
                $results[] = $row;
            }

            return $results;
        }
        public function delete($table, $id) {
            // Connect to the database
            $db = $this->connect();

            // Prepary our query for binding
            $stmt = $db->prepare("DELETE FROM {$table} WHERE ID = ?");

            // Dynamically bind values
            $stmt->bind_param('d', $id);

            // Execute the query
            $stmt->execute();

            // Check for successful insertion
            if ( $stmt->affected_rows ) {
                return true;
            }
        }
        private function prep_query($data, $type='insert') {
            // Instantiate $fields and $placeholders for looping
            $fields = '';
            $placeholders = '';
            $values = array();

            // Loop through $data and build $fields, $placeholders, and $values         
            foreach ( $data as $field => $value ) {
                $fields .= "{$field},";
                $values[] = $value;

                if ( $type == 'update') {
                    $placeholders .= $field . '=?,';
                } else {
                    $placeholders .= '?,';
                }

            }

            // Normalize $fields and $placeholders for inserting
            $fields = substr($fields, 0, -1);
            $placeholders = substr($placeholders, 0, -1);

            return array( $fields, $placeholders, $values );
        }
        private function ref_values($array) {
            $refs = array();

            foreach ($array as $key => $value) {
                $refs[$key] = &$array[$key]; 
            }

            return $refs; 
        }
    public function get_results($query) {
        return $this->query($query);
    }
    public function get_row($query) {
        $results = $this->query($query);

        return $results[0];
    }
    }
}

//$db = new DB('root', '', 'test');
//print_r($db->get_results("SELECT * FROM objects"));

?>

1 个答案:

答案 0 :(得分:0)

以下工作。

field--node--field-newsdesk-and-press--interview.html.twig

OR

<?php
//error_reporting(E_ERROR | E_PARSE);
//include_once('common.php');
error_reporting(E_ALL);
ini_set('display_errors', 1);

function safe_json_encode($value, $options = 0, $depth = 512){
    $encoded = json_encode($value, $options, $depth);
    switch (json_last_error()) {
        case JSON_ERROR_NONE:
            return $encoded;
        case JSON_ERROR_DEPTH:
            return 'Maximum stack depth exceeded'; // or trigger_error() or throw new Exception()
        case JSON_ERROR_STATE_MISMATCH:
            return 'Underflow or the modes mismatch'; // or trigger_error() or throw new Exception()
        case JSON_ERROR_CTRL_CHAR:
            return 'Unexpected control character found';
        case JSON_ERROR_SYNTAX:
            return 'Syntax error, malformed JSON'; // or trigger_error() or throw new Exception()
        case JSON_ERROR_UTF8:
            $clean = utf8ize($value);
            return safe_json_encode($clean, $options, $depth);
        default:
            return 'Unknown error'; // or trigger_error() or throw new Exception()

    }
}

function utf8ize($mixed) {
    if (is_array($mixed)) {
        foreach ($mixed as $key => $value) {
            $mixed[$key] = utf8ize($value);
        }
    } else if (is_string ($mixed)) {
        return utf8_encode($mixed);
    }
    return $mixed;
}

try{  

    $db = mysqli_connect('localhost', 'root', '', 'test');

    $sql =  "SELECT id,firstname FROM test";
    $result = mysqli_query($db, $sql);
    mysqli_set_charset($db, "utf8");
    //$rows = array();
    while($row = mysqli_fetch_all($result, MYSQLI_ASSOC)) {
        $results[] = $row;
    }
    mysqli_free_result($result);
    mysqli_close($db);



  $json= '';
  if (count($result) > 0 ) {
    $json = array(
      'result' =>'ok',      
      'db' => $results,
    ); 
  } 
  else {
    $json = array(
      'result' =>'fail',
    );
  }

  //$json = iconv('UTF-8', 'UTF-8//IGNORE', utf8_encode($json));

  //echo json_encode($json);
  echo safe_json_encode($json);

  //print_r($json);
  //echo json_last_error();
  exit;
} 
catch(Exception $e) {
  echo 'Caught exception: ', $e;
}

?>