This is a quick summary of how to retrieve data from MySQL database using PHP and display it using jQuery and Ajax.
Step 1 - Prepare MySQL database and data table.
The SQL query below creates a database and data table and fills the table with sample data.
This blog is for keeping my study notes, sharing knowledge and showing the products of my study.. I welcome your feedback...
Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts
2019/09/30
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
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
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."
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/20
PHP - How to pass variables to Javascript
This post is about how to pass PHP variable to Javascript.
Sample Code (example.php)
Sample Code (example.php)
<?php $user = "Bernie"; ?> <div class = "container" style = "background-color:#332B57;"> <div class = "content"> </div> </div> <div class = "container"> <div class = "content"> </div> </div> <script> function welcomeMsg(){ var user = "<?php echo $user; ?>"; alert(user); } window.addEventListener('load',welcomeMsg); </script>
2017/10/03
Raspberry Pi - How to check whether PHP is installed and its version
This is a quick summary of how to check whether PHP is installed and its version.
1. Login to your Raspberry Pi.
2. Change the working directory to "/var/www/html".
1. Login to your Raspberry Pi.
2. Change the working directory to "/var/www/html".
Subscribe to:
Posts (Atom)

