i have a simple query who select me 3 news from table, but i wont to change this number from other file whith variable.
So this is query:
$query = 'SELECT * FROM news ORDER BY created_at DESC LIMIT 3';
I tried differently, but did not work...
Help please
My code(i can't answer on my question so i add it here)
$newsAmount = 3;
function get_news() {
$query = "SELECT * FROM news ORDER BY created_at DESC LIMIT $newsAmount";
$result = mysql_query($query);
$news = array();
while ($row = mysql_fetch_array($result)) {
$news[] = $row;
}
return $news;
if (!$result) {
trigger_error('Invalid query: ' . mysql_error() . " in " . $query);
}
}
This is this code.
Actually you can just do this:
$query = "SELECT * FROM news ORDER BY created_at DESC LIMIT $newsAmount";
But make sure to keep the string in double quotes so the variable can be evaluated, Single quotes will be printed out as it is.
Try to echo $query, you will notice that its being printed.
Try this:
$newsAmount = 3;
$query = 'SELECT * FROM news ORDER BY created_at DESC LIMIT ' + $newsAmount;
you cannot return an array like you did in your code.
I would suggest to do the query inside your main code instead of writing it in a function.
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’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.
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.