Cant identify Error - php

I have some code that is supposed to show certain things depending if a radio button is pressed.
When no is selected, it hides entries in a table with a status_id of 15.
When yes is selected, it shows entries in a table with a status_id of 15
if($_REQUEST['statusVal']=="") {
$condition1= " AND MO.status_id <>'15'";
}
if($_REQUEST['statusVal']=="archive") {
$condition1 =" ";
} if($_REQUEST['statusVal']=="all") {
$condition1= " AND MO.status_id <>'15'";
}
$mode = $_REQUEST['mode'];
$mode_toggle = $_REQUEST['mode_toggle'];
$sql="SELECT MO.ssrs_id,MO.member_id,MO.assr_user_id,MO.product_id,MO.component_id,MO.status_id,MO.summary,MO.priority,V.product_name,M.component,T.name,S.statusTitle,S.flag_id FROM ".ASSR_SSRS." MO,".PRODUCTS." V,".PROD_COMPONENTS." M,".ASSR_USER." T ,".ASSR_STATUS." S WHERE MO.product_id=V.product_id AND MO.assr_user_id=T.assr_user_id AND MO.component_id=M.component_id AND MO.status_id=S.status_id AND MO.member_id='$memberID' ORDER BY MO.lastupdate DESC".$condition1;
$row_count = getRowCount($sql);
$sql .= $GLOBALS[sql_page];
$result=mysql_query($sql) or die(mysql_error());
I get this error when it is shown on the web browser (using latest Chrome)
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND MO.status_id <>'15' LIMIT 0,10' at line 1
Please help me

This is your query
$sql="
SELECT
MO.ssrs_id,
MO.member_id,
MO.assr_user_id,
MO.product_id,
MO.component_id,
MO.status_id,
MO.summary,
MO.priority,
V.product_name,
M.component,
T.name,
S.statusTitle,
S.flag_id
FROM ".ASSR_SSRS." MO,".PRODUCTS." V,".PROD_COMPONENTS." M,".ASSR_USER." T ,".ASSR_STATUS." S
WHERE
MO.product_id=V.product_id
AND MO.assr_user_id=T.assr_user_id
AND MO.component_id=M.component_id
AND MO.status_id=S.status_id
AND MO.member_id='$memberID'
ORDER BY MO.lastupdate DESC".$condition1;
Here you are appending the dynamic condition at the end and this makes the query invalid when you have some conditions for $condition1 something as
order by MO.lastupdate DESC AND MO.status_id <>'15' and hence the syntax is wrong
It should be as
$sql="
SELECT
MO.ssrs_id,
MO.member_id,
MO.assr_user_id,
MO.product_id,
MO.component_id,
MO.status_id,
MO.summary,
MO.priority,
V.product_name,
M.component,
T.name,
S.statusTitle,
S.flag_id
FROM ".ASSR_SSRS." MO,".PRODUCTS." V,".PROD_COMPONENTS." M,".ASSR_USER." T ,".ASSR_STATUS." S
WHERE
MO.product_id=V.product_id
AND MO.assr_user_id=T.assr_user_id
AND MO.component_id=M.component_id
AND MO.status_id=S.status_id
AND MO.member_id='$memberID'
".$condition1."
ORDER BY MO.lastupdate DESC";

You are adding condition after ORDER BY clause, which is error.
Following is the corrected SQL:
$sql="SELECT MO.ssrs_id,MO.member_id,MO.assr_user_id,MO.product_id,MO.component_id,
MO.status_id,MO.summary,MO.priority,V.product_name,M.component,T.name,
S.statusTitle,S.flag_id FROM ".ASSR_SSRS." MO,".PRODUCTS."
V,".PROD_COMPONENTS." M,".ASSR_USER." T ,".ASSR_STATUS." S WHERE
MO.product_id=V.product_id AND MO.assr_user_id=T.assr_user_id AND
MO.component_id=M.component_id AND MO.status_id=S.status_id AND
MO.member_id='$memberID' " . $condition1 . "ORDER BY MO.lastupdate DESC";

Related

Server-side processing Datatables pagination Microsoft SQL

