Mysql_query error - php

I have one problem with this mysql_query:
$selectNewsComments = mysql_query("
SELECT user_id, comment, time_add
FROM news_comments
ORDER BY id DESC
LIMIT " .($pageNum - 1) * $numberOfNewsInPage . ", ".$numberOfNewsInPage."
WHERE news_id = '".$nid."'
") or die(mysql_error());
That is error message:
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 news_id = '18'' at line 1
Can someone tell me from whear come this proble?

Your ORDER BY id DESC LIMIT should go after the WHERE
Warning: The question's sample code uses PHP's mysql extension, which
was deprecated in PHP 5.5.0 and removed entirely in PHP 7.0.0.

The WHERE clause should go before ORDER BY.
mysql_query("SELECT user_id, comment, time_add FROM news_comments WHERE news_id = '".$nid."'ORDER BY id DESC LIMIT " .($pageNum - 1) * $numberOfNewsInPage . ", ".$numberOfNewsInPage)

use this instead
$selectNewsComments = mysql_query("
SELECT user_id, comment, time_add
FROM news_comments
LIMIT " .($pageNum - 1) * $numberOfNewsInPage . ", ".$numberOfNewsInPage."
WHERE news_id = '".$nid."'
ORDER BY id DESC
") or die(mysql_error());
what i changed is :
i placed ORDER BY id DESC in the end of the query where is should be so that it works

Related

how to use LIKE clause in mysql when using with php

i'm trying to use like in mysql from php and i write this code
$query = mysqli_query($connection, "SELECT * FROM items order by create_at desc where content LIKE '" . $content . "%'")
or die(mysqli_error($connection));
but it says there is an error in my syntax it says like this
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 'where content LIKE 'c%'' at line 1
Order BY clause always come in the end, use WHERE clause first and then use ORDER BY
Solution would be this :
SELECT * FROM items where content LIKE 'YourVariable%' ORDER BY create_at DESC
Your PHP code should be:
$query = "SELECT * FROM items where content LIKE '" . $content . "%' order by create_at desc";
$result = mysqli_query($connection, $query) or die(mysqli_error($connection));
Query clauses sequence should be :
Select
From
join
where
group by
order by
limit
$query = mysqli_query($connection, "SELECT * FROM items order by create_at desc where content LIKE '%.$content.%'") or die(mysqli_error($connection));

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error

I'm using php and pdo
Here is my code:
try {
$sql="LOCK TABLE appleid WRITE, appleid AS appleid1 READ;";
$stmt = $GLOBALS['$connection']->prepare($sql);
$stmt->execute();
$sql="SELECT MAX(num) FROM appleid;";//to know how many rows it has
$stmt = $GLOBALS['$connection']->prepare($sql);
$stmt->execute();
$result=$stmt->fetch();
$table_top=$result[0];
if (empty($head)) $head = 1;
$check=$table_top - $head;
$check++;//number of available rows that are ready to use
if($check>=$this->apple_id_num)
{
$sql = "SELECT id,pass,en_b_y,en_b_m,en_b_d,sqa1,sqa2,sqa3 FROM
appleid AS appleid1
WHERE num>=$head LIMIT $this->apple_id_num ORDER BY `TimeStamp`
DESC;";
$stmt = $GLOBALS['$connection']->prepare($sql);
$stmt->execute();
$this->pre_head=$head;
$head=1+$head+$this->apple_id_num;
$sql="UNLOCK TABLES;";
$this->num_rows = $stmt->rowCount();
echo $stmt->rowCount();
}
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
I get this error :
SELECT id,pass,en_b_y,en_b_m,en_b_d,sqa1,sqa2,sqa3 FROM appleid AS
appleid1 WHERE num>=1 LIMIT 1 ORDER BY TimeStamp DESC;
SQLSTATE[42000]: Syntax error
or access violation: 1064 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 'ORDER BY TimeStamp DESC' at line 2
I got confused and I don't know what is wrong with this query and how to fix it.
SELECT id,pass,en_b_y,en_b_m,en_b_d,sqa1,sqa2,sqa3 FROM
appleid AS appleid1
WHERE num>=$head ORDER BY `TimeStamp`
DESC LIMIT $this->apple_id_num ;
put your limit after orderby
Use orderby clause before limit clause.
$sql = "SELECT id,pass,en_b_y,en_b_m,en_b_d,sqa1,sqa2,sqa3 FROM
appleid AS appleid1
WHERE num>=$head ORDER BY `TimeStamp` DESC LIMIT $this->apple_id_num;";
Change
"SELECT id,pass,en_b_y,en_b_m,en_b_d,sqa1,sqa2,sqa3 FROM
appleid AS appleid1
WHERE num>=$head LIMIT $this->apple_id_num ORDER BY `TimeStamp`
DESC;";
to
"SELECT id,pass,en_b_y,en_b_m,en_b_d,sqa1,sqa2,sqa3 FROM
appleid AS appleid1
WHERE num>=$head LIMIT {$this->apple_id_num} ORDER BY `TimeStamp`
DESC;";
since the double quotes would not know whether to parse the ->apple_id_num as string or part of a variable.
I have not tested, but timeStamp sound like a reserved word of mysql,
maybe you can try a correct quote of this field, or best pratice change the name of the field.

what is the correct syntax for query

how can I get this syntax work? I got it to work with one of the result. But I want to
get out query from 3. (result1, result2, result3).
$sql = "SELECT modulid=1 FROM oppgave WHERE resultat resultat2 resultat3 is NULL ORDER BY RAND() LIMIT 1";
any tip how to query 3 ?
Try below:
If you want to check that any one should be null of of resultat, resultat2 and resultat3, then use below query:
SELECT moduleid
FROM oppgave
WHERE modulid=1 and (resultat is null or resultat2 is null or resultat3 is NULL)
ORDER BY RAND() LIMIT 1;
If you want to check that all resultat, resultat2 and resultat3 should be null, then use below query:
SELECT moduleid
FROM oppgave
WHERE modulid=1 and (resultat is null and resultat2 is null and resultat3 is NULL)
ORDER BY RAND() LIMIT 1;
Rewrite your query like this..
SELECT *
FROM oppgave
WHERE modulid=1
AND
coalesce(resultat, resultat2, resultat3) IS NULL
ORDER BY RAND() LIMIT 1;
Proceed like this..
$result = mysql_query($sql, $tilkobling);
echo "<br>";
while ($nextrow= mysql_fetch_array($result)){
echo "Besvarelse: <br>" . $nextrow['besvarelse']; "<br>Modulid: " . $nextrow['modulid'];
}
This (mysql_*) extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, Prepared Statements of MySQLi or PDO_MySQL extension should be used to ward off SQL Injection attacks !

MYSQL-counting distinct cell values

I tried to count using following-
include 'db_login';
$sql = "(SELECT * , COUNT(*) FROM `table1`
WHERE `ADVERTISERCATEGORY` LIKE '%something%'
GROUP BY `MANUFACTURER` ORDER BY COUNT DESC )";
$row = mysql_fetch_array($sql);
$total = $row[0];
echo "Total rows: " . $total;
but i got following error message-
Warning: mysql_fetch_array() expects parameter 1 to be resource,
string given in /home/content/43/10130843/html/fashion_test.php on line 169
i am learning php/mysql therefore i need help.
thanks
You haven't called mysql_query(), so you can't fetch the array yet...
$sql = "SELECT * , COUNT(*) FROM `table1`
WHERE `ADVERTISERCATEGORY` LIKE '%something%'
GROUP BY `MANUFACTURER` ORDER BY COUNT(*) DESC ";
$query = mysql_query($sql) or die(mysql_error()); // exit on error
$row = mysql_fetch_array($query);
Note: mysql_query() and the rest of the mysql_* library have been deprecated for some time, you should make the move to paramterized queries with mysqli_* or PDO while you're in the development phase.

PHP - How to Use ORDER BY and GROUP BY together

I created a table that contains message, sender, to, time I want group by sender and order by time this is my code
$query= mysql_query("SELECT * FROM `table` ORDER BY `time` GROUP BY `sender`")or die(mysql_error());
while($arr = mysql_fetch_array($query)){
$num = mysql_num_rows($query);
$msg = $arr ['message'];
echo '</br>';
echo $msg;
}
that shows me 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 'GROUP BYsender' at line 1
So how to fix that problem?
Thanks
Klaus
Note your precedence. After the results have been grouped , it should be ordered.
"SELECT * FROM `table` GROUP BY `sender` ORDER BY `time`"
try this code
$query= mysql_query("SELECT * FROM `table` GROUP BY `sender` ORDER BY `time` ")or die(mysql_error());
// ^^--will be before order
$num = mysql_num_rows($query); // out of the while loop
while($arr = mysql_fetch_array($query)){
$msg = $arr['message'].'<br />';
// ^^--remove space here
echo $msg;
}
how to use self-joins to get the max/min/something-n rows per group.
In your situation, it can be applied to the effect you want like so:
SELECT * FROM
(SELECT group_id, MAX(`yourdate`) AS yourdate FROM tbl_name GROUP BY group_id)
AS x JOIN tbl_name USING (`group_id`,yourdate);

Categories