<?php
$results = mysql_query("SELECT * FROM ".TBL_SUB_RESULTS."
WHERE user_submitted != '$_SESSION[username]' AND home_user = '$_SESSION[username]' OR away_user =
'$_SESSION[username]' ") ;
$num_rows = mysql_num_rows($results);
if ($num_rows > 0)
{
while( $row = mysql_fetch_assoc($results))
{
extract($row);
$q = mysql_query("SELECT name FROM ".TBL_FRIENDLY." WHERE id = '$ccompid'");
while( $row = mysql_fetch_assoc($q))
{
extract($row);
?>
<table cellspacing="10" style='border: 1px dotted' width="300" bgcolor="#eeeeee">
<tr>
<td><b><? echo $name; ?></b></td>
</tr><tr>
<td width="100"><? echo $home_user; ?></td>
<td width="50"><? echo $home_score; ?></td>
<td>-</td>
<td width="50"><? echo $away_score; ?></td>
<td width="100"><? echo $away_user; ?></td>
</tr><tr>
<td colspan="2">Accept / Decline</td>
</tr></table><br>
<?
}
}
}
else
{
echo "<b>You currently have no results awaiting confirmation</b>";
}
?>
I am trying to run two queries as you can see.
But they aren't both working.
Is there a better way to structure this, I am having a brain freeze!
Thanks
OOOH by the way, my SQL wont stay in this form!
I will protect it afterwards
SELECT *
FROM TBL_SUB_RESULTS ts
LEFT JOIN
TBL_FRIENDLY tf
ON tf.id = ts.ccompid
WHERE ts.user_submitted != '$_SESSION[username]'
AND ts.home_user = '$_SESSION[username]' OR ts.away_user = '$_SESSION[username]'
For debuging purposes, try to print your queries to the sceen. Check if you find any errors in your final query. You might just have misspelled a variable name.
<?php
$query = "SELECT * FROM ".TBL_SUB_RESULTS." WHERE user_submitted != '$_SESSION[username]' AND home_user = '$_SESSION[username]' OR away_user = '$_SESSION[username]'";
echo "query: ".$query;
$results = mysql_query($query);
// rest of code...
I think you need brackets in your WHERE clause:
WHERE user_submitted != '$_SESSION[username]'
AND (home_user = '$_SESSION[username]'
OR away_user = '$_SESSION[username]')
Related
I inserted the data in the table very well and it's showing in phpMyadmin but when i try to display data from the database only one single item is displayed. I need help.
Code and screenshoots below
<?php
$sql = "SELECT * FROM nw";
$result = mysqli_query($conn, $sql);
if($result == TRUE){
$count = mysqli_num_rows($result);
if($count > 0){
while($row = mysqli_fetch_assoc($result)){
$incidentId = $row['id'];
$icTypeName = $row['inctype_name'];
$addedBy = $row['added_by'];
$addedOn = $row['added_on'];
}
?>
<tr class="tableData">
<td><?php echo $incidentId;?></td>
<td><?php echo $icTypeName;?></td>
<td><?php echo $addedBy;?></td>
<td><?php echo $addedOn;?></td>
<td><i class="uil uil-edit icon editIcon"></i></td>
</tr>
<?php
} else {
$_SESSION['noData'] = 'No incidents to show!';
}
} else {
die('Failed to Connect!');
}
?>
Images below
I expect to get all the data in the database tables displayed with the "SELECT * FROM nw"
Simply move the } that ends the while loop to after the code that is using the variables. You are currently consuming all the resultset before outputting anything, so you will only see the last rows data.
<?php
$sql = "SELECT * FROM nw";
$result = mysqli_query($conn, $sql);
if($result == TRUE){
$count = mysqli_num_rows($result);
if($count > 0){
while($row = mysqli_fetch_assoc($result)){
$incidentId = $row['id'];
$icTypeName = $row['inctype_name'];
$addedBy = $row['added_by'];
$addedOn = $row['added_on'];
//} REMOVED
?>
<tr class="tableData">
<td><?php echo $incidentId;?></td>
<td><?php echo $icTypeName;?></td>
<td><?php echo $addedBy;?></td>
<td><?php echo $addedOn;?></td>
<td><i class="uil uil-edit icon editIcon"></i></td>
</tr>
<?php
} // END OF WHILE LOOP
} else {
$_SESSION['noData'] = 'No incidents to show!';
}
} else {
die('Failed to Connect!');
}
?>
I don't know why it doesn't show up anything, I already tested my query and it is working in my phpmyadmin, but in my php code it does not work upon adding the AS keyword. My goal for this is to place a value to a variable coming from the SUM() keyword.
<?php
require_once "user-connect.php";
$user = $_SESSION['id'];
$sql = "SELECT SUM(total) AS sumz FROM cart WHERE userID = $user AND month(orderDate) = month(now()) AND day(orderDate) = day(now())";
$result = $link->query($sql);
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
echo $row['sumz'];
}
if (mysqli_query($link, $sql)) {
} else {
echo "Error: " . $sql . "" . mysqli_error($link);
} ?>
<table cellspacing="0">
<tbody>
<tr class="cart-subtotal">
<th>Cart Subtotal</th>
<td><span class="amount"><?php echo $row['sumz']; ?></span></td>
</tr>
You can use:
$sql = "SELECT SUM(total) FROM cart WHERE..."
And in HTML:
<td><span class="amount"><?php echo $row['SUM(total)']; ?></span></td>
Take a look at PHP fetch assoc guide.
while ($row = $result->fetch_assoc()) {
echo $row['sumz'];
}
An example of getting the SUM value
<?php
$sql="SELECT sum(amount) as total FROM table";
$result = mysqli_query($sql);
while ($row = mysqli_fetch_assoc($result)) {
echo $row['total'];
}
mysqli_close($con);
?>
I'm have a database with users from different countries. My pagination is working because the query counts all rows. So it gives you like 5 pages and let's say the total records are finished by page3.
My question is:
1) How can I count only records from one country let's say "australia"? And the pagination should only give me pages with results no extra pages with no results?
My code for pagination is below:
<?php
$per_page = 1;
$pages_query = mysql_query("SELECT COUNT(*) FROM `users`");
$pages = ceil(mysql_result($pages_query,0)/$per_page);
if (!isset($_GET['page']))
{
header("location: blood.php?page=1");
}
else
{
$page = $_GET['page'];
}
$start = (($page - 1)*$per_page);
$colours = mysql_query("SELECT * FROM users WHERE country ='australia' LIMIT $start,$per_page");
while($row = mysql_fetch_assoc($colours))
{
$username = $row['first_name'];
echo "$username<br>";
}
for($number=1; $number<=$pages; $number++)
{
echo ''.$number.' ';
echo" ";
}
echo "<br>Current Page: $page";
?>
how do i include the above code together with the correction suggested in the below code where results are returned after user searches for a username and lastname.
<?php
include 'core/init.php';
include 'includes/overall/oheader.php';
?>
<?php
if(isset($_POST['submit']))
{
$query = $_POST['uname_search'];
$queryl = $_POST ['lname_search'];
$min_length = 3;
if(strlen($query) >= $min_length && strlen($queryl) >= $min_length)
{
$query = htmlspecialchars($query);
$query = mysql_real_escape_string($query);
$queryl = htmlspecialchars($queryl);
$queryl = mysql_real_escape_string($queryl);
echo "<table border='1' width='250px' align='center' cellpadding='1' cellspacing='1'>";
echo "<tr align='center' bgcolor='#002C40' style='color:#FFF'>
<td height='30px' width='150px'>Username</td> <td>first_name</td> <td>last_name</td><td>email</td><td>Mobile_Number</td><td>gender</td><td>county</td><td>blood_group</td>
$raw_results =
mysql_query("SELECT * FROM `users` WHERE (`username` LIKE '%".$query."%') AND (`last_name` LIKE '%".$queryl."%')");
if(mysql_num_rows($raw_results) > 0)
{
while($results = mysql_fetch_array($raw_results))
{
$image = "images/profile/1a4671c319.jpg" ;
echo "<tr align='center' bgcolor='#002C40' style='color:#FFF'>
<td rowspan='4'>"."<img src=".$results['profile']." width=100px height=100px> "."</td>
<td >Username</td>
<td >first_name</td>
<td>last_name</td>
<td>email</td>
</tr>";
echo "<tr align='center' bgcolor='#0f7ea3'>
<td>".$results['username']."</td>
<td>".$results['first_name']."</td>
<td>".$results['last_name']."</td>
<td>".$results['email']."</td>
</tr>" ;
echo "<tr align='center' bgcolor='#002C40' style='color:#FFF'>
<td>Mobile_Number</td>
<td>gender</td>
<td>county</td>
<td>blood_group</td>
</tr>";
echo "<tr align='center' bgcolor='#0f7ea3'>
<td>".$results['Mobile_Number']."</td>
<td>".$results['gender']."</td>
<td>".$results['county']."</td>
<td>".$results['blood_group']."</td>
</tr>" ;
}
}
else{
echo "<tr align='center' bgcolor='#6C0000'>
<td colspan='8' height='25px'>No results</td><tr>";
echo "</table>";
}
}
else{
echo "Minimum length is ".$min_length;
}
}
include 'includes/overall/ofooter.php';
?>
You could apply the same where clause to the first query which counts the users:
$pages_query =
mysql_query("SELECT COUNT(*) FROM `users` WHERE country ='australia'");
# Here -----------------------------------^
Hello my first question here, I have a bad of trouble getting this code to work or better said how to proceed.
I have 2 tables, table 1 holds a string separated by commas, after exploding the array I want to compare the strings to another table that holds product prices related to the string.
<?php
function packages()
{
global $db;
$query = "SELECT * FROM product_package WHERE package_id > 1; ";
$result = mysqli_query($db, $query) or die('<h3>Query failed</h3>');
$rowcount = mysqli_num_rows($result);
if ($rowcount < 1)
{
return;
}
else
{
while ($row = mysqli_fetch_array($result))
{
$singles = explode(',', $row["product_ids"]);
$query2 = "SELECT product_price FROM products WHERE product_id = $single; ";
$result2 = mysqli_query($db, $query2);
?>
<tr>
<td><?php echo $row["package_id"] ?></td>
<td><?php echo $row["package_name"] ?></td>
<td><?php echo $row["product_ids"] ?></td>
<td>
EDIT
</td>
<td>
<?php
foreach($singles as $single)
{
echo $single . '<br />';
}
?>
</td>
</tr>
<?php
}
}
}
?>
How do I echo the prices that are overlapping with $single?
I am having a problem again with my MySQL query. I am trying to execute my query using my $_GET variable. so here is my code. and many thanks in advance and answer will be very appreciated.
and please dont down vote my question if anyone thinks its not correct, please edit it or tell me to edit it.
many thanks.
here is Code.
$Status = $_GET['status'];
$User = $_GET['user'];
require('./connect.php');
$query = "SELECT * FROM users ORDER BY id ASC";
$result = mysqli_query($con, $query);
$numrows = mysqli_num_rows($result);
if ($numrows > 0) {
echo '<table class="table" border="1">
<tr style="background-color: #0DF; color: #222; font-weight:bold;">
<td>ID:</td>
<td>User Name:</td>
<td>Email:</td>
<td>First Name:</td>
<td>Last Name:</td>
<td>Domain:</td>
<td>Country:</td>
<td>Phone:</td>
<td>Plan:</td>
<td>Duration:</td>
<td>Payable:</td>
<td>Paid Date:</td>
<td>Active Plan:</td>
<td>Active:</td>
<td>Register Date:</td>
</tr>';
while ( $row = mysqli_fetch_assoc($result) ) {
$dbid = $row['id'];
$dbuser = $row['username'];
$dbemail = $row['email'];
$dbfname = $row['first_name'];
$dblname = $row['last_name'];
$dbdomain = $row['domain'];
$dbcountry = $row['country'];
$dbphone = $row['phone'];
$dbplan = $row['plan'];
$dbduration = $row['duration'];
$dbpayable = $row['payable'];
$dbpaid_date = $row['paid_date'];
$dbactive_plan = $row['active_plan'];
$dbactive = $row['active'];
$dbdate = $row['date'];
if ( $dbactive_plan == 0) {
$status = "Activate";
$changeStatus = ''.$status.'';
}
else {
$status = "Deactivate";
$changeStatus = ''.$status.'';
}
echo '
<tr>
<td>'.$dbid.'</td>
<td>'.$dbuser.' Delete</td>
<td>'.$dbemail.'</td>
<td>'.$dbfname.'</td>
<td>'.$dblname.'</td>
<td>'.$dbdomain.'</td>
<td>'.$dbcountry.'</td>
<td>'.$dbphone.'</td>
<td>'.$dbplan.'</td>
<td>'.$dbduration.'</td>
<td>'.$dbpayable.'</td>
<td>'.$dbpaid_date.'</td>
<td>'.$dbactive_plan.' '.$changeStatus.'</td>
<td>'.$dbactive.'</td>
<td>'.$dbdate.'</td>
</tr>';
}//while loop
echo '</table>';
if ($Status == 1 && $User == $dbuser) {
$query = "UPDATE users SET active_plan='$Status' WHERE username='$User'";
mysqli_query($con, $query);
echo "$Status";
}
else if ($Status == 0 && $User == $dbuser) {
$query = "UPDATE users SET active_plan='$Status' WHERE username='$User'";
mysqli_query($con, $query);
echo "$Status";
}
UPDATED
You're defining $dbuser inside the while loop, so it is getting set to the username from the last iteration of the loop. I'm not sure based on your code what $dbuser is supposed to be, judging by your code....