Im trying to setup pagination with datatables using PHP PDO with MS SQL, since "limit" is not applicable i find it really hard to make the code work.
I have tried the "TOP" syntax but it will only filter the specified number and the pagination wont work.
I have tried offset and fetch still not working.
These is the working code when Mysql is used and its so easy to understand and perform.
if($_POST["length"] != -1)
{
$query .= 'LIMIT ' . $_POST['start'] . ', ' . $_POST['length'];
}
These are the codes i tried (sorry im not really great in coding) :
//if($_POST["length"] != -1)
{
Trial 1 : //$query .= "TOP " . $_POST['start'] . " OFFSET " . $_POST['length'];
Trial 2 : //$query .= "SELECT * from item ORDER BY id DESC offset 0 rows fetch next 10 rows only ";
Trial 3 ://$query .="AND id BETWEEN ".intval( $_POST["start"] )." AND ".intval( $_POST["length"] );" "
}
The result should be a pagination with 10 filtered records each.enter image description here
Update 1:
Here is the screenshot of the query i tried to test it in a MS SQL server but getting an error (using MS SQL 2008)
SQL Query
Your approach depends on SQL Server version.
Approach, based on ORDER BY clause with OFFSET and FETCH as a paging solution requires SQL Server 2012+. Your syntax seems correct, so next code should work:
<?php
if ($_POST["length"] != -1) {
$query = "
SELECT *
FROM item
ORDER BY id DESC OFFSET ".($_POST['start']-1)." ROWS FETCH NEXT ".$_POST["length"]." ROWS ONLY
";
}
?>
For SQL Server 2008+, you may use ROW_NUMBER() as a paging solution:
<?php
if ($_POST["length"] != -1) {
$query =
"SELECT *
FROM (
SELECT
*,
ROW_NUMBER() OVER (ORDER BY id DESC) AS Rn
FROM item
)
WHERE Rn BETWEEN ".$_POST['start']." AND ".($_POST['start'] + $_POST['length'] - 1);
}
?>
In MSSQL to use the limit you will need to write your query like this:
ORDER BY X.Field
OFFSET 20 ROWS
FETCH NEXT 10 ROW ONLY OPTION (RECOMPILE)
And this will skip the first 20 records and fetch the next 10.

Select from table where clause from array from another table

I'm trying to insert a failsafe into a code to prevent them from going a step further, and I've been scratching my head for a while now. I am able to put something into an array, but I am not able to get the items from the array to match the second select query. I only get Array instead of the value from the item.
My first select query is this:
$datohenter3 = "select DATE_FORMAT(datotid, '%Y.%m.%d') AS dato from gramorapport34 group by dato order by dato asc";
$hentdatoer = $db->query($datohenter3);
$periodedatoer = array();
for ($x = 1; $x <= $db->affected_rows; $x++) {
$periodedatoer[] = $hentdatoer->fetch_assoc();
}
Then I want to match the values from this array with my next select query:
$rapportdatoer = "select fradato, tildato from gramorapportlogg WHERE fradato IN('".$periodedatoer."') OR tildato IN('".$periodedatoer."')";
$rapportdatoeksist = $db->query($rapportdatoer);
if ( !$rapporteksist ) die('Database Error: '.$db->error);
while($row = mysqli_fetch_array($rapportdatoeksist))
{
print_r($row);
}
The errors I am getting are:
Notice: Array to string conversion for the second select
Database Error: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ' fradato IN('Array') OR tildato IN('Array')' at line 1
I'm not an expert in JOIN SELECT queries. This is using MariaDB 10.3.12 with PHP7.2
var_dump available at: https://www.lokalradio.no/rapport/gramo/datohenttest.php
Notice that $periodedatoer is an array of array. Each element inside has 1 key of dato (as you var_dump displays).
So use array-column to get the values and then implode as:
$rapportdato = implode("','", array_column($periodedatoer, "dato"));
Now you can use $rapportdato in your second query as:
$rapportdatoer = "select fradato, tildato from gramorapportlogg WHERE fradato IN('" . $rapportdato . "') OR tildato IN('" . $rapportdato . "')";

Query 2 tables join

