Sunday, January 25, 2015

Basic MYSQLI Commands

CONNECT TO DATABASE
$host=localhost
$user=mysqluser
$password=mysqlpassword
$database=database
$mysqli = new mysqli($host, $user, $password, $database);

QUERY DATABASE
$query="SELECT * FROM table";
$result=$mysqli -> query($query);

GET NUMBER OF ROWS
$result=$mysqli -> query("SELECT * FROM table");
$row_cnt = $result -> num_rows;

--- or ---

$row_cnt = mysqli_num_rows($result);

OUTPUT QUERY RESULTS
while($row = $result -> fetch_assoc()){
    echo $row['column name'] ;
}

SELECT FROM TABLE BASED UPON TIMESTAMP
this assumes that "date" in the mysql table is a timestamp. This will select all entries made where the date was within the last 24h.

SELECT * FROM table WHERE date >= now() - INTERVAL 1 DAY;