I’m using this code:
$date = date('Y-m-d');
$selStat = "SELECT obqva_id, COUNT(*) as broqch FROM statist WHERE date='$date' GROUP BY obqva_id ORDER BY COUNT(*) DESC LIMIT 8 ";
$queryStat = mysqli_query($conn, $selStat);
while ($rowStat = mysqli_fetch_array($queryStat)) {
$count = $rowStat['broqch'];
$id = $rowStat['obqva_id'];
echo $id.' - '.$count.'<br>';
$selBest = "SELECT * FROM view WHERE id='$id' GROUP BY $count ";
$queryBest = mysqli_query($conn, $selBest);
**$rowView = mysqli_fetch_array($queryBest);** this problem !
$selImage = "SELECT * FROM upload WHERE obqva_id='$id'";
$queryImage = mysqli_query($conn, $selImage);
$rowImage = mysqli_fetch_array($queryImage);
?>
Which produces this output:
First and next result has a problem, three and next have no problem... why?
First number 41 is ID next number total view.
it seems that the query is not valid, or there was no result.
thats why you get a bool instead of a resource.
you can filter that by putting your mysqli_query function in an IF statement, like this:
$selBest = "SELECT * FROM view WHERE id='$id' GROUP BY $count ";
if($queryBest = mysqli_query($conn, $selBest)){
$rowView = mysqli_fetch_array($queryBest);
}
else{
$rowView = false;
}
That's that, now for the query. You are trying to group the result on a number:
SELECT * FROM view WHERE id='$id' GROUP BY $count
Not sure why you want to group, as it seems that you only want to get some info on a specific id. In that case, i would get rid of the GROUP BY statement.
Related
I want to order data by Id, how can i do this ?
if($_GET["grupid"]>0){
$DUZEN = array();
$sql = "SELECT * FROM siparis_ana WHERE grupid =".$_GET["grupid"];
$rsDuzen = mysql_query($sql, $conn) or die(mysql_error());
while ($r = mysql_fetch_assoc($rsDuzen)) {
$DUZEN[] = $r;
}
}
i can read all data with this code which have same group id. But data aline random.
You have to use mysql order clause in your query like order by id asc. Which you can use at the end of your query.
$sql = "SELECT * FROM siparis_ana WHERE grupid =".$_GET["grupid"]." order by id asc";
Your sql query should be like given below...
$sql = "SELECT * FROM siparis_ana where grupid = " . $_GET['grupid'] . " ORDER BY id asc ";
I have record of 1000 and more records. I have to provide only 50 records per request like
0-50, 50-100, 100-150 like this. I'm using the following code:
public function get_database($data)
<?php
{
$start = $data['start'];
$limit = $data['limit'];
$alumni_details = array();
$query1 = "select * from alumni where
status='Active',limit '".$start."','".$limit."' ";
$query_run = mysql_query($query1);
while($row = mysql_fetch_assoc($query_run))
{
$row['date_of_birth'] = date('d M, Y', strtotime($row['date_of_birth']));
$alumni_detail['alumni_details'] = $row;
$alumni_details[] = $alumni_detail;
}
echo json_encode($alumni_details);
}
But I need to take only user_id based on that I need to encode data in json dynamically with limit.
Your code as below:
$start = $data['start'];
$query = SELECT * FROM `alumni` WHERE `status` = 'Active' ORDER BY `alumni_id` LIMIT $start,5"
It should be dynamic 5 would not be taken as constant. take it in some variable,
and yes it should be fixed, but as per requirement in future it can be change so take it in variable.
$start=$data['start'];
$query=select * from alumni where status='Active' order by alumni_id Limit $start,5"
where 5 is the limit set it according to requirement
$start=$data['start'];
$query=select * from alumni where status='Active' order by alumni_id Limit $start,5"
You have syntax error in your query string.
Use concatenation for example:
$query1 = "select * from alumni where status='Active' limit ".$start.",".$limit;
Or just put variables into double quoted string:
$query1 = "select * from alumni where status='Active' limit $start, $limit";
There is a typo also: you should use $start variable in the query, not $star
I am using LIKE to do my searching, i try it in phpMyAdmin and return the result but when i use it in php it return empty result.
$search = "ip";
$start = 0;
$query = "SELECT * FROM product WHERE product_name LIKE '%$search%' LIMIT $start,30";
$result = mysql_query($query);
if(empty($result))
$nrows = 0;
else
$nrows = mysql_num_rows($result);
It will return result when i using phpMyAdmin to run this query but when i use it in php, it return empty.
Update:
Sorry guys,
I just found out the problem is i didn't connect database as well. anyway, thanks for helping.
Try This
$query = "SELECT * FROM `product` WHERE `product_name` LIKE '%".$search."%' LIMIT 0, 30";
And if the sole purpose of your code is to get the number of products with the searched-for name, use SELECT COUNT(*) instead of doing a mysql_num_rows() on all your data. It will decrease your querytime and the amount of data that is (unnecessarily) fetched.
I am not sure why this is not working, as the query seems to be correct to me. I would like to suggest you writing query this way
$query = <<<SQL
SELECT * FROM product WHERE product_name LIKE "%$search%" LIMIT $start,30
SQL;
please note that there should not be any space or any character after SQL;
$query = "SELECT * FROM product WHERE product_name LIKE '%" . $search . "%' LIMIT " . (int) $start. ",30";
you can use directly mysql_num_rows()
but here is right code
$query = "SELECT * FROM product WHERE product_name LIKE '%".$search."%' LIMIT $start,30";
$search = "ip";
$start = '0';
$query = "SELECT * FROM product WHERE product_name LIKE '%".$search."%' LIMIT $start,30";
$result = mysql_query($query)or die(mysql_error());
if(mysql_num_rows($result) == 0){
$nrows = 0;
} else{
$nrows = mysql_num_rows($result);
}
//use mysql_num_rows($result) instead of empty($result) because in this situation $result is every time not empty so use inbuilt PHP function mysql_num_rows($result);
I have the following code:
$query = mysql_query("SELECT * FROM activity ORDER BY activity_time DESC LIMIT 50");
while($result = mysql_fetch_array($query)) {
extract($result);
if ($activity_type == "discussion") {
$query = mysql_query("SELECT * FROM discussions WHERE discussion_uuid = '$activity_ref'");
while($result = mysql_fetch_array($query)) {
extract($result);
echo $discussion_user . " said:<br>" . $discussion_text . "<br>";
}
} elseif ($activity_type == "file") {
}
}
But it just returns the last row. My goal is to have a chronological list of "activities" each displayed slightly differently depending on their type.
Your using $query and $result twice so the second loop is overwriting the result of the first and stopping...
$query = mysql_query("SELECT * FROM activity ORDER BY activity_time DESC LIMIT 50");
and
$query = mysql_query("SELECT * FROM discussions WHERE discussion_uuid = '$activity_ref'");
same with $results var...
I would suggest you change to $query and $query2 but best to use something like
$activies = mysql_query("SELECT * FROM activity ORDER BY activity_time DESC LIMIT 50");
while($activity = mysql_fetch_array($activies)) {
and
$discussions = mysql_query("SELECT * FROM discussions WHERE discussion_uuid = '$activity_ref'");
while($discussion = mysql_fetch_array($discussions)) {
I would also avoid using extract - as you might be overwriting vars your not expecting to...
You have to create another connection to the database so that you can run them at the same time.
OR
You can load the results of the first one into an array, and then just loop through the array.
Suppose I have a while loop like:
$sql = mysql_query("SELECT * FROM tablename");
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$sql_2 = mysql_query("SELECT * FROM secondtable WHERE id != $id ");
while($ro = mysql_fetch_array($sql_2)){
$id2 = $ro["id2"];
echo $id2;
}
}
then if first query return 5 results i.e 1-5 and second query returns 3 results than if i want to echo out second query it gives me like this..........
111112222233333
than how can i fix to 123 so that the second while loop should execute according to number of times allowed by me........!! how can i do that.........!!
I'm not sure I 100% understand your question - it's a little unclear.
It's possible you could solve this in the query with a GROUP BY clause
$sql_2 = mysql_query("SELECT id FROM secondtable WHERE id != $id GROUP BY id");
But that would only work if you need just secondtable.id and not any of the other columns.
When you say "number of time allowed by me" do you mean some sort of arbitrary value? If so, then you need to use a different loop mechanism, such as Greg B's solution.
Do you want to explicitly limit the number of iterations of the inner loop?
Have you considered using a for loop?
$sql = mysql_query("SELECT * FROM tablename");
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$sql_2 = mysql_query("SELECT * FROM secondtable WHERE id != $id ");
for($i=0; $i<3; $i++){
$ro = mysql_fetch_array($sql_2);
$id2 = $ro["id2"];
echo $id2;
}
}
Your first while loop is iterating over all 5 results, one at a time.
Your second while loop is iterating over each of the 5 results, producing it's own set of results (i.e. 3 results for each of the 5 iterations, totaling 15 results).
I believe what you are trying to do is exclude all IDs found in your first loop from your second query. You could do that as follows:
$sql = mysql_query("SELECT * FROM tablename");
$exclude = array();
while($row = mysql_fetch_array($sql)) {
array_push($exclude, $row['id']);
}
// simplify query if no results found
$where = '';
if (!empty($exclude)) {
$where = sprintf(' WHERE id NOT IN (%s)', implode(',', $exclude));
}
$sql = sprintf('SELECT * FROM secondtable%s', $where);
while($row = mysql_fetch_array($sql_2)) {
$id2 = $row["id2"];
echo $id2;
}
$sql = mysql_query("SELECT * FROM tablename");
$tmp = array();
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
if(!in_array($id, $tmp)) {
$sql_2 = mysql_query("SELECT * FROM secondtable WHERE id != $id ");
while($ro = mysql_fetch_array($sql_2)){
$id2 = $ro["id2"];
echo $id2;
}
$tmp[] = $id;
}
}
Saving all queried $id's in an array to check on the next iteration if it has already been queried. I also think that GROUPing the first query result would be a better way.
I agree with Leonardo Herrera that it's really not clear what you're trying to ask here. It would help if you could rewrite your question. It sounds a bit like you're trying to query one table and not include id's found in another table. You might try something like:
SELECT * FROM secondtable t2
WHERE NOT EXISTS (SELECT 1 FROM tablename t1 WHERE t1.id = t2.id);