query to display four random data from database with out repeating - php

i'm trying to display quotes but i can't seem to get the data to show on the index page i'm still new to php and mysql and its been a ruff road
html index page
<div class="quote">
<div class="container">
<blockquote class="blockquote">
<p class="mb-0">"<?php echo $row['feedback']; ?>"</p>
<footer class="blockquote-footer"><?php echo $row['companyname']; ?></footer>
</blockquote>
<blockquote class="blockquote-reverse">
<p class="mb-0">"<?php echo $row['feedback']; ?>"</p>
<footer class="blockquote-footer"><?php echo $row['companyname']; ?></footer>
</blockquote>
<blockquote class="blockquote">
<p class="mb-0">"<?php echo $row['feedback']; ?>"</p>
<footer class="blockquote-footer"><?php echo $row['companyname']; ?></footer>
</blockquote>
</div>
</div>
and database connect
<?php
$hostname = "";
$username = "";
$password = "";
$db = "";
$dbconnect=mysqli_connect($hostname,$username,$password,$db);
if ($dbconnect->connect_error) {
die("Database connection failed: " . $dbconnect->connect_error);
}
$query="SELECT companyname,feedback,status from review WHERE status=? ORDER BY RAND() LIMIT 3";
?>

<?php
$hostname = "";
$username = "";
$password = "";
$db = "";
$dbconnect=mysqli_connect($hostname,$username,$password,$db);
if ($dbconnect->connect_error) {
die("Database connection failed: " . $dbconnect->connect_error);
}
$query=mysqli_query($dbconnect,"SELECT companyname,feedback,status from review WHERE status=? ORDER BY RAND() LIMIT 3");
$row = mysqli_fetch_assoc($query);
?>

Related

displaying results from a mysql database

Working on a project to display a list of cars
$username="username";
$password="password";
$database="listofcars";
$mysqli = new mysqli("localhost", $username, $password, $database);
#mysql_select_db($database) or die( "Unable to select database");
$query2="SELECT * FROM cars";
$result=$mysqli->query($query2);
$num=$mysqli->mysqli_num_rows($result);
$mysqli->close();
echo "
<div class="item">
<div class="container">
<div class="imgcontainer"><img alt="Cars for sale" src="$carimage" width="380" height="380" /></div>
<div class="details">
<a href="$internallink" target="_blank">
<h3 class="title"> $carname <br />
<span> $cartype </span></h3>
<p>
$cardesc
</p>
<div class="button"><span data-hover="Order Car">Order Car</span></div>
</a>
</div>
</div>
</div>
I need it to loop the results
database name: listofcars
tablename: cars
Field names
carsid
carname
carimage
cartype
internallink
cardesc
Try this to see if it works.
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "listofcars";
// Create connection
$mysqli = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$mysqli) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM cars";
$result = mysqli_query($mysqli, $sql);
?>
<?php while($row = mysqli_fetch_assoc($result)) { ?>
<div class="item">
<div class="container">
<div class="imgcontainer">
<img alt="Cars for sale" src="<?php echo $row['carimage']; ?>" width="380" height="380">
</div>
<div class="details">
<a href="<?php echo $row['internallink']; ?>" target="_blank">
<h3 class="title"><?php echo $row['carname']; ?><br><span><?php echo $row['cartype']; ?></span></h3>
<p><?php echo $row['cardesc']; ?></p>
<div class="button">
<span data-hover="Order Car">Order Car</span>
</div>
</a>
</div>
</div>
</div>
<?php } ?>
mysqli_close($mysqli);
You need to leave the connection open until you are done and loop through it after running the query.
Something like
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "listofcars";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
//Query
$sql = "SELECT * FROM cars";
$result = mysqli_query ($conn, $sql);
while($row = mysqli_fetch_array($result)) {
//Whatever you want to do for each row...
echo "
<div class='item'>
<div class='container'>
<div class='imgcontainer'><img alt='Cars for sale' src='".$row[carimage]."' width='380' height='380' /></div>
<div class='details'>
<a href='".$row[internallink]."' target='_blank'>
<h3 class='title'> ".$row[carname]." <br />
<span> ".$row[cartype]." </span></h3>
<p>
".$row[cardesc]."
</p>
<div class='button'><span data-hover='Order Car'>Order Car</span></div>
</a>
</div>
</div>
</div>";
}
$conn->close();

Display the value on to the label below the applicant pending

