How to retrieve image from DB using PHP - php

I'm using the following code retriever image from DB along with other attributes i.e Full Name, Mobile No. etc.But it is showing an empty image box.
require 'database.php';
$MobileNo = null;
if ( !empty($_GET['MobileNo'])) {
$MobileNo = $_REQUEST['MobileNo'];
}
if ( null==$MobileNo ) {
header("Location: index.php");
} else {
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM user where MobileNo = ?";
$q = $pdo->prepare($sql);
$q->execute(array($MobileNo));
$data = $q->fetch(PDO::FETCH_ASSOC);
Database::disconnect();
}
?>
<div class="control-group">
<label class="control-label">Picture</label>
<div class="">
<label class="">
<?php
$row = $data or die("line 44 not working");
$s=$row['Picture'];
echo $row['Picture'];
echo '<img src="'.$s.'" alt="HTML5 Icon"style="width:128px;height:128px">';
?>
</label>
</div>
</div>

You have disconnected the database before fetching $row = $data or die("line 44 not working");
You need to disconnect it after the variable is set.
if ( null==$MobileNo ) {
header("Location: index.php");
} else {
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM user where MobileNo = ?";
$q = $pdo->prepare($sql);
$q->execute(array($MobileNo));
$data = $q->fetch(PDO::FETCH_ASSOC);
$row = $data or die("line 44 not working");
$s=$row['Picture']; //This is where you make the change.
echo $row['Picture'];
Database::disconnect(); //Now disconnect
}
?>
<div class="control-group">
<label class="control-label">Picture</label>
<div class="">
<label class="">
<?php
echo '<img src="'.$s.'" alt="HTML5 Icon"style="width:128px;height:128px">';
?>
</label>
</div>
</div>

Related

How to detect link in text by php and send default meta information

How to detect link in some text which is included from chat database and detect default meta information and put it to text like on discord.
ATTACHMENT
CODE
<?
session_start();
include('../../php/connect.php');
if(isset($_GET['uid']) && isset($_GET['cid']) && isset($_SESSION['user'])) {
$uid = $_GET['uid'];
$user = $_SESSION['user'];
$check = mysqli_query($con, "SELECT * FROM users WHERE id = '$uid'");
$ch = mysqli_fetch_array($check);
if($ch['username'] == $user) {
$cid = $_GET['cid'];
$uq = mysqli_query($con, "SELECT * FROM users WHERE id = '$cid'");
$u = mysqli_fetch_array($uq);
$asd = $u['username'];
$photo = $ch['photo'];
$query = mysqli_query($con, "SELECT * FROM users WHERE username = '$asd'");
$q = mysqli_fetch_array($query);
$row = mysqli_query($con, "SELECT * FROM messages WHERE (user1,user2,type) = ('$user','$asd','message') OR (user2,user1,type) = ('$user','$asd','message') ORDER BY id ASC");
while($result = mysqli_fetch_object($row)) { $date = date_create($result->sent); $time = date_format($date, 'H:i'); $date = date_format($date, 'd.m.Y');
?>
<div class="message" id="message">
<div class="hr-text">
<span>
<? echo $date; ?>
</span>
</div>
<div id="avatar" class="avatar-u"><img src="<? if($result->user1 == $asd) { echo '../../img/avatars/'.$q['photo']; } elseif($result->user1 == $user) { echo '../../img/avatars/'.$photo; } ?>">
</div>
<span class="username-u">
<? echo $result->user1; ?>
</span>
<span class="time">
<? echo $time; ?>
</span>
<div class="message-content">
<? echo '<xmp>'.$result->content.'</xmp>'; ?>
</div>
</div>
<?
}
} else {
header('location: ../index.php');
}
} else {
header('location: ../index.php');
}
?>
AJAX imports this site to #content-frame every 750ms.
I just want for example paste detected link to <a> element and then write some meta information like in ATTACHMENT upper

Php fetching and echoing in HTML

