php和mysql错误?

enter image description here

I have been trying to resolve this issue, I am new to php and mysql. Can someone please point out where the bug is. I know there are certian version of mysql but I have not been successful in find the correct solution for this one.

Below is the code:

<?php
define('DB_HOST', 'localhost'); 
define('DB_USER', 'root'); 
define('DB_PASS', '');
define('DB_DATABASE', 'datadb');

$connection = mysqli_connect(DB_HOST, DB_USER,DB_PASS,DB_DATABASE);

// if there is an error number that exists, echo out message

if(mysqli_connect_errno())
{
    echo "error connecting to database:<br>"; 
    echo mysqli_connect_errno(); 
}


$db_selected = mysqli_select_db($connection,DB_DATABASE); 


function fetch_all($query)
{
$data = array();

$result = mysqli_query($connection,$query);
//iterate through the array
while($row = mysqli_fetch_assoc($result))
{
 $data[] = $row;
}
return $data;
}

function fetch_record($query)
{
$result = mysqli_query($query);
return mysqli_fetch_assoc($result);
}

?>

the $connection variable is undefined inside your functions. Currently in your fetch_all function.

You should set it global like this:

function fetch_all($query) {
    global $connection;
    $data = array();    
    $result = mysqli_query($connection,$query);
    while($row = mysqli_fetch_assoc($result)) {
        $data[] = $row;
    }
    return $data;
}

or better add it as a parameter:

function fetch_all($connection, $query) {
    $data = array();
    $result = mysqli_query($connection,$query);
    while($row = mysqli_fetch_assoc($result)) {
        $data[] = $row;
    }
    return $data;
}

but my favourite is to write a class:

<?php

define('DB_HOST', 'localhost'); 
define('DB_USER', 'root'); 
define('DB_PASS', '');
define('DB_DATABASE', 'datadb');

class db {

    private $connection;
    private $db_selected;

    public function connect() { 
        $this->connection = mysqli_connect(DB_HOST, DB_USER,DB_PASS,DB_DATABASE);
        if(mysqli_connect_errno()) {
            echo "error connecting to database:<br>"; 
            echo mysqli_connect_errno(); 
        }
        $this->db_selected = mysqli_select_db($connection,DB_DATABASE);         
    }

    public function fetch_all($query) {
        $data = array();
        $result = mysqli_query($this->connection,$query);
        while($row = mysqli_fetch_assoc($result)) {
         $data[] = $row;
        }
        return $data;
    }

    public function fetch_record($query) {
        $result = mysqli_query($this->connection, $query);
        return mysqli_fetch_assoc($result);
    }
}

Usage:

$db = new db();
$db->connect();
$data = $db->fetch_all();

by the way: do you really need to do a mysqli_select_db if you already passed the DB_DATABASE to mysqli_connect? I don't think so.

Since you are not passing $connection to fetch_all, you should specify it as global inside the fetch_all function, ie:

function fetch_all($query) {
  global $connection;
  $data = array();

  $result = mysqli_query($connection,$query);
  //iterate through the array
  while($row = mysqli_fetch_assoc($result)) {
    $data[] = $row;
  }
  return $data;
}