I have to pull out the value from the database table call count.php which I already got the value 49. The problem is how to insert into the html
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "hopeplace";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT COUNT(*) AS TOTAL_APPLICANT FROM applicant";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while ($row = mysqli_fetch_assoc($result)) {
echo $row["TOTAL_APPLICANT"];
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
code for the container for the applicant pending
<div class="content">
<div class="row">
<div class="col-xs-5">
<div class="icon-big icon-danger text-center">
<i class="ti-user"></i>
</div>
</div>
<div class="col-xs-7">
<div class="numbers">
<p>Applicant Pending</p>
<p>*this is the place where value need to be put*</p>
</div>
</div>
</div>
<div class="footer">
<hr />
</div>
</div>
You can assign value to one variable and echo that variable inside html
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "hopeplace";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$count = 0;
$sql = "SELECT COUNT(*) AS TOTAL_APPLICANT FROM applicant";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$count = $row["TOTAL_APPLICANT"] ;
}
}
mysqli_close($conn);
?>
code for the container for the applicant pending will be like
<div class="content">
<div class="row">
<div class="col-xs-5">
<div class="icon-big icon-danger text-center">
<i class="ti-user"></i>
</div>
</div>
<div class="col-xs-7">
<div class="numbers">
<p>Applicant Pending</p>
<p>
<?php echo $count; ?>
</p>
</div>
</div>
</div>
<div class="footer">
<hr />
</div>
</div>
save both html & php in same file or ensure that your html code is saved as a .php file. So, your script will be - <p><?php echo $total; ?></p> or <p><?= $total ?></p>

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);
}

Show all fields based on ID

I am building a blog and trying to show all comments that apply to the post.
Each post has an ID and each comment is stored with the post ID.
here is my code:
<?php
$con = mysql_connect("localhost","cl49-XXX-b","X");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("cl49-XXX-b", $con)or die( "Unable to select database line 873");
$result=mysql_query("SELECT * FROM blogcomments WHERE ID='".$ID."'") or die('Error on Line 215 :'.mysql_error());
echo " <ul class='comments'> "; // first row beginning
for ($i = 1; $i <= mysql_num_rows($result); $i++)
{
$row = mysql_fetch_array($result);
$name = $row['name'];
$email = $row['email'];
$comment = $row['comment'];
$created=$row['created'];
echo" <li>
<div class='comment'>
<div class='thumbnail'>
<img class='avatar' alt='' src='img/avatar.jpg'>
</div>
<div class='comment-block'>
<div class='comment-arrow'></div>
<span class='comment-by'>
<strong>$name</strong>
<span class='pull-right'>
<span> <a href='#'><i class='icon-reply'></i> Reply</a></span>
</span>
</span>
<p>$comment</p>
<span class='date pull-right'>$created</span>
</div>
</div> ";
echo " </li>"; // it's time no move to next row
}
?>
When I run this code the page only shows one comment, although my DB has 3 comments with the correct ID.
I would consider using mysqli_ as mysql_ has been depreciated. I'd also consider using a while loop, hopefully this will help:
<?php
$DBServer = 'localhost';
$DBUser = 'xxxx';
$DBPass = 'xxxx';
$DBName = 'xxxx';
$mysqli = new mysqli($DBServer, $DBUser, $DBPass, $DBName);
if ($mysqli->connect_errno) {
echo "Failed to connect to the database: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
$query = "SELECT * FROM blogcomments WHERE ID='". $ID ."'";
echo " <ul class='comments'> ";
if ($result = $mysqli->query($query)) {
while ($row = $result->fetch_assoc()) {
?>
<li>
<div class='comment'>
<div class='thumbnail'>
<img class='avatar' alt='' src='img/avatar.jpg'>
</div>
<div class='comment-block'>
<div class='comment-arrow'></div>
<span class='comment-by'>
<strong><?php echo $row['name']; ?></strong>
<span class='pull-right'>
<span><a href='#'><i class='icon-reply'></i> Reply</a></span>
</span>
</span>
<p><?php echo $row['comment']; ?></p>
<span class='date pull-right'><?php echo $row['created']; ?></span>
</div>
</div>
</div>
</li>
<?php
} $result->close();
} $mysqli ->close();
echo "</ul>";
?>
I haven't tested this for bugs, but let me know if you like further information.
When you do this :
mysql_query("SELECT * FROM blogcomments WHERE ID='".$ID."'")
You select just one comment in the database.
Maybe you have to do that :
mysql_query("SELECT * FROM blogcomments WHERE post_ID='".$post_ID."'")
Because you are selecting the row where ID field value is $ID in the table blogcomments.
I guess you store the referred post_id to whom the comment is connected in a field called something like "post_id". You better point your select query to that field ;)
If you put the structure of the table in the question, I might help :)
You're not closing your <ul>. Might it just be a HTML formatting issue?

Categories