I want to get all the films whose genre is equal to ID_GENRE = 8. This is the query that I'm doing:
http://l4c.me/fullsize/2-tablas-1434140362.png
$query_GetSimilar = sprintf("SELECT * FROM z_movie,z_movie_genre ORDER BY z_movie.visits DESC WHERE z_movie_genre.id_genre = 8 LIMIT 18");
$GetSimilar = mysql_query($query_GetSimilar, conect::dbconect()) or die(mysql_error());
$row_GetSimilar = mysql_fetch_assoc($GetSimilar);
$totalRows_GetSimilar = mysql_num_rows($GetSimilar);
But I jump the next error
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE z_movie_genre.id_genre = 8 LIMIT 18' at line 1
Note:
WHERE should be first before ORDER BY
You can use INNER JOIN to get two connecting tables, assuming that they have connecting id/column.
For example, both z_movie and z_movie_genre table has the column id_genre to connect each other, you can try this:
$query_GetSimilar = sprintf("SELECT * FROM z_movie
INNER JOIN z_movie_genre ON z_movie.id_genre = z_movie_genre.id_genre
WHERE z_movie_genre.id_genre = 8
ORDER BY z_movie.visits DESC
LIMIT 18");
Make a simple search, and see if you can put 'order by' before 'where'.
ok, now my query is:
$query_GetSimilar = sprintf("SELECT * FROM z_movie,z_movie_genre WHERE z_movie_genre.id_genre=8 ORDER BY z_movie.visits DESC LIMIT 18");
$GetSimilar = mysql_query($query_GetSimilar, conect::dbconect()) or die(mysql_error());
$row_GetSimilar = mysql_fetch_assoc($GetSimilar);
$totalRows_GetSimilar = mysql_num_rows($GetSimilar);
but I do not have the expected results

Query error MySQL php

Im new to php and my sql in trying to get all the results from this table if nothing is selected but for some reason its always displaying one result. Any ide why
$query = "SELECT *, ROUND(AVG(d.rating),0) FROM restaurant AS r, review AS d WHERE 1=1 ";
if($vicinity) $query .= "AND r.vicinity=\"$vicinity\" ";
if($cuisine) $query .= "AND r.cuisine=\"$cuisine\" ";
if($price) $query .= "AND r.price=\"$price\"";
if($name) $query .= "AND r.name LIKE \"%$name%\"";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
and im only getting the first item on the table
I would look into PDO personally. You can find out a lot about it in the manual here.
guessing you're only getting one result because the avg call without a group by is triggering some interesting behavior. try adding a group by, and i'm guessing you also want to associate the restaurants and reviews with a join. eg:
$query = "SELECT *, ROUND(AVG(d.rating),0) FROM restaurant AS r LEFT JOIN review AS d on r.id = d.restaurant_id WHERE 1=1";
...
...
$query .= ' GROUP BY r.id';
$result = mysql_query($query);
per the thread, sounds like you should look into prepared statements as well :). and the SELECT * should probably also just be SELECT r.* - the data returned as part of the * results from the rating won't be meaningful after the group by (the r.* and round(avg(d.rating),0) values should be though)
Try to use prepared statements and get results and use iterator to parse and print in a loop will get. See also this tutorial.

PHP mySQL, Joining More than Two Tables Together with Similar IDs?

Here is what I am working with:
$query = "SELECT Products.Title, Product_Lines.pl_Title, Manufacturers.man_Title".
"FROM Products, Product_Lines, Manufacturers ".
"WHERE Products.pl_ID = Product_Lines.pl_ID AND Product_Lines.man_ID = Manufacturers.man_ID";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo $row['Title']. " - ". $row['pl_Title']. " - ". $row['man_Title'];
echo "<br />";
}
I am getting this error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server
version for the right syntax to use near 'WHERE Products.pl_ID = Product_Lines.pl_ID AND
Product_Lines.man_ID = Manufactur' at line 1
I am unfamiliar with this method and this error
SELECT Products.Title, Product_Lines.pl_Title, Manufacturers.man_Title
FROM Products INNER JOIN Product_Lines ON Products.pl_ID = Product_Lines.pl_ID INNER JOIN Manufacturers ON Product_Lines.man_ID = Manufacturers.man_ID
WILL DO
I don't see white space before your FROM clause. This is a possible cause for the error. Try:
$query = "SELECT Products.Title, Product_Lines.pl_Title, Manufacturers.man_Title".
" FROM Products, Product_Lines, Manufacturers ".
"WHERE Products.pl_ID = Product_Lines.pl_ID AND Product_Lines.man_ID = Manufacturers.man_ID";
Need To follow two things
Foriegn Key
Join
Search Join i.e [left join right join...] In Inter u ll get anwser for ur question/..

Categories