Running if database row number is less than 10 - php

I'm trying to make a function work only if the number in the row 'click' in my datbase is less than 10. This is what I have right now: I put the echo in there just to see if the condition is working or not.
<?php
include'connect.php';
$result = mysqli_query($con,"SELECT id, link_name, click FROM clicks");
while($row = mysqli_fetch_array($result))
{
if ($row['click'] < 10) {
echo $row['id'] . " " . $row['link_name']. " " .$row['click'];
echo "<br>";
}
}
mysqli_close($con);
?>

try;
$result = mysqli_query($con,"SELECT id, link_name, click FROM clicks WHERE click<10");
while($row = mysqli_fetch_array($result))
{
echo $row['id'] . " " . $row['link_name']. " " .$row['click'];
echo "<br>";
}
mysqli_close($con);
?>

Why don't you use a where clause in your query?
$result = mysqli_query($con,"SELECT id, link_name, click FROM clicks
where click <10");

Related

php clicking on users name to display profile

So i have this code to display all the users in my database and to access them. That works fine but is there any way to get where it says click here just to display a variable in this case the leader name(aka user name)?
<?php
require_once "config.php";
$sql = "SELECT id , leader_name, nation_name, power FROM nation_info";
$result = $link->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$nationList = [];
$userid = $row['id'];
echo ' click here '; // the click here on this line
// echo '<a class="viewProfile" href="viewnation.php?id=' . $userid . '"><button>View Profile</button></a>'; old method of viewing profile
echo " Nation Name: " . $row["nation_name"]. " Leader Name " . $row["leader_name"]. " Power " . $row["power"];
echo "<br>";
}
} else {
echo "0 results";
}
$link->close();
?>
Are you talking about this? Just printing the name in place of click here?
<?php
require_once "config.php";
$sql = "SELECT id , leader_name, nation_name, power FROM nation_info";
$result = $link->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$nationList = [];
$userid = $row['id'];
echo ' ' . $row["leader_name"] . ' '; // the click here on this line
// echo '<a class="viewProfile" href="viewnation.php?id=' . $userid . '"><button>View Profile</button></a>'; old method of viewing profile
echo " Nation Name: " . $row["nation_name"]. " Leader Name " . $row["leader_name"]. " Power " . $row["power"];
echo "<br>";
}
} else {
echo "0 results";
}
$link->close();
?>

Can not display MySQL data decimal(16,2) in PHP

I have an MySQL data base that has a cell in a record called balance. The db shows it is stored as a decimal(16,2). When I try to display the vale of the record with echo $row["balance"]; In PHP, it display nothing at all. Can you please point me in the right direction. Thanks you.
$sql = "SELECT id, email, username FROM ppb_users WHERE id = '$USERIDX' ";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
//// output data of each row
while($row = $result->fetch_assoc())
{
echo "<br> id: " . $row["id"] . "<br> email: " . $row["email"] . "<br> username: " . $row["username"] . "<br> Ballance: " . $row["balance"] ."<br>";
$UserEmail = $row["email"];
$balancex = $row["balance"];
}
}
else
{
echo " 0 results <br><br>";
}
$conn->close();
Sorry folks, it would seem I had one of those moments in where when it is over and you realize what you did, how small you feel. I did not add balance to the SQL se3lect line. Uggg! Soryy.

Select Data by Id PHP

