How do I fetch an image from my DB using ID (PDO)? [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed yesterday.
Improve this question
I am trying to fetch a product from my products table using PDO concept. I don't know how to fetch the image based on the ID?
Below is what I have included in my script.
require 'includes/db.con.php';
$id= $_GET['id'];
$query = $conn-> prepare("SELECT * FROM PRODUCTS WHERE id ?");
$query ->execute(array($id));
$data= $query->fetchAll(PDO::FETCH_ASSOC);
?>
<html>
<head lang="en-GB" dir="ltr">
<meta charset="utf-8">
<title>Men's Shoes</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="products.css">
</head>
<body>
<!---section content-->
<section>
<h2>men's shoes</h2>
</section>
<!--- Shoes -->
<main id="maincontent">
<figure>
<img src="img/men_shoes/brown_suede_shoes.jpg" alt="brown suede shoes">
<blockquote><b>£40.00</b></blockquote>
<figcaption>Brown Suede Shoes</figcaption>
</figure>

Related

How to display COUNT() result from mysql to a webpage and automatically update without refreshing webpage? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have this query:
SELECT COUNT(*) FROM my_table;
And I want to display it in my webpage and automatically update it without refreshing the webpage.
HTML Code:
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div class="container">
<h5>/*mysql query result goes here*/</h5>
</div>
</body>
</html>
I also have this count.php:
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "myDB";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT COUNT(*) AS Count FROM my_table";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
echo $row['Count'];
$conn->close();
?>
I don't know how to display the count in my html. I also want to update the display automatically without refreshing the webpage, lets say every 30 seconds.
To do an ajax call to your count.php
$.ajax({
url: "count.php",
cache: false,
success: function(html){
$("#result").html(html);
}
});
Your html file can now contain
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div class="container">
<h5 id="result">/*mysql query result goes here*/</h5>
</div>
</body>
</html>
More information on $.ajax; http://api.jquery.com/jquery.ajax/
you should use post() Methode form jquery with setTimeout().
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div class="container">
<h5 id="count">/*mysql query result goes here*/</h5>
</div>
</body>
<script>
setTimeout(function(){
$.post("Count.php",
{get_count:true},
function(result){
$("#count").html(result);
}
);
},30000);
</script>
</html>
Dont forget to Change the php file. You need to check if POST['get_count'] isst, if you want to handle More then one Post request.

Using PHP to change HTML elements [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a simple HTML page:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<p id="demo">Nothing here</p>
<button onclick="?">Button</button>
</body>
</html>
and a separate PHP file:
<?php
// Load and exec craigslist
$site = curl_init("https://www.craigslist.org/about/sites");
curl_setopt($site, CURLOPT_RETURNTRANSFER, true);
$target = curl_exec($site);
$dom = new DOMDocument();
#$dom -> loadHTML($target);
// Save logo text
$title = $dom -> getElementById('logo') -> nodeValue;
?>
Here's what I want to do:
When I click the button on the HTML page, I want the PHP script to run so that I can get the craigslist title and store it a PHP variable ($title). I want to replace the text in <p id="demo> ("Nothing here") with the text stored in $title ("craigslist").
How do I do this?
Change your html to this
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<p id="demo">Nothing here</p>
<button onclick="myFunc()">Button</button>
<script>
function myFunc() {
var xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("demo").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","file.php",true); //Change file.php to the location of your php file
xmlhttp.send();
}
</script>
</body>
</html>
And add this line to your PHP file after setting the title variable
echo $title;
Now of course best case scenario you would use jQuery's built-in AJAX but this works well enough

Parse error while [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Hello im getting an PHP Error "Parse error: syntax error, unexpected 'endwhile' (T_ENDWHILE) in C:\wamp\www\index.php on line 32" any ideas on what im doing wrong?
<?php
include ('includes/db_connect.php');
$query = $db->prepare("SELECT post_id, title, body FROM posts");
$query->execute();
$query->bind_result($post_id, $title, $body);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=9" />
<!--[if lt IE 9]>
<script src="//html5shim.googlecode.com/svn/trunk/html5.js"></head>script>
<![endif]-->
<script scr="http://code.jquery.com/jquery-1.5.min.js"></script>
</head>
<style>
#container{
margin:auto;
width:800px;
}
</style>
<body>
<div id="container">
<?php
while($query->fetch());
?>
<article>
<h2><?php echo $title ?></h2>
<p><?php echo $body?></p>
</article>
<?php endwhile?>
</div>
</body>
</html>
change: while($query->fetch());
to: while($query->fetch()):
(semi-colon to colon)

PHP short tags not working as expected [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I'm trying to make my php page working, by having html code combined with it. Here is a source code:
<?
$username="username";
$password="password";
$db_name="db_name";
mysql_connect('localhost',$username,$password) or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$userstable='profile';
$query = "SELECT * FROM $userstable order by id";
$result = mysql_query($query) or die(mysql_error());
?>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>DayZSC | Showlist</title>
<meta charset="utf-8" />
<link type="text/css" rel="stylesheet" href="styles.css" />
<link rel="shortcut icon" href="favicon.ico" />
</head>
<body>
<div class="container">
<div id="logo">
<img src="img/logo.png" />
</div>
<?
while ($row=mysql_fetch_array($result)) {
?>
<div class='p_content'>
<h3>
<?
echo ".$row['name'].";
?>
</h3>
<p style="text-align: justify;">
<table align="center"><tbody><tr>
<td>Copies Sold (Week)</td>
<td>11152</td>
</tr>
<tr>
<td>Copies pirated (Week)</td>
<td>124214</td>
</tr>
</tbody></table>
</p>
<h2>Comments</h2><br /><br />
</div>
<?
}
?>
<div id="footer">All rights reserved &copy 2014 | Designed by BerdyevCreations</div>
</div>
</body>
</html>
So whenever I try to run this page, it gives me just a blank. Cannot find error log either. Please help?
I think you are using the PHP short tags wring, try and use this:
<?= $row['name'] ?>
Instead of your
<?
echo ".$row['name'].";
?
I think your problems is with php tags, you are curently using short ones while your php is probably not configured to accept them so change short tags
<?
To long tags
<?php
You need to activate the short code directive in PHP.INI if you have php version 5.3 and earlier
since 5.4 This directive is enabled by default
like this
php.ini
short_open_tag=On

Styling PHP Output with a CSS file [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How do I attach a style sheet to PHP output? Besides bold and italic, it would be nice to give my output some style.
while ($row = sqlsrv_fetch_array ($query))
{
echo '<b>'.'School Name: '.'</b>'.$row['School_Name'].'<br />';
echo '<b>'.'CoSer Code: '.'</b>'.$row['COSER_Code'].'<br />';
echo '<b>'.'Product: '.'</b>'.$row['Product_Name'].'<br />';
echo '<b>'.'Cost Description: '.'</b>'.$row['Cost_Description'].'<br />';
echo '<br />';
}
Simply add <style> style code here </style> or
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
<?php
//your code here
?>
At the top of the PHP page to use CSS.
Use this for your output:
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/CSSfile.css">
</head>
<body>
<?php //Put Here your code
?>
</body>
</html>
Give your output ids and classes which you can than style in your css.
For example you could give your <b> tags another text font with this css code:
b { font-family:Helvetica,sans-serif; }
For more information about changing fonts look here http://de.selfhtml.org/css/formate/zentrale.htm

Categories