Showing posts with label MySQL. Show all posts
Showing posts with label MySQL. Show all posts

2019/09/27

PHP and MySQL - Trying out "mysqli_fetch_assoc()".

This is a quick experiment on using "mysqli_fetch_assoc()" to retrieve data from MySQL database and display the retrieved data.

The Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php

$conn = mysqli_connect("localhost", "root", "", "sensors");

$query = "SELECT * FROM t_viewname";

// Get the number of row in the result
$number_filter_row = mysqli_num_rows(mysqli_query($conn, $query));

echo "No. of Row: ".$number_filter_row;
//echo $number_filter_row;
echo "<br>";

// mysqli_query() -> Perform queries against the database.
// For example:
//      mysqli_query($con,"SELECT * FROM Persons");
//      mysqli_query($con,"INSERT INTO Persons (FirstName,LastName,Age) VALUES ('Glenn','Quagmire',33)");

$result = mysqli_query($conn, $query);

// Reference:
// https://stackoverflow.com/questions/14456529/mysqli-fetch-array-while-loop-columns

$post = array();
while ($row = mysqli_fetch_assoc($result)) {
    $post[] = $row;   
}

foreach ($post as $row) { 
    foreach ($row as $element) {
        echo $element.", ";
    }
    echo "<br>"; 
}

?>

Note, see below for the explanation on why there is no index inside the while loop.

Ref.: https://stackoverflow.com/questions/9105419/generate-array-from-php-while-loop


The Result


Reference:

mysqli_fetch_array while loop columns
https://stackoverflow.com/questions/14456529/mysqli-fetch-array-while-loop-columns

PHP and MySQL - A quick experiment on "mysqli_fetch_assoc()", "mysqli_fetch_row()", and "mysqli_fetch_array()".

This is a quick experiment on "mysqli_fetch_assoc()", "mysqli_fetch_row()", and "mysqli_fetch_array()".

As for which one to use, I think below comment from stackoverflow.

"I'd typically use _fetch_assoc, because I want to be accessing my data by name because it makes the code more comprehensible and less prone to errors. _fetch_array returns numerically and string indexed arrays, which is superfluous and potentially maybe possibly more wasteful. Not that it really matters in the grand scheme of things, but still. Do what you need; if you want named keys, use the function that gives you named keys and nothing more."

2018/09/17

How to change the time zone setting of MySQL running on cloud server such as DigitalOcean

I ran into an issue with the time stamp of the data stored in the MySQL database running on DigitalOcean droplet. The time stamp is 8 hours off from my current time. This post is a quick summary of how to solve this issue.

Check the current local time setting of the LAMP server

Ref.: https://www.tecmint.com/check-linux-timezone/

So, the time zone of the server is set to UTC. It needs to be changed to my time zone so that the time stamp could be corrected.

wei48221@lamp-s-1vcpu-1gb-sfo2-01:/etc/mysql$ timedatectl
      Local time: Tue 2018-09-18 12:39:28 UTC
  Universal time: Tue 2018-09-18 12:39:28 UTC
        RTC time: Tue 2018-09-18 12:39:28
       Time zone: Etc/UTC (UTC, +0000)
 Network time on: yes
NTP synchronized: yes
 RTC in local TZ: no

2017/08/26

Raspberry Pi - Installation and configuration of MariaDB and phpMyAdmin on Raspbian Jessie Lite

This post is a quick summary of how to install MariaDB on Raspberry Pi running Raspbian as well as on how to perform basic configuration and operation.

The Environment:

- Raspberry Pi 1 Model B
- OS: Raspbian Jessie Lite
- Primary Storage: 16GB Class 10 SD Card