Post date format and select date format from the database - php

I have a textbox that displays the current date in format: d-m-Y
<input type="text" name="date" class="form-control" value="<?php echo date('d-m-Y');?>"/>
When I save this the date will written to my database like: Y-m-d. For example: the current date is: 02-06-2016. My script writes this to the database as: 2002-06-16.
I am using the following code to save the data to my database:
<?php
session_start();
$correct = true;
$date = $_POST['date'] ;
if($correct){
$db = new PDO('mysql:host=localhost;dbname=db', 'root', '');
$query = "INSERT INTO table(date) VALUES (?)";
$stmt = $db->prepare($query);
$stmt->execute(array($date));
header('Location: ./index.php');
}
else
{
echo "Error";
}
?>
What do I need to change in my code so it will post it like: 2016-06-02 Y-m-d or 02-06-2016 d-m-Y?
If d-m-Y is not possible, how can show the data that is written like Y-m-d in the database show as format d-m-Y in my php script?
Edit:
For selecting the date I use:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "db";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM table WHERE id='1' ORDER BY id DESC LIMIT 1";
$sql = date("d-m-Y",strtotime($row['date']));
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<td>" . $row["id"]. "</td>";
echo "<td>" . $row["date"]. "</td>";
}
} else {
echo "<p> </p>";
}
$conn->close();
?>

$date = $_POST['date'] ;
$date = date("Y-m-d",strtotime($date));
To show date in d-m-Y format in HTML page, again convert it into
$date = date("d-m-Y",strtotime($row['date']));
[Note: I'm assuming your fetch array as $row and column as date.]
For more info, please have a look on strtotime - php manual
Update Code (As question is edited)
$sql = "SELECT * FROM table WHERE id='1' ORDER BY id DESC LIMIT 1";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<td>" . $row["id"]. "</td>";
echo "<td>" . date("d-m-Y",strtotime($row['date'])). "</td>";
}
} else {
echo "<p> </p>";
}
$conn->close();

Related

How to split a multi value sql column into three single result

So I have a SQL column that stores data like this.
"[1338,0,8523]"
I was wondering if I can then garb each one individually and because the value is minutes, if I could times it by 60 to get the hours and then display it, I've used the below for my other results but I'm stuck on this one.
<?php
$servername = "localhost";
$username = "Time";
$password = "fakepassword";
$dbname = "time";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed to Timebase Highscores: " . $conn->connect_error);
}
$sql = "SELECT name, time FROM players ORDER BY time DESC LIMIT 5";
$result = $conn->query($sql);
echo "Top 5 Life Wasters". "<br>";
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo $row["name"]. " - $" . $row["time"] . "<br>";
}
} else {
echo "Error fetching any players from database.";
}
$conn->close();
?>

how to php echo individual column values of a result row from mysql query?

The below code works perfectly and gives me the "id" "firstname" and "lastname". What I want is to use a loop to echo all the field values in the result row without having to quote each column name like
$row["id"]
below is the working code
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<br> id: ". $row["id"]. " - Name: ". $row["firstname"]. " " .
$row["lastname"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
any thoughts please
This should work..
while($row = $result->fetch_array()) {
$i=0;
while($i<count($row)){
echo $row[$i];
$i++;
}
}
just use a foreach loop inside while like this
foreach($row as $key=>$value){
echo "<br> $key: ". $value. "<br>";
}
If I understand what you want to do is automate the output of the name of each column with its value (independent of the number of columns you get).
So do it like this :
if ($result->num_rows > 0) {
foreach($result->fetch_assoc() as $row) { // browse each records
foreach($row as $col => $value) { // browse each columns on a record
echo "<br>$col: $value<br>";
}
}
}
else {
echo "no result";
}

Displaying results of SQL query as a table?

I am trying to output the results of an SQL query as a table on a page on my website. I have found a few solutions online but I can't get any of them to work properly. Right now I copied and pasted a bit of code to just output the first two columns but I can't figure out how to get every column in a table. I am new to PHP and web development in general so any help would be appreciated.
My PHP:
<?php
SESSION_START() ;
$servername = "localhost";
$username = "MY USERNAME";
$password = "MY PASSSWORD";
$dbname = "MY DATABASE NAME";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
//$_session['userid'] = $userlogged;
$sql = "SELECT * FROM `climbs` WHERE `userlogged` = '" . $_SESSION['userid'] . "'";
$result = mysqli_query($conn,$sql);
if ($result->num_rows > 0) {
echo "<table><tr><th>ID</th><th>Name</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["climb-id"]. "</td><td>" . $row["climbname"]. " " . $row["cragname"]. "</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
mysqli_close($conn);
?>
check with var_dump :
some like that:
$result = mysqli_query($conn,$sql);
var_dump($result);
if ($result->num_rows > 0) {
maybe the query it's wrong.

PHP Array ForEach SQL Query

I am trying to use a FOREACH loop to query a database based on each value in the $userid array below. I am also looping through the $grade array as I need the corresponding value for the sql query to then put into a HTML table.
//Decode JSON file to an Object
$json_d = json_decode(file_get_contents("results.json"));
//Provisional Array Setup for Grades
$grade = array();
$userid = array();
foreach($json_d->assignments[0]->grades as $gradeInfo) {
$grade[] = $gradeInfo->grade;
$userid[] = $gradeInfo->userid;
}
//Server Details
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "moodle";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "<table><tr><th>First Name </th><th>Last Name </th><th>Grade </th></tr>";
foreach($userid as $id) {
foreach($grade as $grd) {
$sql = "SELECT firstname, lastname FROM mdl_user WHERE id='$id'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "<tr><td>" . $row["firstname"]. "</td><td>" . $row["lastname"]. "</td><td> " . $grd . "</td></tr>";
}
} else {
echo "ERROR!";
}
}
}
echo "</table>";
mysqli_close($conn);
I have checked the $grade and $userid array and they do contain the correct values however running the PHP file I only get the first record outputted in the table. E.G.
FirstName LastName Grade
Student 1 85
Whereas I need the other 2 records that are supposed to appear.

Getting only one row of a mysql table displayed using PHP

I Currently have this code that displays all entries rows from a mysql table and displays them on a web page;
<?php
$servername = "localhost";
$username = "appuser1";
$password = "*****";
$dbname = "acmefg_app";
$row = mysql_fetch_array(mysql_query("SELECT jobnumber FROM appdata WHERE id = '5608' LIMIT 1"));
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, jobnumber, assetnumber FROM appdata";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<br> id: ". $row["id"]. "<br>";
echo "<br> Job number: ". $row["jobnumber"]. "<br>";
echo "<br> Asset number: " . $row["assetnumber"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
How can I change this so it only displays one result? Lets say in this case I wanted to display the row with an id of 5611? Many Thanks
If you want to show only one row then no need to use while() loop. Remove loop
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
echo "<br> id: ". $row["id"]. "<br>";
echo "<br> Job number: ". $row["jobnumber"]. "<br>";
echo "<br> Asset number: " . $row["assetnumber"] . "<br>";
}
Or can set LIMIT 1 in your query string.
SELECT id, jobnumber, assetnumber FROM appdata LIMIT 1

Categories