i have this code
<?php
$votes_query=mysql_query("select * from votes where CandidateID='$id'");
$vote_count=mysql_num_rows($votes_query);
echo $vote_count;
?>
which fetches individual *ID*s from my votes table.
I need a code which can help fetch the sum of specific *ID*s.
Lets say, I have ID-205 and ID-209 in my table.
<?php
$ids = array('205','209');
$votes_query=mysql_query("select * from votes where CandidateID IN($ids) ");
$vote_count=mysql_num_rows($votes_query);
echo $vote_count;
?>
Put all ID's in an array & Use IN Clause of mysql.
<?php
$ids = array('205','209');
$sql "SELECT SUM(field_score) AS vote_score FROM votes WHERE CandidateID IN ($ids)";
$vote_score = mysql_query($sql);
echo $vote_score;
?>
Related
I've tried to echo number of rows where a certain id=$id. This is essentially to count the number of "votes" a person has been receiving on the website. It works like a charm in mysqlworkbench, however the number of rows where this person's id has been inserted into database (through the voting button) won't show up on the webpage. The table name is forslag_stemmer, and it has its primary key= id, and foreign keys brukerid (the user that votes for a certain person) and foreign key forslagid (this is the people who receives votes from the users).
This query works in workbench, but not on the page:
echo "<u><b>Number of votes</u></b>:";
$sql= "SELECT COUNT( * ) FROM
bb.forslag_stemmer WHERE forslagid=$forslagid";
$resultat = $kobling->query($sql);
while($rad=$resultat->fetch_assoc())
{
$forslagid = $rad["forslagid"];
echo $sql;
echo "$resultat->num_rows";
}
I really don't know what to do?
You select one field COUNT( * ) as result of your query. There will be no other fields in a result.
echo "<u><b>Number of votes</u></b>:";
// I added an alias for field
$sql= "SELECT COUNT( * ) as votes_count FROM bb.forslag_stemmer WHERE forslagid=$forslagid";
$resultat = $kobling->query($sql);
$rad = $resultat->fetch_assoc();
// access value by alias
echo $rad['votes_count'];
mysqli_result::num_rows Gets the number of rows in a result
In your Example since you're querying as "select count(*) from table" it always returns Number of rows as "1"
You can get Number of rows in two ways
Method 1:
$sql= "SELECT * from FROM bb.forslag_stemmer WHERE forslagid=$forslagid";
$result = $con->query($sql);
echo $result->num_rows; // prints number of rows found
Method 2:
$sql= "SELECT count(*) as resultcount from FROM bb.forslag_stemmer WHERE
forslagid=$forslagid";
$result = $con->query($sql);
while($row = $result->fetch_assoc()){
echo $row['resultcount']; // prints number of rows found
}
Thanks for helping, first I will show code:
$dotaz = "Select * from customers JOIN contracts where customers.user_id ='".$_SESSION['user_id']."' and contracts.customer_contract = ".$_SESSION['user_id']." order by COUNT(contracts.customer_contract) DESC limit $limit, $pocetZaznamu ";
I need to get the lists of users (customers table) ordered by count of contracts(contracts table)
I tried to solve this by searching over there, but I can't... if you help me please and explain how it works, thank you! :) $pocetZanamu is Number of records.
I need get users (name, surname etc...) from table customers, ordered by number of contracts in contracts table, where is contract_id, customer_contract (user id)..
This should do it where is the column name you are counting.
$id = $_SESSION['user_id'] ;
$dotaz = "Select COUNT(`customer_contract`) AS CNT, `customer_contract` FROM `contracts` WHERE `user_id`=$id GROUP BY `customer_contract` ORDER BY `CNT` DESC";
Depending on what you are doing you may want to store the results in an array, then process each element in the array separately.
while ($row = mysqli_fetch_array($results, MYSQL_NUM)){
$contracts[$row[1]] = $row[0];
}
foreach ($contracts AS $customer_contract => $count){
Process each user id code here
}
Not sure what you are counting. The above counts the customer_contract for a table with multiple records containing the same value in the customer_contract column.
If you just want the total number of records with the same user_id then you'd use:
$dotaz = "Select 1 FROM `contracts` WHERE `user_id`=$id";
$results = $mysqli->query($dotaz);
$count = mysql_num_rows($results);
This code pulls together a list of members then lists all the items they have added in a category and then outputs all this information to a table.
Here's the 3 table layouts:
Members
member_id - fname - lname - etc.
Items
member_id - item_id - etc.
Categories
name - etc.
And the current code which takes FOREVER to load:
<?php $sql="SELECT * FROM members ORDER BY lname, fname ASC";
$result=mysql_query($sql);
while($rows=mysql_fetch_array($result)){ ?>
<? echo('Name'); ?>
<? echo $rows[fname];?> <? echo $rows[lname];?>
<? $sql2="SELECT * FROM leadtypes ORDER BY name ASC";
$result2=mysql_query($sql2);
while($rows2=mysql_fetch_array($result2)){ ?>
<? echo $rows2[name];?>
<? echo(': '); ?>
<? $sql4="SELECT lead_id FROM leads WHERE member_id='$rows[member_id]' AND type='$rows2[name]' ORDER BY name ASC";
$result4=mysql_query($sql4);
$num4 = mysql_num_rows($result4); ?>
<? echo $num4;?>
<? } ?>
<? } ?>`
I know I shouldn't query * but this is a very stripped down piece of code on a test server. I would be grateful If somebody knows a good way of combining all this together to speed up the system.
Try this
$sql = SELECT A.lead_id,B.name,C.fname,C.lname
FROM leads A
JOIN leadtypes B ON A.type = B.name
JOIN members C ON A.member_id = C.member_id
You can echo this as
$rs = mysql_query($sql);
while($rows = mysql_fetch_array($rs)){
echo $row['name'];
echo $row['fname'];
}
Have a look at these links
http://www.w3schools.com/sql/sql_join.asp
http://dev.mysql.com/doc/refman/5.0/en/join.html
Try doing one join instead of multiple queries. For example,
SELECT lt.*, l.lead_id FROM leadtypes lt, leads l WHERE l.member_id=lt.member_id AND l.type=lt.name ORDER BY l.name ASC
If that one query is still slow then it means you need to add indices on your tables.
I am trying to order data from the table 'tech_inquiry' by the Field 'number' in descending order. This should put the results in order by year. Each 'number' field corresponds to another field with a title/date (what is actually viewed by visitors) which I can't sort by because the date is at the end of the title and not always in the same place.
Also, the table 'tech_inquiry_allowed' determines what is viewable to who when logged in.
With that said, here is the code:
<?
$query2=mysql_query("SELECT * FROM tech_inquiry_allowed where term_code = '$term_code' ");
while($row2=mysql_fetch_assoc($query2))
{
$id2=$row2['id'];
$query3=mysql_query("SELECT * FROM tech_inquiry WHERE id= '$id2' ORDER BY number DESC");
$row3=mysql_fetch_assoc($query3);
$name3=$row3['name'];
?>
<hr />
<li><? echo $name3; ?> </li>
<?
}
?>
Also, I have another 'admin' section that is able to order data correctly. The only difference is there is no conditional 'where' clause because no user authentication is needed (it is used to add data to the table).
Here is that code:
<?
$query2= mysql_query("SELECT * from tech_inquiry ORDER BY number DESC");
while($row2=mysql_fetch_assoc($query2))
{
$id2=$row2['id'];
$name2=$row2['name'];
?>
<hr />
<li><? echo $name2; ?> </li>
<?
I am wondering if it might be the fact that we are running a query inside a loop.
There are a number of problems here. First of all you only need one query to accomplish this. Please read up on SQL query writing when you get a moment. You will find it to be VERY helpful.
Second, you are using way more code than you need to. Below is the much simplified, cleaner, and probably faster code.
<?php
$query = mysql_query("SELECT * FROM tech_inquiry ti WHERE id IN (SELECT id FROM tech_inquiry_allowed where term_code = '$term_code') ORDER BY ti.number");
while ($row = mysql_fetch_object($query)) {
echo "<hr />\n<li><a href='get_file.php?id={$row->id}'>{$row->name}</a></li>";
}
?>
You are not looping the inner query. Anyway, you should be using a single query for this:
SELECT allowed.*, inquiry.*
FROM tech_inquiry_allowed allowed
INNER JOIN tech_inquiry inquiry
ON inquiry.id = allowed.id
WHERE term_code = '$term_code'
ORDER BY inquiry.number DESC
I need to echo text from a specific ID in my table..
If i use
<?php
database_connect();
$navquery = "SELECT * from content
WHERE id = 1
ORDER by position;";
$navresult = mysql_query($navquery);
?>
it will echo the text in ID 1..
but when i try
<?php
database_connect();
$navquery = "SELECT * from content
WHERE id = 2
ORDER by position;";
$navresult = mysql_query($navquery);
?>
it wont echo the text in ID 2..
help me please?
You do not have a row on your content table with an id of 2. You can try the query below to get another row.
SELECT *
FROM content
WHERE ID<>1
LIMIT 1