<?
require('../../members/inc/config.php');
require('../../lib/framework.php');
$sql = "SELECT * FROM members ";
$query = $db->prepare( $sql );
$query->execute();
$results = $query->fetchAll( PDO::FETCH_ASSOC );
?>
<table class="table">
<tr>
<th>ID</th>
<th>Username</th>
<th>Email</th>
</tr>
<?php foreach( $results as $row ){
echo "<tr><td>";
echo $row['memberID'];
echo "</td><td>";
echo $row['username'];
echo "</td><td>";
echo $row['email'];
echo "</td>";
echo "</tr>";
}
?>
</table>
I guys, I want get the latest 5 users of my table. I already tried do this way:
$sql = "SELECT * FROM members ORDER BY username DESC LIMIT5";
But.. don't retrieve any results..
The query should be
$sql = "SELECT * FROM members ORDER BY username DESC LIMIT 5";
Whitespace is required between LIMIT and 5
If you want last 5 order should be by id/creation_time
$sql = "SELECT * FROM members ORDER BY id DESC LIMIT 5";
$sql = "SELECT * FROM members ORDER BY creation_time DESC LIMIT 5";
Related
I am using the following to fetch the latest articles from the database:
//Latest Article
$title_query1 = "SELECT title FROM articles ORDER BY id DESC LIMIT 1";
$description_query1 = "SELECT description FROM articles ORDER BY id DESC LIMIT 1";
$content_query1 = "SELECT content FROM articles ORDER BY id DESC LIMIT 1";
$image_query1 = "SELECT image FROM articles ORDER BY id DESC LIMIT 1";
$title_result1 = mysqli_query($con, $title_query1) or die(mysqli_error($con));
$description_result1 = mysqli_query($con, $description_query1) or die(mysqli_error($con));
$content_result1 = mysqli_query($con, $content_query1) or die(mysqli_error($con));
$image_result1 = mysqli_query($con, $image_query1) or die(mysqli_error($con));
//Second Latest Article
$title_query2 = "SELECT title FROM articles ORDER BY id DESC LIMIT 2,1";
$description_query2 = "SELECT description FROM articles ORDER BY id DESC LIMIT 2,1";
$content_query2 = "SELECT content FROM articles ORDER BY id DESC LIMIT 2,1";
$image_query2 = "SELECT image FROM articles ORDER BY id DESC LIMIT 2,1";
$title_result2 = mysqli_query($con, $title_query2) or die(mysqli_error($con));
$description_result2 = mysqli_query($con, $description_query2) or die(mysqli_error($con));
$content_result2 = mysqli_query($con, $content_query2) or die(mysqli_error($con));
$image_result2 = mysqli_query($con, $image_query2) or die(mysqli_error($con));
However, i'm not sure how I can then do something like this:
<h1>Here is the first article: <?php $title_result1 ?><h1>
<h2>Here is the first article description: <?php $description_result1 ?>
<h1>Here is the second article: <?php $title_result2 ?><h1>
<h2>Here is the second article description: <?php $description_result2 ?>
Also, is this method not good? If I am going to do this for 100+ articles, will it cause the web page to load slowly?
Thanks
You do not need to do a single query for each column. You can get all columns for a row by doing select * Also, as mentioned, you can fetch as many rows as you want, and loop through them.
I prefer the while method.. Example, show last 100 articles
// fetch latest 100
$sql = "SELECT * FROM articles ORDER BY id DESC LIMIT 100";
if ($result = mysqli_query($con, $sql)){
// got results, convert result object to array called $row
while ($row = mysqli_fetch_array($result)) {
// echo out $row array elements with
//the column names from the database as array index
echo '<h2>'. $row['title'] .'</h2>'; // example wrap results with HTML
echo '<b>' .$row['description'] .'</b>';
echo $row['content'];
echo <img src="'. $row['image'] .'" title="'.$row['description'].'">';
echo '<br>'; //so next result is on new line
} // end of while loop
} else {
//no result, error
}
How to Count number of rows in a table that matches to the related condition and echo that count out.
Code goes as follows::
<?php
$sql = "SELECT * FROM input ORDER BY date DESC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$myid = $row["id"] ;
$sql3 = "SELECT COUNT question_id FROM output WHERE question_id = $myid";
$result3 = $conn->query($sql3);
?>
<div id="q">
<small><p><?php echo $row["date"]; ?></p></small>
<p id="tag3"><small><?php echo $result3['']; ?></small></p>
</div>
Any Suggestions will be appreciated..
Quotes missing
$sql3 = "SELECT * question_id FROM output WHERE question_id = '".$myid."'";
Secondly i dont see closing braces for while
<?php
$sql = "SELECT * FROM input ORDER BY date DESC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$myid = $row["id"] ;
$sql3 = "SELECT COUNT(*) as rowCount FROM output WHERE question_id = '".$myid."'";
$result3 = $conn->query($sql3);
$rowCount= $result3->fetch_assoc();
?>
<div id="q">
<small><p><?php echo $row["date"]; ?></p></small>
<p id="tag3"><small><?php echo $rowCount['rowCount']; ?></small></p>
</div>
<?php }//while
} //if
?>
You need to use COUNT(question_id) function as below
$sql3 = "SELECT COUNT(question_id ) as TotalQuestions FROM output WHERE question_id = $myid";
$result3 = $conn->query($sql3);
Then to fetch result from above query use fetch_fieled()
while ($info = $result3->fetch_field()) {
$TotalCount = $info->TotalQuestions ;
}
And then display the value
<small><?php echo $TotalCount ; ?></small
try this
$sql3 = "SELECT COUNT(1) as row_count FROM output WHERE question_id = ".$myid."";
$result3 = $conn->query($sql3);
$row_count = $result3->fetch_assoc();
echo $row_count['row_count'];
Hello here is my current code:
$sqlpack = "Select * from package_in_plan where plan_id='$package_id' order by plan_id";
$planres = mysqli_query($conn,$sqlpack);
$plan = array();
while ($row1 = mysqli_fetch_assoc($planres)){
$plan[] = $row1['package_id'];
}
$plan_1 = implode(',', $plan);
for ($x = 0; $x < count($plan); $x++) {
$sql_service = "Select * from service_in_package where package_id='".$plan[$x]."'";
$chid = mysqli_query($conn,$sql_service);
while ($row = mysqli_fetch_array($chid)){
$ch_id = $row['service_id'];
$sql = "Select * from itv where status='1' and id='$ch_id' order by number asc";
$results = mysqli_query($conn,$sql);
while ($row = mysqli_fetch_array($results)){
$nr = $row['id'];
$namn = $row['name'];
$chnr = $row['number'];
echo $nr;
echo $namn;
echo $chnr;
}
}
}
What i need is the output to be sorted by number($chnr), right now my code is not sorting because it's receiving specific id from previous select ($ch_id).
How can i let the output of $results to be sorted "order by number".
Number is INT in itv table.
You could make use of subqueries which results in a single query that can be sorted.
$sql = "Select * from itv where status='1' and id IN
(Select service_id from service_in_package where package_id IN
(Select package_id from package_in_plan where plan_id='$package_id' order by plan_id))
order by number asc";
$results = mysqli_query($conn,$sql);
while ($row = mysqli_fetch_array($results)){
$nr = $row['id'];
$namn = $row['name'];
$chnr = $row['number'];
echo $nr;
echo $namn;
echo $chnr;
}
How to display 10 latest jobs from database like this. Please click here to see image
I have written following code so far not sure what to do next.
$query = "SELECT * FROM jobs ORDER BY posting_date DESC LIMIT 10";
$query = mysqli_query($conn, $query);
while ($row = mysqli_fetch_assoc($query) ) {
$job_title = row["job_title"];
$company_name = $row["company_name"];
$department = $row["department"];
$location = $row["location"];
$job_type = $row["job_type"];
$job_description = $row["job_description"];
$posting_date = date('d-m-y');
}
The $row returns all the columns from database. I want only four job title, company name, location and date
Use column names in select query.
$query = "SELECT job_title,company_name,location,posting_date FROM jobs ORDER BY posting_date DESC LIMIT 10";
$query = mysqli_query($conn, $query);
echo "<table>";
echo "<tr> <th>Job Title</th> <th>Company Name</th> <th>Location</th> <th>Date Posted</th> </tr>";
echo "<tbody>";
while($row = mysqli_fetch_assoc($query) ) {
$job_title = $row["job_title"];
$company_name = $row["company_name"];
$department = $row["department"];
$location = $row["location"];
$posting_date = date('d-m-y', strtotime($row['posting_date']));
echo "<tr>";
echo "<td>".$job_title."</td>";
echo "<td>".$company_name."</td>";
echo "<td>".$location."</td>";
echo "<td>".$posting_date."</td>";
echo "<tr>";
}
echo "</tbody>";
echo "</table>";
Hmm change the query
$query = "SELECT job_title, company_name, location, posting_date FROM jobs ORDER BY posting_date DESC LIMIT 10";
You can order by for rownum
$query = "SELECT job_title, company_name, location, posting_date FROM jobs ORDER BY rownum DESC LIMIT 10";
Here is an example how to build a htrml table with the 4 columns:
$query = "SELECT * FROM jobs ORDER BY posting_date DESC LIMIT 10";
$query = mysqli_query($conn, $query);
echo "<table>";
while ($row = mysqli_fetch_assoc($query) ) {
echo "<tr>";
echo "<td>$row['job_title']</td>";
echo "<td>$row['company_name']</td>";
echo "<td>$row['location']</td>";
echo "<td>$row['posting_date']</td>";
echo "<tr>";
}
echo "<table>";
i am new in php i want to know how to view message
this is my code
$idto = $_GET['id'];
$query2 = mysql_query("select * from users where id =".$idto." ");
$row2 = mysql_fetch_assoc($query2);
echo "<b><font size='6' face='Comic Sans MS'><center>".$row2['firstname']."</center></font></b><br><br>";
$query2 = mysql_query("select * from message where id_from =".$idto." and id_to =". $_SESSION['id']." ORDER BY id");
$query1 = mysql_query("select * from message where id_to =".$idto." and id_from =". $_SESSION['id']." ORDER BY id");
i select message send by user1 & by user 2 but i dont know how to view them correctly
when i use this
while ($row = mysql_fetch_assoc($query2) )
{
echo'<fieldset style="width:250" align="center">';
echo"<legend><b>".$row2['firstname']."</b></legend>";
echo $row['message'];
echo "<br>";
echo $row['date'];
echo"<hr>";
echo"</fieldset>";
}
while ($row1 = mysql_fetch_assoc($query1) )
{
echo'<fieldset style="width:250" align="center">';
echo"<legend><b>".$_SESSION['firstname']."</b></legend>";
echo $row1['message'];
echo "<br>";
echo $row1['date'];
echo"<hr>";
echo"</fieldset>";
}
it view messages but in incorrectly way
can any one help me please thanks :)
if you dont understand what i am saying
i mean i want to know how to view message
Firstly switch from mysql_* to mysqli_* because mysql_* functions are deprecated.
To order your queries in ascending order:
$query1 = mysql_query("
SELECT *
FROM message
WHERE id_to = ".$idto."
AND id_from = ".$_SESSION['id']."
ORDER BY id ASC
");
$query2 = mysql_query("
SELECT *
FROM message
WHERE id_from = ".$idto."
AND id_to = ".$_SESSION['id']."
ORDER BY id ASC
");