I am trying to get data from sql using php. But I am getting error 500 while retrieving Date values. No error while retrieving other type of data.
Can anyone tell me why it is happening?
Value in cell : "1999/12/31 16:01:05.000"
Windows Server
$tsql = "
SELECT
ProgramName,
RepeatID,
Round(ScrapFraction*100,2) As Yeld,
MachineName,
Round(CuttingTime*60,2) AS CuttingTimeMins,
PostDateTime,
PostedByUserID,
Thickness,
Material,
PostDateTime
FROM
dbo.Program";
/* Execute the query. */
$stmt = sqlsrv_query( $conn, $tsql);
if ( $stmt )
{
//echo "Statement executed.<br>\n";
}
else
{
echo "Error in statement execution.\n";
die( print_r( sqlsrv_errors(), true));
}
echo "<Table width='1200' border='1'>";
echo "<tr>";
echo "<Td>Program Name</td>";
echo "<Td>Repeat</td>";
echo "<Td>Yeld</td>";
echo "<Td>MachineName</td>";
echo "<Td>Cutting Time</td>";
echo "<Td>PostedBy</td>";
echo "<Td>Thickness</td>";
echo "<Td>Material</td>";
echo "<Td>data</td>";
echo "</tr>";
while( $row = sqlsrv_fetch_array($stmt))
{
echo "<tr>";
echo "<Td><a href='xa.php?revice=".$row['ProgramName']."'>".$row['ProgramName']."</a></td>";
echo "<Td><center>".$row['RepeatID']."</center></td>";
echo "<Td><center>".$row['Yeld']."%</center></td>";
echo "<Td>".$row['MachineName']."</td>";
echo "<Td>".$row['CuttingTimeMins']."</td>";
echo "<Td><center>".$row['PostedByUserID']."</td>";
echo "<Td><center>".$row['Thickness']."</center></td>";
echo "<Td><center>".$row['Material']."</center></td>";
echo "</tr>";
}
/* Free statement and connection resources. */
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
Related
I have tried everything I can find to try and get time formatted results from my table using mysql_fetch_array while also getting other results.
This is my code:
<?php
$con = new mysqli("mysql.hostinger.co.uk", "user", "password", "database");
$con->set_charset('utf8mb4');
//Retrieve all the data from the Update Log Table
$result = $con->query("SELECT requestID, songTitle, artistName, requester, dedicatedTo, TIME_FORMAT(requestTime, '%r'), dance FROM `table` ORDER BY requestID DESC") or die(mysql_error());
echo "<form class='formleft'>";
//keeps getting the next row until there are no more to get
while($row = mysqli_fetch_array($result)){
//Print out the contents of each row into a table
echo "<div class='reqgap'></div>";
echo "<input type='checkbox' id='play";
echo $row['requestID'];
echo "' class='play checkbox' autocomplete='off' ";
if ($row['requestID'] == '0') {
echo "style='display:none;' />";
} else {
echo "/>";
};
echo "<label class='played' for='play";
echo $row['requestID'];
echo "' ";
if ($row['requestID'] == '0') {
echo "hidden >";
} else {
echo ">";
};
echo "Played";
echo "</label>";
echo "<article class='request songs req";
echo $row['requestID'];
if ($row['requestID'] % 2 == 0) {
echo " even";
} else {
echo " odd";
};
echo "' >";
echo "<article class='requestleft'>";
echo "<article class='requestnum'>";
echo "<p class='requestnumt'>";
if ($row['requestID'] == '') {
echo "RequestID Error";
} else {
echo $row['requestID'];
}
echo "</p>";
echo"</article>";
echo "<article class='requesttime'>";
echo "<p class='requesttimet'>";
if ($row['requestTime'] == '') {
echo "Request Time Error";
} else {
echo $row['requestTime'];
}
echo " UTC</p>";
echo"</article>";
It just shows my error message "Request Time Error" when it comes to displaying that data.
Any assistance will be greatly appreciated
New to PDO and I have a PDO connection script which I am requiring at the top but this is what I have to try and just display the database that is made of up 4 employees and their birthdays. Whenever I run it, I get nothing. Am I at least in the right ballpark here?
<?php
require "pdoconnect.php";
try{
$stmt = $conn->prepare("SELECT bid, bname, bday FROM birthday");
$stmt->execute();
# setting the fetch mode
$result = $stmt->fetchAll();
if( ! $result){
print('No Records Found');
}
else{
echo "<table border='1' align='center' cellpadding='5px' cellspacing='2px'>";
//while($row = $stmt->fetch())
echo "<tr><th>Bid</th><th>Name</th><th>Birthdaay</th><th colspan='2'></th></tr>";
foreach($result as $row){
echo "<tr>";
echo "<td>";
echo $row['bid'];
echo "</td>";
echo "<td>";
echo $row['bname'];
echo "</td>";
echo "<td>";
echo $row['bday'];
echo "</td>";
}
echo "</table>";
}
}
catch(PDOException $e){
echo "Error: " . $e->getMessage();
}
$conn = null;
echo "</table>";
?>
This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 6 years ago.
I am currently trying to output a the results of a MySQL table in PHP. I have a general understanding of web based programming but not enough to debug my code. I know the SQL is good, and the database is linked to my site it's just a matter of making it post to a table. I will post the code and would appreciate some help:
<?php
$sql = "SELECT player_name AS 'Name',
position AS 'Position',
team AS 'Team',
opp AS 'Opponent'
FROM `dbname`
WHERE position = 'QB'";
$stmt = $db->query($sql);
if($stmt-> num_rows > 0) {
echo "<table class='table'>";
echo "<thead class='thead-inverse'>";
echo "<tr><th>Name</th><th>Position</th><th>Team</th><th>Opponent</th>";
echo "</thead>";
echo "<tbody>";
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<tr><td>";
echo $row['Name'];
echo "</td><td>";
echo $row['Position'];
echo "</td><td>";
echo $row['Team'];
echo "</td><td>";
echo $row['Opponent'];
echo "</td></tr>";
}
echo "</tbody>";
echo "</table>";
}
else {
echo "No Results";
}
All that I get from this is a no results output.
You forgot to close the query with a double quote.
<?php
$sql = "SELECT player_name AS 'Name',
position AS 'Position',
team AS 'Team',
opp AS 'Opponent'
FROM `dbname`
WHERE position = 'QB'";
$stmt = $db->query($sql);
$stmt -> execute();
if($stmt-> num_rows > 0) {
echo "<table class='table'>";
echo "<thead class='thead-inverse'>";
echo "<tr><th>Name</th><th>Position</th><th>Team</th><th>Opponent</th>";
echo "</thead>";
echo "<tbody>";
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<tr><td>";
echo $row['Name'];
echo "</td><td>";
echo $row['Position'];
echo "</td><td>";
echo $row['Team'];
echo "</td><td>";
echo $row['Opponent'];
echo "</td></tr>";
}
echo "</tbody>";
echo "</table>";
}
else {
echo "No Results";
}
Add $stmt -> execute();
You can also use $stmt -> store_result(); after the execute part
I'm not that good in programming PHP, and still learning from it.
Here's my problem, I need to display the result of rows from two different tables, had researched things and tried but all failed.
Hope someone could give some advice with my line of code.
$query = "SELECT tblparent.*, tblchild.* FROM tblparent, tblchild* FROM tblparent";
$num_results = $result->num_rows;
$result = $mysqli->query( $query );
if( $num_results ){
echo "<center><table border='1' id='members'>";
echo "<tr>";
echo "<th>Parent ID</th>";
echo "<th>Parent Firstname</th>";
echo "<th>Parent Lastname</th>";
echo "<th>Parent Middlename</th>";
echo "<th>Child ID</th>";
echo "<th>Child Firstname</th>";
echo "<th>Child Middlename</th>";
echo "<th>Child Lastname</th>";
echo "<th>Action</th>";
echo "</tr>";
while( $row = $result->fetch_assoc() ){
extract($row);
echo "<tr>";
echo "<td>{$Parent_ID}</td>";
echo "<td>{$PFname}</td>";
echo "<td>{$PLname}</td>";
echo "<td>{$PMname}</td>";
echo "<td>{$Child_ID}</td>";
echo "<td>{$CFname}</td>";
echo "<td>{$CMname}</td>";
echo "<td>{$CLname}</td>";
echo "<td>";
echo "<a href='#' onclick='delete_mem( {$Parent_ID} );'>Delete</a>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
}
else{
echo "No records found.";
}
$result->free();
$mysqli->close();
I see two mistakes:
you should satisfy the right order of statements, $result should be
before $num_results assigment.
it seems that there is a mistake in your SQL query.
You need to adjust the following code, I am assuming that tblparent has an id and tblchild has a relation to tblparent id as parent_id:
$query = "SELECT tblparent.*, tblchild.* FROM tblparent, tblchild WHERE tblparent.id = tblchild.parent_id";
$result = $mysqli->query( $query );
$num_results = $result->num_rows;
I am using this code which allows me to see my DB records in a table and in this table there is an option to delete or edit the records.
Only I get this error message and I can't figure out what I am doing wrong.
The error message:
Fatal error: Call to a member function prepare() on a non-object in C:\wamp\www\Inventaris++\NinjaCodeDelete.php on line 32
The code:
<?php
include'Connect2db3.php';
$action = isset($_GET['action']) ? $_GET['action']: "";
if($action=='delete'){ //if the user clicked ok, run our delete query
try {
$query = "DELETE FROM BCD WHERE id = ?";
$stmt = $conn->prepare($query);
$stmt->bindParam(1, $_GET['id']);
$result = $stmt->execute();
echo "<div>Record was deleted.</div>";
}catch(PDOException $exception){ //to handle error
echo "Error: " . $exception->getMessage();
}
}
$query = "SELECT ID, Categorie, SerieNummer, MacAdress, ProductCode, Prijs, RekNummer, PaletNummer, Hoeveelheid, Aantekeningen FROM BCD";
$stmt = $conn->prepare( $query );
$stmt->execute();
$num = $stmt->rowCount();
echo "<a href='reports.php'>View Reports</a>";
if($num>0){ //check if more than 0 record found
echo "<table border='1'>";//start table
echo "<tr>";
echo "<th>Categorie</th>";
echo "<th>SerieNummer</th>";
echo "<th>MacAdress</th>";
echo "<th>ProductCode</th>";
echo "<th>Prijs</th>";
echo "<th>RekNummer</th>";
echo "<th>PaletNummer</th>";
echo "<th>Hoeveelheid</th>";
echo "<th>Aantekeningen</th>";
echo "</tr>";
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row);
echo "<tr>";
echo "<td>{$Categorie}</td>";
echo "<td>{$SerieNummer}</td>";
echo "<td>{$MacAdress}</td>";
echo "<td>{$ProductCode}</td>";
echo "<td>{$Prijs}</td>";
echo "<td>{$RekNummer}</td>";
echo "<td>{$PaletNummer}</td>";
echo "<td>{$Hoeveelheid}</td>";
echo "<td>{$Aantekeningen}</td>";
echo "<td>";
echo "<a href='edit.php?id={$id}'>Edit</a>";
echo " / ";
echo "<a href='#' onclick='delete_user( {$id} );'>Delete</a>";
echo "</td>";
echo "</tr>";
}
echo "</table>";//end table
}else{
echo "No records found.";
}
?>
<script type='text/javascript'>
function delete_user( id ){
var answer = confirm('Are you sure?');
if ( answer ){
window.location = 'NinjaCodeDelete.php?action=delete&id=' + id;
}
}
</script>
I also want to say that I am not an advanced programmer I found this code online, where it seemed to be working for the other people who have used it.
I have some experience with Mysql and php but not with PDO.
I hope u can help me!
thank you in advanced.
What is inside your " include'Connect2db3.php'; "?
And with this I'm refering to connect2db3.php and is this file inside the right folder?
Your connection could look like this:
<?php
$config['conn'] = array(
'host' => 'yourHostName',
'username' => 'yourUserName',
'password' => 'yourPassword',
'dbname' => 'yourDBName'
);
$conn = new PDO('mysql:host=' . $config['conn']['host'] . ';dbname=' . $config['conn']['dbname'], $config['conn']['username'], $config['conn']['password']);
?>