I have trouble to select a set of specific data using ID from the database. For example, employee one has a unique id of e000000001, when I click the view button in the index will lead to employee detail page which shows the detail of that particular employee instead of all the employees' detail. Thank you.
//from index.php page
<?php
require_once 'db/dbEmpList.php';
$sqlStr = "SELECT * FROM employees;";
$result = $connection->query($sqlStr);
if ($result->num_rows > 0) {
echo "<table class='table table-sm'><thread><tr><th>Full Name</th><th>Employee ID</th><th>Position</th><th>View Employee's Details</th></tr>";
while ($row = $result->fetch_assoc()) {
echo "<tr><td>"
. $row["empName"]. "</td><td>"
. $row["empID"]. "</td><td>"
. $row["position"]. "</td>"
. "<td> <a href='employeedetail.php?id={$row["empID"]}'>View</a>"
. "</td></tr>";
}
}
// from employee page
require_once 'db/dbEmpDetail.php';
$sql = "SELECT * FROM employees where empID = '{$row["empID"]}' ";
$result = mysqli_query($connection, $sql);
if (mysqli_num_rows($result)) {
while ($row = mysqli_fetch_assoc($result)) {
echo '<tr>' .'<td>' .$row["empName"].'</td>'.'<td>'. $row["position"].'</td>' .'<td>'.$row["empNRIC"].'</td>' .'<td>'.$row["empID"].'</td>' .'<td>'.$row["empEmail"].'</td>' .'<td>'.$row["empPwd"].'</td>' . "</tr>";
}
} else {
echo "0 results";
}
mysqli_close($connection);
?>
// FROM EMPLOYEE PAGE
The way you retrieve URL query string is wrong. You should be using $_GET to get the query string from URL. In your case it should be $_GET['id']. See the code below:
require_once 'db/dbEmpDetail.php';
$employeeid = trim(mysqli_real_escape_string($_GET['id']));
$sql = "SELECT * FROM employees where empID = '".$employeeid."' ";
$result = mysqli_query($connection, $sql);
if (mysqli_num_rows($result)) {
while ($row = mysqli_fetch_assoc($result)) {
echo '<tr>' .'<td>' .$row["empName"].'</td>'.'<td>'. $row["position"].'</td>' .'<td>'.$row["empNRIC"].'</td>' .'<td>'.$row["empID"].'</td>' .'<td>'.$row["empEmail"].'</td>' .'<td>'.$row["empPwd"].'</td>' . "</tr>";
}
}
else {
echo "0 results";
}
mysqli_close($connection);
?>

Query gives a blank page

I want to select name, phone from approval(tableB) using condition in users(tableA), I got the below code
$email = 'email';
$sql = "SELECT phone, name FROM approval RIGHT JOIN users ON users.email=email.$assigned";
$result = $conn->query($sql);
if ($result) { // output data of each row
while($row = $result->fetch_assoc()) {
echo "<center><table class='table table-bordered'><thead><tbody><tr><td>Name</td><td>" . $row["name"]. " </td></tr><tr><td>Phone</td><td> " . $row["phone"]. " </td></tr><tr><td></td><td> ".file_get_contents("jquery.php")."</td></tr></thead></tbody‌​> </center>";
}
echo "</table>";
} else {
echo "<center>!Sorry you have not been paired.</center>";
}
}
$conn->close();
?>
on running it, it gives me a blank page.

How to retrieve items from a db and then display one row of data on a different page on button click using php and mysql?

I am having a huge problem. I have displayed data on a single events page the events being displayed are the rows of data in the database.
Here's the code.
<?php
$sql = "SELECT * FROM events ORDER BY `startdate` DESC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<div class='event col-sm-3'>
<h1>". $row['eventname'] . "</h1>
<p><b>Regular:</b> " . $row['regular'] . "Ksh</p>
<p><b>VIP:</b> " . $row['vip'] . "Ksh</p><br>
<p><b>" . $row['startdate'] . "</b></p>
<p><b>Tickets remaining:<b>" . $row['tickets'] . "</p><br><br>
<button class='button'>book</button>
</div>";
}
}
?>
Here are the events being displayed
Now I would want when I click the button that says book the event the fields(items) in the specific row that I have clicked(using the book button) be displayed in a different page and get stored in variables.
If I understand what you are trying to do, you might want to use an 'a' tag and not a button.
You should pass the 'id' value of the ticket to the page where you want to have an independent query (individual_ticket.php)
Your code should look like this
<?php
$sql = "SELECT * FROM events ORDER BY `startdate` DESC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<div class='event col-sm-3'>
<h1>". $row['eventname'] . "</h1>
<p><b>Regular:</b> " . $row['regular'] . "Ksh</p>
<p><b>VIP:</b> " . $row['vip'] . "Ksh</p><br>
<p><b>" . $row['startdate'] . "</b></p>
<p><b>Tickets remaining:<b>" . $row['tickets'] . "</p><br><br>
Book
</div>";
}
}
?>
And inside the 'individual_ticket.php'.
Do something like.
<?php
if(isset($_GET['id']))
{
$id = filter_var($_GET['id'], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH);
$sql = "SELECT * FROM events WHERE id = '$id'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
//you can start assigning variables here
}
}
}
?>

Categories