<?php
include_once 'config.php';
$query = $config -> prepare("SELECT `edit`, `user_banned`, `ban_reason`, `ban_time`, `user_banner`, `ban_timestamp` FROM `samp_ban` ORDER BY `edit` ASC LIMIT 10");
if($query -> execute())
{
$query_results = $query->fetchAll();
if($ban_time == 0) { $query_result["ban_time"] = "Permanent"; }
}
?>
code edidetcode edidetcode edidetcode edidet
ERROR: Undefined variable: ban_time
You have to combine and html and php for getting all data from query
if($row_count)
{
while($query_result = $query -> fetch()){
$Username = $query_result['Username'];
$Headshots = $query_result['Headshots'];
$ForumName = $query_result['ForumName'] ;
?>
<div class="row">
<div class="cell" data-title="Full Name">
<?php echo $Username ?>
</div>
<div class="cell" data-title="Headshots">
<?php echo $Headshots ?>
</div>
<div class="cell" data-title="Forum Title">
<?php echo $ForumName ?>
</div>
</div>
<?php
}
}
The issue is that you are trying to output same variables on both rows. As a result you get two rows with same results. You have to store rows from database to array and then make a for loop to output your html with data from that array.
PHP code
<?php
include_once 'config.php';
$query = $config -> prepare("SELECT `Username`, `Headshots`, `ForumName` FROM `users` ORDER BY `Headshots` DESC LIMIT 10");
if($query -> execute())
{
$query_results = $query->fetchAll();
}
?>
HTML code
<div class="limiter">
<div class="container-table100">
<div class="wrap-table100">
<div class="table">
<div class="row header">
<div class="cell">
Nickname
</div>
<div class="cell">
Headshots
</div>
<div class="cell">
Forum Name
</div>
</div>
<?php foreach( $query_results as $query_result ) { ?>
<div class="row">
<div class="cell" data-title="Full Name">
<?php echo $query_result["Username"]; ?>
</div>
<div class="cell" data-title="Headshots">
<?php echo $query_result["Headshots"]; ?>
</div>
<div class="cell" data-title="Forum Title">
<?php echo $query_result["ForumName"]; ?>
</div>
</div>
<?php } ?>
You need to change
<?php
include_once 'config.php';
$query = $config -> prepare("SELECT `Username`, `Headshots`, `ForumName` FROM `users` ORDER BY `Headshots` DESC LIMIT 10");
if($query -> execute())
{
$row_count = $query -> rowCount();
if($row_count)
{
while($query_result = $query -> fetch())
$Username = $query_result['Username'];
$Headshots = $query_result['Headshots'];
$ForumName = $query_result['ForumName'] ;
}
}
?>
into
<?php
include_once 'config.php';
$query = $config -> prepare("SELECT `Username`, `Headshots`, `ForumName` FROM `users` ORDER BY `Headshots` DESC LIMIT 10");
if($query -> execute())
{
$row_count = $query -> rowCount();
if($row_count)
{
while($query_result = $query -> fetch())
{
$Username = $query_result['Username'];
$Headshots = $query_result['Headshots'];
$ForumName = $query_result['ForumName'] ;
echo '<div class="row">';
echo '<div class="cell" data-title="Full Name">';
echo $Username;
echo '</div>';
echo '<div class="cell" data-title="Headshots">';
echo $Headshots;
echo '</div>';
echo '<div class="cell" data-title="Forum Title">';
echo $ForumName;
echo '</div>';
echo '</div>';
}
}
}
?>
You forgot the { } arround the while loop

MySQL Database row in a Button

