PHP count from database WHERE - php

I am trying to show a count of all applications stored in a database with the status of 1.
Here is my UPDATED code:
$result=mysql_query("SELECT * FROM member ")or die('You need to add an administrator ' );
$counter = mysql_query("SELECT COUNT(*) as personID FROM member where state='1' ");
$row = mysql_fetch_array($result);
$personID = $row['personID'];
$num = mysql_fetch_array($counter);
$countadmin = $num["personID"];
However this isn't showing anything when I echo `$countadmin'
Can anyone help

You may try this
$query = mysql_query("select count(*) from member where state='1'");
if ($query) {
$count = mysql_result($query, 0, 0);
echo $count;
}
Check mysql_result and also notice the Warning at top. Also make sure that, the field is state not status, it's confusing, you mentioned status in your question but used state in the query.
Also, following line is not required to get the count of your second query
$result=mysql_query("SELECT * FROM member ")or die('You need to add an administrator ' );
Also, make sure that, you have connected to a database and selected it.

You try to readout "ID", but you select COUNT as "personID"

You have two options here;
$result = mysql_query("SELECT * FROM `member` WHERE `Status`='1'");
$num_rows = mysql_num_rows($result);
or
$result = mysql_query("SELECT COUNT(`Status`) FROM `member` WHERE `Status`='1'");
while($row = mysql_fetch_array($result)){
$Count = $row['count(Status)'];
}

Related

Need Some Help Regarding Fetching Data from Mysql using explode function

In the below script I want to fetch data from mysql using a explode function and also a variable within an explode function.
Here's how I want to get
<?php
include ('config.php');
$track = "1,2,3";
$i = 1
$trackcount = explode(",",$track);
$sql = "SELECT * FROM tracks WHERE id='$trackcount['$i']'";
$retval = mysql_query($sql, $conn);
while ($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo "{$row['track_name']}";
}
mysql_free_result($retval);
?>
This is the code
$sql = "SELECT * FROM tracks WHERE id='$trackcount[$i]'";
I want sql to fetch data from tracks table where id = $trackcount[$i]
Whatever the value of $trackcount[$i] mysql should fetch but it shows a blank screen.
If I put this
$sql = "SELECT * FROM tracks WHERE id='$trackcount[1]'";
It works perfectly
save your $trackcount[$i] in one variable and then pass it in the query as given below
<?php
include ('config.php');
$track = "1,2,3";
$i = 1;
$trackcount = explode(",",$track);
$id=$trackcount[$i];
$sql = "SELECT * FROM tracks WHERE id='$id'";
$retval = mysql_query($sql, $conn);
while ($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo "{$row['track_name']}";
}
mysql_free_result($retval);
?>
and one more thing check your previous code with echo of your query and see what is passing ok.
echo $sql = "SELECT * FROM tracks WHERE id='$trackcount['$i']'";//like this
problem is with your query
$sql = "SELECT * FROM tracks WHERE id='$trackcount['$i']'";//change
to
$sql = "SELECT * FROM tracks WHERE id='$trackcount[$i]'";
Generally you would want to use the IN operator with this type of query, so for you this would be:-
$sql="SELECT * FROM `tracks` WHERE `id` in (".$track.");";
or, if the $ids are in an array,
$sql="SELECT * FROM `tracks` WHERE `id` in (".implode( ',', $array ) .");";

related articles on post page

<?php
mysql_select_db($database_XXX, $XXX);
$result= mysql_query("SELECT COUNT(*) FROM news");
$total = mysql_result($result, 0, 0);
// create a random number
mt_srand((double)microtime()*1000000);
$number = mt_rand()%$total;
// get a random entry
$result= mysql_query("SELECT * FROM news LIMIT $number, 5");
$row = mysql_fetch_array($result);
?>
This is the PHP code I'm using and it pulls up random data from the table I want but I can't seem to figure out how to have it not show the current post it's on. Will I need to throw in an if statement in? If I do where would it go and how to impletment it. The only thing I can thing of is using if statements to check if the post_id on page matches the post_id posted. But I'm new to this and the only thing I can think of is.
if(!$row['post_id'] == $_GET['id']) {
}
I don't know what to make it do after. Also if anyone knows how or can help point me in the right direction that would be great. Thanks.
Here is the update of the total thing here. It still shows post it's already on. hope this helps. This is the php code for the page.
<?php
mysql_select_db($database_xxx, $xxx);
$result= mysql_query("SELECT COUNT(*) FROM news");
$total = mysql_result($result, 0, 0);
// create a random number
mt_srand((double)microtime()*1000000);
$number = mt_rand()%$total;
// get a random entry
$result= mysql_query(sprintf("SELECT * FROM news WHERE post_id <> %d LIMIT %d, 3", $post->post_id, $number));
$row = mysql_fetch_array($result);
?>
<?php
if (! isset($_GET['id']) || (int) $_GET['id'] === 0 ) {
echo "Incorrect input, aborting";
exit;
}
mysql_select_db($database_xxx, $xxx);
$sql = "SELECT * FROM news WHERE post_id = " . $_GET['id'];
// a line of debug to make sure things are as expected
$query = MYSQL_QUERY($sql);
// query your table for a match with post_id
if (mysql_num_rows($query) == "1")
// if a record is found, show the info
{
$fetch = mysql_fetch_array($query); // set $fetch to have the values from the table
} else {
echo "No match in database found."; // if no match is found, display this error
}
?>
Assuming that code is on the page where you view the main product:
$result= mysql_query(sprintf("SELECT * FROM news WHERE id <> %d LIMIT %d, 5", $post->id, $number));
Also, you should not be using mysql_* functions anymore as they are deprecated; checkout PDO

simple php error but can't see where i'm going wrong?

