I have a script where I try to get the date from my database.
The script needs to show: The date in the database is (date). Click here to continue. When I run the SQL query in phpMyAdmin, the SQL query returns the date. When I run it in my script I get no result.
Here is my script:
<?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 = "set lc_time_names = 'nl_NL';";
$sql = "SELECT date_format(date, '%e %M %Y') AS date FROM table WHERE id='1'";
$result = $conn->multi_query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "The date in the database is:";
echo " " . $row['date'] . ". ";
echo "Click here to continue.";
}
} else {
echo "0";
}
?>
When I run this script I get 0. When I change echo "0"; with echo " " . $row['date'] . ". "; I get a empty page.
What am I doing wrong? How can I fix this?
I had another edit that isn't approved...does this work?
Just splitting your queries/variables into 2 separate ones--only only worry that first value may not persist for you.
Like so:
<?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 = "set lc_time_names = 'nl_NL'";
$sql2 = "SELECT date_format(date, '%e %M %Y') AS date FROM table WHERE id='1'";
$resultZ = $conn->query($sql);
$result = $conn->query($sql2);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "The date in the database is:";
echo " " . $row['date'] . ". ";
echo "Click here to continue.";
}
} else {
echo "0";
}
?>
You are using mysqli_query (which executes exactly ONE query) and thus your script is only ever getting to the set variable statement and ending at the semicolon.
Follow the advice below--the 2nd example shows using multi_query and setting the variable value just like you do in phpMyAdmin.
Try with:
<?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 date_format(date, '%e %M %Y') AS date FROM table WHERE id='1'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "The date in the database is:";
echo " " . $row['date'] . ". ";
echo "Click here to continue.";
}
} else {
echo "0";
}
?>
Or use multi_query:
<?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 = "set lc_time_names = 'nl_NL'; SELECT date_format(date, '%e %M %Y') AS date FROM table WHERE id='1'";
$result = $conn->multi_query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "The date in the database is:";
echo " " . $row['date'] . ". ";
echo "Click here to continue.";
}
} else {
echo "0";
}
?>
You have formatted the sql script.. So instead of writing $row['date'] just put like this $row[0]
Hope this works for you..
Related
while executing query got this error "Error updating record: You have an error in your SQL syntax"
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SET #a:=0;UPDATE registrations SET EXIBIT_NO=#a:=#a+1 ORDER BY GR_ID";
You can wrtie:
$result=mysqli_query($conn,$sql);
Code Example:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
I would need some help with showing data that I have on my database but I can't seen to be able to.`
$servername = "servername";
$username = "username";
$password = "password";
$dbname = "dbname";
$connect = mysqli_connect($servername, $username, $password, $dbname) or die ("connection failed");
//Query
$query = "SELECT * FROM 'Students'";
mysqli_master_query($dbname, $query) or die ("Error while Query");
$result = mysqli_master_query($dbname, $query);
$row = mysql_fetch_array($result);
while ($row = mysql_fetch_array($result)) {
echo "<p>".$row['Name']."</p>";
};
mysql_close($connect);
?>`
I am pretty new to this so I could have missed something simple. Any help appreciated.
Below is a sample code of the normal procedure to connect to a database and to select data from it. Please follow this type of coding since MySQL is now deprecated and MySQLi is used.
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
For further reference check out http://php.net/manual/en/book.mysqli.php and also https://www.w3schools.com/php/php_mysql_insert.asp
I need the posts on my guestbook to be sorted by time ascending.
I have been told that I need to add:
ORDER by datetime
in my code. But I dont know what the correct way to enter this line is.
Here is my code:
<?php
$host = "ZZZ"; // Host name
$username = "ZZZ"; // Mysql username
$password = "ZZZ"; // Mysql password
$db_name = "ZZZ"; // Database name
$tbl_name = "ZZZ"; // Table name
// Create connection
$conn = mysqli_connect($host, $username, $password, $db_name);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM ". $tbl_name ." ";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "
<b> Name: ". $row["name"]."<br>
Date Added: : ". date('d-m-Y H:i', $row["datetime"]) ."</b><br><br>
Comment: ". $row["comment"]."<br>
<br>
";
}
} else {
echo "0 results";
}
mysql_close(); //close database
?>
$sql = "SELECT * FROM " . $tbl_name . " ORDER BY datetime ASC";
Also you have mysql_close and your other functions are mysqli
I have a PHP script that outputs my events from my ICAgenda calendar on the website.
It outputs "name" "date" and "location" / event.
How can I import this into XCODE to show the events?
I thought styling the php script, and then add it as a webview in XCODE ? Or I'll guess there is another workaround to get the data in XCODE ?
This is the code that gathers the events:
<?php
$servername = "myservername";
$username = "username";
$password = "password";
$dbname = "dbname";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT next, title, city FROM jos_icagenda_events ORDER BY next";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "Datum & tijd: " . $row["next"]. "<br>";
echo "Plaats: " . $row["title"]. "<br>";
echo "Locatie: " . $row["city"]. "<br><br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
I am looking for a solution so that the user can select an appointment slot. I am trying to add a radio button, but not sure how to go about it.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "hairdressingapointments";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT `ApointmentDate`, `ApointmentTime` FROM `apointments`
WHERE `ApointmentDate` between curdate() and curdate()+ interval '50' day
&& quantity=1 && staffID=901";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo " Apointment Date: " . $row["ApointmentDate"]. " Appointment Time: "
. $row["ApointmentTime"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>