I have a database for my ToDo App which has following cloumns:
| ID | ShortDescription | Description | Date | Status |
I already can add a Task to the Datatable and can see it in phphmyadmin.
I have following code till now:
$id = mysql_real_escape_string($_GET['id']);
$out = 'SELECT * FROM ToDo1 WHERE `id` = '.$id.' LIMIT 1';
$result = mysqli_query($link, $out);
$row= mysqli_fetch_array($result);
?>
<div id= "OutShortDescription">
<?php
echo $row['ShortDescription'];
?>
</div>
<div id= "OutDescription">
<?php
echo $row['Description'];
?>
</div>
<div id= "OutDate">
<?php
echo $row['Date'];
?>
</div>
<div id= "OutStatus">
<?php
echo $row['Status'];
?>
</div>
Now I want to put every ID row on a own Site.
For that I want to make a table of Buttons (Buttonnumber=ID).
On this Button should only be shown the ShortDescription and when I click it I want to go to a the Site which matches to the Button.
Can someone help me?
EDIT
okay thanks now I have this code but it wont work:
<?php
$dbname= 'Groups';
$dsn = 'mysql:host=localhost;dbname='.$dbname;
$user = 'root';
$pass = '';
$db = new PDO($dsn, $user,$pass);
$query = "SELECT * FROM groups2 WHERE id = :id LIMIT 1";
$ps = $db->prepare($query);
$ps->bindParam(':id', $id);
$ps->execute();
$row = $ps->fetch(PDO::FETCH_ASSOC);
?>
<div class="searchwindow">
<?php
$data = $link->query('SELECT * FROM Groups2');
foreach($data as $row) {
echo '<p><input type="button" onclick="window.location = All_Groups.php?id=' . $row['ID'] . ' value='.$row['ShortDescription'].' /></p>';
}
I have now following code
<div data-role="page" id="SearchPage" data-title="SearchPage">
<div data-role="header">
<h1>Search</h1>
</div>
<div data-role="content">
<div data-role="header">
<form>
<fieldset data-role="controlgroup" data-type="horizontal" data-mini="true" align="center" id="selectMenu">
<select name="selectStatus" id="selectStatus">
<option value="0">Status</option>
<option value="1">Done</option>
<option value="2">In Progress</option>
</select>
</fieldset>
</form>
</div>
<?php
$dbname= 'Groups';
$dsn = 'mysql:host=localhost;dbname='.$dbname;
$user = 'root';
$pass = '';
$db = new PDO($dsn, $user,$pass);
$query = "SELECT * FROM groups2 WHERE id = :id LIMIT 1";
$ps = $db->prepare($query);
$ps->bindParam(':id', $id);
$ps->execute();
$row = $ps->fetch(PDO::FETCH_ASSOC);
?>
<div class="searchwindow">
<?php
$data = $link->query('SELECT * FROM Groups2');
foreach($data as $row) {
$path = $row['ID'];
$description = $row['ShortDescription'];
echo ("<form action='All_Groups.php?id=$path'><button type='submit' value='$description'/>$description</form>" );
}
?>
</div>
</div>
<div data-role="footer" data-position="fixed">
<div data-role="navbar">
<ul>
<li>Search</li>
<li>New</li>
<li>More</li>
</ul>
</div><!-- Ende navbar -->
</div><!-- Ende footer -->
</div>
And this is my All_groups.php
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Unbenanntes Dokument</title>
</head>
<body>
<?php
$servername ="localhost";
$username = "root";
$password = "";
$dbname = "Groups";
$link = mysqli_connect($servername, $username, $password, $dbname);
if (!$link) {
die('Verbindung nicht möglich : ' . mysqli_error($link) );
}
?>
<?php
$row="";
$Date="";
$Status="";
$ShortDescription="";
$Description="";
mysqli_select_db($link, "groups");
?>
</div>
<?php
$id = mysql_real_escape_string($_GET['id']);
$out = "SELECT * FROM groups2 WHERE ID = '$id' ";
$result = mysqli_query($link, $out);
$id = mysqli_fetch_array($result);
?>
<div id= "OutShortDescription">
<?php
echo $id['ShortDescription'];
?>
</div>
<div id= "OutDescription">
<?php
echo $id['Description'];
?>
</div>
<div id= "OutStatus">
<?php
echo $id['Status'];
?>
</div>
<div id= "OutDate">
<?php
echo $id['Date'];
?>
<div data-role="footer" data-position="fixed">
<div data-role="navbar">
<ul>
<li>Search</li>
<li>New</li>
<li>More</li>
</ul>
</div><!-- Ende navbar -->
</div>
</body>
</body>
</html>
First of all, don't use the mysql_* methods! Use PDO or mysqli_* instead.
Below, I'm pasting your example query, using PDO:
$dsn = 'mysql:host=localhost;dbname='.$dbname;//$dbName is the name of your database
$user = 'root';
$pass = '123';//use your login information here
$db = new PDO($dsn, $user,$pass);
$query = "SELECT * FROM ToDo1 WHERE id = :id LIMIT 1";
$ps = $db->prepare($query);
$ps->bindParam(':id', $id)
$ps->execute();
$row = $ps->fetch(PDO::FETCH_ASSOC);
Now, to get your button, you don't need to use jquery:
<?php
$path = $row['ID'];
$description = $row['ShortDescription'];
echo "<form action='your/site/$path'><button type='submit' value='$description'/>$description</form>"
?>
Another option is use the onclick:
<?php
$path = $row['ID'];
$description = $row['ShortDescription'];
echo "<input type=\"button\" onclick=\"location.href='your/site/$path'\" value=\"$description\" />";
?>
The \ before " is a escape, so PHP will print the character " and not interpret it as the end of your string.
Advice: Try to avoid mix HTML and PHP, in general this is a bad practice.

