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>";
?>
Related
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);
Basically I have 4 fields in a form. I want to the user search for books in a library by either title, or by author or both. I also want the user to set the length of the list of items and from the starting point, these are not restrictions though the user does not have to specify.
Here is the code:
require_once __DIR__.'/config.php';
session_start();
$dbh = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_USERNAME, DB_USERNAME, DB_PASSWORD);
$title = $_GET["title"];
$authors = $_GET["authors"];
$st = $_GET["start"];
$ln = $_GET["length"];
$stmt = $dbh->prepare("SELECT title, authors, description, price FROM books WHERE title = :title LIMIT :length OFFSET :start");
$stmt->execute(array(':title' => $title,':start' => $st,':length' => $ln));
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$title = $row['title'];
$authors = $row['authors'];
$description = $row['description'];
$price = $row['price'];
}
echo "<table>";
echo "<tr>";
echo "<td>Title</td>";
echo "<td>$title</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Authors</td>";
echo "<td>$authors</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Description</td>";
echo "<td>$description</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Price</td>";
echo "<td>$price</td>";
echo "</tr>";
echo "</table>";
So far it literally just returns me what I have typed in the input - so nothing much at all! Does anyone know how I can do this?
Adapt your code this way:
echo "<table>";
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$title = $row['title'];
$authors = $row['authors'];
$description = $row['description'];
$price = $row['price'];
echo "<tr>";
echo "<td>Title</td>";
echo "<td>$title</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Authors</td>";
echo "<td>$authors</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Description</td>";
echo "<td>$description</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Price</td>";
echo "<td>$price</td>";
echo "</tr>";
}
echo "</table>";
to print all the rows returned by the query. Your code was printing just the last row.
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;
Problem is while loop always showing last inserted row result. I've tried this below code to follow/unfollow button option. For example I'm a user id=1. I have already followed user id=4. Now, I want to follow user id=5. When i click follow button(id=5) it turns into Unfollow properly. But, I have already followed user id=4. That turns into Follow. This is my problem.
Then I tried echo $following;. it Prints 5555. That means last inserted data. But I want 45. I'm sure I've made a mistake in my while loop. But I don't know what I should change?
<?php
try
{
$stmt = $conn->prepare("SELECT * FROM users ORDER BY Autoid");
$stmt->errorInfo();
$stmt->execute();
$sth = $conn->prepare("SELECT * FROM followers ORDER BY Autoid");
$sth->errorInfo();
$sth->execute();
while($follow_row = $sth->fetch(PDO::FETCH_ASSOC))
{
$following = $follow_row['Following'];
$follower = $follow_row['Follower'];
}
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
echo "<tr>";
echo "<td>". $row['Autoid'] ."</td>";
echo "<td>". $row['Name'] ."</td>";
// echo $row['Following'];
if($_SESSION['sesuname'] == $row['Username'])
{
echo "<td class='itsyou' >Its You ". $_SESSION['sesuname'] ."</td>";
}
else
{
if(($follower == $_SESSION['sesid']) AND ($following != $row['Autoid']))
{
//echo "<td>true</td>";
echo $following;
echo "<td>";
echo "<form id='jsform' method='post' action='subscribe.php'>";
echo "<input type='hidden' name='id' value=" . $row['Autoid'] . " />";
echo "<button class='follow' >Follow</button>";
echo "</form>";
echo "</td>";
}
else
{
//echo "<td>false</td>";
echo "<td>";
echo "<form id='ufform' method='post' action='unsubscribe.php'>";
echo "<input type='hidden' name='uid' value=" . $row['Autoid'] . " />";
echo "<button class='follow' >UnFollow</button>";
echo "</form>";
echo "</td>";
}
}
echo "</tr>";
}
} catch (PDOException $e) {
'Database Error : ' .$e->getMessage();
}
?>
This code:
while($follow_row = $sth->fetch(PDO::FETCH_ASSOC))
{
$following = $follow_row['Following'];
$follower = $follow_row['Follower'];
}
Simply OVERWRITES $following and $follower every time you fetch a row, leaving you with the LAST row fetched in the variables. Perhaps you want something more like
$following[] = $follow_row['Following'];
$follower[] = $follow_row['Follower'];
^^--- append new row value to an array.
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']);
?>