Thank you, very nice solution, that was answered my question.
How can I use your solution if my $sql is as follows:
$sql = "SELECT * FROM $tbl_main, $tbl_country, $tbl_members
WHERE ($tbl_main.country_id = $tbl_country.country_id) AND ($tbl_main.member_id = $theselect) ORDER BY $chosenTable.$orderby LIMIT $startpoint, $limit";
i.e. how can I use your solution somewhere in the middle of a code.
You can conditionally concatenate the extra condition:
$sql = "SELECT * FROM $tbl_main, $tbl_country, $tbl_members
WHERE ($tbl_main.country_id = $tbl_country.country_id)";
if(isset($theselect)) {
$sql .= " AND ($tbl_main.member_id = $theselect)";
}
$sql .= " ORDER BY $chosenTable.$orderby LIMIT $startpoint, $limit";
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 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.
What am I doing wrong in my query?
thank you in advance for your help
new - not work
$query = "SELECT * ";
$query .= "FROM photographs ";
$query .= "WHERE `caption` LIKE '%".$query."%' ";
$query .= "OR `caption2` LIKE '%".$query."%' ";
//$query .= "WHERE visible = 1 ";
$query .= "ORDER BY $order_by LIMIT $start, $display ";
$result = mysqli_query ($connection, $query);
old query - work
//$query = ("SELECT * FROM photographs WHERE (`caption` LIKE '%".$query."%') OR (`caption2` LIKE '%".$query."%')");
//$result = mysqli_query($connection, $query);
You are overwriting the $query variable with parts of your query. :-)
LIKE '%".$query."%' ";
should be replaced with
LIKE '%".$yourTerm."%' ";
where $yourTerm is what you are trying to search in your database
my PHP code looks like this right now:
$query = mysql_query("SELECT * FROM `questions` WHERE `id` = '$id' ORDER BY RAND()") or die(mysql_error());
$cmd = mysql_fetch_assoc($query);
$question = $cmd['question'];
Right now, the questions just get randomized - which is fine - but sometimes the same question appears again, and I don't want that. I assume you can fix this with a session. But how? If someone can fix the code, I'd really appreciate.
Perhaps something like this:
$query = "SELECT * FROM `questions` WHERE `id` = `$id` AND `id` NOT IN (";
$query .= implode(', ', array_keys($_SESSION['questions']));
$query .= ') ORDER BY RAND()';
mysql_query($query) or die(mysql_error());
// Here add the returned questions to the $_SESSION['questions'] array so they would not appear again.
I don't know how the rest of the program works, so the logic you need may be a little different, but I'm sure that sort of query is what you're looking for.
This should do what you need:
$query = mysql_query("SELECT * FROM `questions` WHERE `id` = '$id' ORDER BY RAND()") or die(mysql_error());
$question = null;
while ($cmd = mysql_fetch_assoc($query)) {
if (array_search($cmd['question'], $_SESSION['asked']))
continue;
$question = $cmd['question'];
$_SESSION['asked'][] = $question;
break;
}
if (!$question) {
// No unique questions found.
} else {
// $question will be unique here
}
You can store the ID of all previous asked questions inside your session:
if (!isset($_SESSION['QuestionAsked']))
{
$_SESSION['QuestionAsked'] = array();
}
You can then extend your query to exclude all asked questions:
$query = 'SELECT * FROM `questions`';
if ($_SESSION['QuestionAsked'])
{
$askedIds = implode(',', $_SESSION['QuestionAsked']);
$query .= sprintf(' WHERE `id` NOT IN (%s)', $askedIds);
}
$query .= ' ORDER BY RAND()';
And finally after querying a new question, add it to the session:
$_SESSION['QuestionAsked'][] = $currentQuestionId;
I have a query
public static function TestQuery(
$start=0,
$limit=0){
$sql = "
SELECT count(*) AS total
FROM db.table1
JOIN db.table2
ON table1.fieldID = {$fieldID}
AND table2.assigned = 'N'";
$qry = new SQLQuery;
$qry->query($sql);
if($row = $qry->fetchRow()){
$total = intval($row->total);
}
return $total;
}
which works fine but when I add the limit as below, then it doesnt work and gives me errors
public static function TestQuery(
$start=0,
$limit=0){
$sql = "
SELECT count(*) AS total
FROM db.table1
JOIN db.table2
ON table1.fieldID = {$fieldID}
AND table2.assigned = 'N'";
//this fails
if($recordlimit > 0) $sql .= "LIMIT {$startRecord}, {$recordLimit} ";
//
$qry = new SQLQuery;
$qry->query($sql);
if($row = $qry->fetchRow()){
$total = intval($row->total);
}
return $total;
}
Any help will be appreciated
Put a space in front of LIMIT:
" LIMIT {$startRecord}, {$recordLimit} "
without the space you sql will result in a syntax error.
Edit: This is answer is not correct! MySQL will not error without a space before LIMIT (however, earlier versions of phpmyadmin will incorrectly parse such sql).
Your variables are called $limit and $start:
if($limit > 0) $sql .= " LIMIT {$start}, {$limit} ";
Try changing
if($recordlimit > 0) $sql .= "LIMIT {$startRecord}, {$recordLimit} ";
To
if($recordlimit > 0) $sql .= " LIMIT {$start}, {$limit} ";
It looks like your SQL is getting squished together and should be getting a bad syntax error, and you had the wrong (seemingly) variable names in there.
Wrong variables
if($recordlimit > 0) $sql .= "LIMIT {$startRecord}, {$recordLimit} ";
Solved
Thanks