Putting the WHERE statement in SQL STATEMENT

<div class = "col-md-9 text-left">
<?php
$host = 'localhost';
$dbname = 'project';
$username = 'root';
$password = '1234';
$charset = 'utf8';
try
{
$pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$sql = "SELECT subject,description,time,date FROM status";
$q = $pdo->query($sql);
$q->setFetchMode(PDO::FETCH_ASSOC);
$usid = ($row['userID']);
$sql1 = 'SELECT status.subject, status.description, status.time , status.date , status.stno , status.userID , tbl_users.userID , tbl_users.Fname , tbl_users.Lname
FROM status , tbl_users
WHERE status.userID=tbl_users.userID ORDER BY status.time DESC';
$q1 = $pdo->prepare($sql1);
$q1->execute([$usid]);
$q1->setFetchMode(PDO::FETCH_ASSOC);
}
catch (PDOException $e)
{
die("Could not connect to the database $dbname :" . $e->getMessage());
}
?>
<?php while ($row = $q->fetch()): ?>
<?php while ($row1 = $q1->fetch()): ?>
<div class="col-md-9">
<div class="box box-widget">
<div class="box-header with-border">
<div class="user-block">
<img class="img-circle" src="<?php echo $row10['des']; ?><?php echo $row9['userPic']; ?>" alt="User Image">
<span class="username"><?php echo htmlspecialchars($row1['Fname']); ?> <?php echo htmlspecialchars($row1['Lname']); ?></span>
<span class="description">Shared publicly - <?php echo htmlspecialchars($row['time']) ?> <?php echo htmlspecialchars($row['date']) ?></span>
</div>
<!-- /.user-block -->
<div class="box-tools">
<button type="button" class="btn btn-box-tool" data-toggle="tooltip" title="Mark as read">
<i class="fa fa-circle-o"></i>
</button>
<button type="button" class="btn btn-box-tool" data-widget="collapse">
<i class="fa fa-minus"></i>
</button>
<button type="button" class="btn btn-box-tool" data-widget="remove">
<i class="fa fa-times"></i>
</button>
</div>
<!-- /.box-tools -->
</div>
<!-- /.box-header -->
<div class="box-body">
<p><b><?php echo htmlspecialchars($row1['subject']) ?></b></p>
<p><i><?php echo htmlspecialchars($row1['description']) ?></i></p>
<?php
// Check connection
$servername = "localhost";
$username = "root";
$password = "1234";
$dbname = "project";
htmlspecialchars($a = $row1['stno']);
$d1 = $row7['userID'];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM likes WHERE rec = $a";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
// output data of each row
while($row = $result->fetch_assoc())
{
$GLOBALS['a'] = $row['do'];
}
}
$z4 = $GLOBALS['a'];
if ($d1==$z4)
{
include ("unlikee.php");
}
else
{
include ("likee.php");
}
$conn->close();
?>
<span class="pull-right text-muted"><?php
$con=mysqli_connect("localhost","root","1234","project");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
htmlspecialchars($a = $row1['stno']);
$sql="SELECT * FROM likes WHERE rec = $a";
if ($result=mysqli_query($con,$sql))
{
// Return the number of rows in result set
$rowcount=mysqli_num_rows($result);
printf("%d\n",$rowcount);
// Free result set
mysqli_free_result($result);
}
mysqli_close($con);
?>
</h5>
<span class="description-text"><?php
$con=mysqli_connect("localhost","root","1234","project");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
htmlspecialchars($a = $row1['stno']);
$sql="SELECT * FROM likes WHERE rec = $a";
if ($result=mysqli_query($con,$sql))
{
// Return the number of rows in result set
$rowcount=mysqli_num_rows($result);
if ($rowcount==1)
echo 'Like';
else
echo 'Likes';
// Free result set
mysqli_free_result($result);
}
mysqli_close($con);
?> - 3 comments</span>
</div>
<!-- /.box-body -->
<!-- /.box-footer -->
<div class="box-footer">
<form action="#" method="post">
<img class="img-responsive img-circle img-sm" src="../dist/img/user4-128x128.jpg" alt="Alt Text">
<!-- .img-push is used to add margin to elements next to floating images -->
</form>
</div>
<!-- /.box-footer -->
</div>
<!-- /.box -->
</div>
<?php endwhile; ?> <?php endwhile; ?>
</div>
I want to retrieve the Data of only one USER but I don't know how to give a condition for it in SQL Statement. Where and how I can put WHERE userID = $user_Session?
$sql = "SELECT subject,description,time,date FROM status";
In the two code statements above where should I put the first?
$sql1 = 'SELECT status.subject, status.description, status.time , status.date , status.stno , status.userID , tbl_users.userID , tbl_users.Fname , tbl_users.Lname
FROM status , tbl_users
WHERE status.userID=tbl_users.userID ORDER BY status.time DESC';
$sql1 =
'SELECT
status.subject, status.description, status.time , status.date , status.stno , status.userID , tbl_users.userID , tbl_users.Fname , tbl_users.Lname
FROM
status , tbl_users
WHERE
status.userID=tbl_users.userID
AND [correct_table_name].userID = $user_Session # here with AND instead WHERE
ORDER BY
status.time DESC';
Here is the code I added a parameter UID
try
{
$pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$sql = "SELECT subject,description,time,date FROM status";
$q = $pdo->query($sql);
$q->setFetchMode(PDO::FETCH_ASSOC);
$usid = ($row['userID']);
$sql1 = 'SELECT status.subject, status.description, status.time , status.date , status.stno , status.userID , tbl_users.userID , tbl_users.Fname , tbl_users.Lname
FROM status , tbl_users
WHERE status.userID=tbl_users.userID and tbl_users.userID = :UID ORDER BY status.time DESC';
$q1 = $pdo->prepare($sql1);
$q1->bindParam(':UID', $usid, PDO::PARAM_INT); //call with param
$q1->execute();
$q1->setFetchMode(PDO::FETCH_ASSOC);
}