I have the code below and I just want to count from the table members how many people have a 1 in the column loggedin and echo that back. I'm sure I'm missing something small, I just can't see it.
<?php
include ('functions.php');
connect();
$result = mysql_query("SELECT * FROM members WHERE loggedin = '1'");
$num_rows = mysql_num_rows($result);
$total_mem = $num_rows + (1223);
return $total_mem;
echo $total_mem;
?>
The echo will never be called because it is after the return statement.
Remove the return statement and the value should be shown.
Why not let your database do the counting for you?
$result = mysql_query("SELECT count('id') as logged_in_count FROM members WHERE loggedin = '1'");
$row = mysql_fetch_assoc($result);
$num_rows = $row['logged_in_count'];
$total_mem = $num_rows + (1223);
echo $total_mem;
return $total_mem;
You're never going to hit that echo statement, because you have a return statement right above it.
Why not use SELECT COUNT(1) FROM members WHERE loggedin = 1, and then pull the value directly from that? You'll save time because it will only need to return 1 row instead of all the rows, when all you want is the count.

PHP fetching data from MySQL database

So I'm trying to fetch data in a many-to-many relationship.
So far I have this, which finds the user:
$user = $_SESSION['user'];
$userID = mysql_query("SELECT * FROM users WHERE user='$user'") or die(mysql_error());
And I know that to echo this information I have to put it in an array like so:
while ($r = mysql_fetch_array($userID)) {
echo $r["0"];
}
This works fine, but when I try to find this variable in another table, I'm not sure what to use as the variable:
$projects = mysql_query("SELECT projects_ID FROM projects_users WHERE users_ID='???'") or die(mysql_error());
I've tried replacing ??? with $userID and $r, but to no avail. I know the code works because it's fine when I put a user ID in manually - where have I gone wrong?
$user = $_SESSION['user'];
$query = mysql_query("SELECT * FROM users WHERE user='".mysql_real_escape_string($user)."' LIMIT 1") or die(mysql_error()); //--note the LIMIT
$result = mysql_fetch_array($query);
$userID = $result[0];
$projects = mysql_query("SELECT projects_ID FROM projects_users WHERE users_ID='$userID'") or die(mysql_error());
Untested, but this should work:
$user = mysql_real_escape_string($_SESSION['user']);
$query = mysql_query("SELECT * FROM users WHERE user='$user'") or die(mysql_error());
$result = mysql_fetch_array($query);
$userID = $result[0];
$projects = mysql_query("SELECT projects_ID FROM projects_users
WHERE users_ID='$userID'") or die(mysql_error());
I your case, you'd need to place $r[0] there.
I think this code is helpful for beginners when you want to get data in array form
we use mysqli instead of mysql to protecting your data from SQL injection.
Before use this code check the database connection first
<?php $tableName='abc';
$qry="select * from $tableName";
$results=mysqli_query($qry);
while($records=mysqli_fetch_array($results))
{
$firstrecord=$records[1];
$secondrecord=$records[2];
}
?>
You can get your projects with one query:
$user = mysql_real_escape_string($_SESSION['user']);
$query = mysql_query("SELECT pu.projects_ID FROM users u
INNER JOIN projects_users pu ON (pu.users_ID = u.users_id)
WHERE u.user='$user'") or die(mysql_error());
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_assoc($result)) {
echo $row['projects_ID'];
}

Add the SUM of a Column in mySQL & PHP

How would i add the sum of a column in mysql?
Here's my code:
$add = mysql_query("SELECT SUM(rsvp) FROM TABLE_NAME WHERE rsvp > 0;")or die(mysql_error());
when i echo it, it gives me a Resource id #
try this instead
$q = mysql_query("SELECT SUM(rsvp) as sum FROM TABLE_NAME WHERE rsvp > 0") or die(mysql_error());
$row = mysql_fetch_assoc($q);
echo $row['sum'];
I recommend looking up more on how to use PHP and MySQL together possibly from one of these sites:
- http://php.net/manual/en/book.mysql.php
- http://www.youtube.com/user/phpacademy - this one is pretty nooby but it does cover everything from pagination to image uploads and beyond. a good place to start I guess.
mysql_query returns a resource, not a value. You need to use another function, such as mysql_fetch_row to access the value it contained:
$result = mysql_query("SELECT SUM(rsvp) FROM TABLE_NAME WHERE rsvp > 0;") or die(mysql_error());
$row = mysql_fetch_row($result); // get an array containing the value of the first row of the above query
$sum = (int) $row[0]; // get an integer containing the value of the first (and, here, only) item in that row
Yes, and you use mysql_fetch_array to fetch a row from that resource.
$resource = mysql_query( ... );
if ($row = mysql_fetch_array($resource))
{
$add = $row[0];
}
I've found the problem,
Note: Undefined index: sum in D:\xampp\htdocs\demo-shop\cart.php on line 9
$sum_query= "SELECT sum(Prod_Tot) as sum FROM cart WHERE Prod_Tot > 0";
$sum_query_res = mysql_query($sum_query);
$row = mysql_fetch_row($sum_query_res);
echo $row['sum'];
If I put echo $row['0']; instead of echo $row['sum']; then it is ok.
ended up figuring it out.
$add = mysql_query("SELECT SUM(rsvp) FROM TABLE_NAME WHERE rsvp >= 1;")or die(mysql_error());
list ( $rsvp_total ) = mysql_fetch_array($add);
echo $rsvp_total;
This is short and simple try it
$sql = mysql_query("SELECT SUM(rsvp) as sum FROM TABLE_NAME WHERE rsvp > 0") or die(mysql_error());
$record = mysql_fetch_assoc($sql);
echo $record['sum'];

Categories