A
A
Armand M2015-10-16 13:26:55
PHP
Armand M, 2015-10-16 13:26:55

How to fix error in php?

When fetching records from the database, it gives the following 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 'desc,date,author, mini_img ,view FROM data WHERE cat='1'' at line 1

<?php 
    
echo $myrow["text"];
    
$result = mysql_query("SELECT id,title,desc,date,author,mini_img,view FROM data WHERE cat='$cat'", $db);

if (!$result)
{
echo "<p>Запрос на выборку данных из базы не прошел. Напишите об этом администратору [email protected] <br>Код ошибки:</p>";
exit(mysql_error());
}

if (mysql_num_rows($result) > 0)
{
  $myrow = mysql_fetch_array($result);

  do
  {
  printf ("<table border='0'>
            			<tr>
            				<td>
                <p><a href='view_post.php?id=%s'>%s</a></p>
                <p>Дата добавления: %s</p>
                <p>Автор: %s</p>
              </td>
            			</tr>
            
            			<tr>
              				<td>%s<br>Просмотров: %s</td>
            			</tr>
          			</table><br><br>", $myrow['id'], $myrow['title'], $myrow['date'], $myrow['author'], $myrow['desc'], $myrow['view']);
  }
  while ($myrow = mysql_fetch_array($result));
}

else
{
  echo "<p>Информация по запросу не может быть извлечена. В таблице нет записей</p>";	
  exit();
}
    
?>

Answer the question

In order to leave comments, you need to log in

5 answer(s)
M
Max, 2015-10-16
@muaythai075

on desc you swear. Best of all, change it to descr (both in the table and in the query), so as not to bother with escaping. Well, the advice is to throw out the book and stop using mysql_ - 100% bull's-eye.

M
Melkij, 2015-10-16
@melkij

https://dev.mysql.com/doc/refman/5.6/en/keywords.html

R
Ruslan Fedoseev, 2015-10-16
@martin74ua

do not use function words as the names of tables, fields ..
in this case desc

M
Memorivardo, 2015-10-16
@Memorivardo

Such errors are most often associated with incorrect request formation. Specifically, in your case, the error is most likely caused by the fact that the query contains the words "desc", "date" and "view", which are keywords for MySQL. You can escape them using forward quotation marks: `desc`, `date`, `view`, etc.
But it’s better to just look at some PDO right away, there questions about shielding have been thought out for you and there will be a little less problems with injections in the future. Everything seems to be well written here: phpfaq.ru/pdo

6
65536, 2015-10-16
@65536

Write about it to the administrator [email protected]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question