Variable for $_GET array not returning any values

I can't seem to return any values on my $_GET array.
It works fine when e.g.
$sql = "SELECT * FROM review WHERE brand='brandx'" but when I change it to brand='$id' in line 5, nothing gets passed.
The fetch array in my index.php works perfectly fine however when it gets href to brand.php (as shown below), I lose my marbles.
<?php
if(isset($_GET["id"])){
include "php_includes/db_conx.php";
$id = preg_replace('#[^0-9]#i', '', $_GET["id"]);
$sql = "SELECT * FROM review WHERE brand='$id'";
$query = mysqli_query($db_conx, $sql);
$productList = "";
// Now make sure that brand exists in the table
$productCount = mysqli_num_rows($query);// count the output amount
if($productCount > 0){
//get the products off the selected brand
while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$username = $row['username'];
$productname = $row['productname'];
$comment = $row['comment'];
$rating = $row['rating'];
$date = $row['date'];
$productList .=
'
<div class="wrapper">
<div class="brand-and-name">
<div class="brand">
<a href="brand.php?id='.$id.'">
<span>'.$id.'</span>
</a>
</div>
<div class="prod-name">
'.$productname.'
</div>
</div>
<div class="prod-container" id="pd1">
<div class="prod-img"><img src="https://giovanniphotography.files.wordpress.com/2011/09/creativemevid19.jpg" /></div>
<div class="comment">
<b>My Score: '.$rating.'/10</b>
<br /><br />
<p>'.$comment.'</p>
</div>
<div class="profile">
<div class="profile-thumb" id="pt1"></div>
<div class="name" id="nm1">
'.$username.'<br />'.$date.'
</div>
</div>
<div class="social-share-1">
<div class="like-btn"></div>
<div class="comment-btn"></div>
<div class="wishlist-btn">+ wishlist</div>
</div>
</div><!--end .prod-container#pd1-->
</div><!--wrPer-->
';
}
}else{
echo "Product doesnt exist";
exit ();
}
}else{
echo "You got to pick a brand man!";
exit ();
}
?>
Does this code work, after your preg_replace? If not you may not have magic quotes enabled in php.ini. I notice in the rest of your output you are concatenating strings and variables.
print "ID: $id";
use a prepared statement and dont hassle with sanitizing of user input:
if($stmt = $db_conx->prepare("SELECT username, productname, comment, rating, date FROM review WHERE brand=?)")
{
$stmt->bind_value('s', $_GET["id"]);
$result = $stmt-execute();
$stmt->bind_result($username, $productname, $comment, $rating, $date ); //bind result to vars
//now you can loop through your result:
while($stmt->fetch()) {
//use $username, $productname, $comment, $rating, $date etc to work with your values
}
}

Categories