Data on the condition is not displayed. No conditions are displayed. Here is the database and table.

-- создание базы в том случае, если ее нет CREATE DATABASE IF NOT EXISTS abc; -- сделать базу текущей USE abc; -- создание таблицы в том случае, если ее нет CREATE TABLE IF NOT EXISTS t1( id SERIAL, start VARCHAR(50) comment 'начало акции', end VARCHAR(50) comment 'конец акции', name VARCHAR(250) NOT NULL comment 'название товара или услуги', price DECIMAL(15, 2) NOT NULL comment 'обычная цена', price_action DECIMAL(15, 2) NOT NULL comment ' акционная цена', discount INT comment 'скидка', PRIMARY KEY(id) ); 

Here is the entire PHP file

 <?php header("Content-Type: text/html; charset=utf-8"); mysql_set_charset('utf8'); ?> <!DOCTYPE html> <html> <head> <title></title> <script src="script/jquery-1.11.3.min.js"></script> <style> ol, ul { list-style: none; } table { border-collapse: collapse; border-spacing: 0; } table { height: auto; width: 1000px; float: left; margin: 20px auto; } table th, td { width: 166.66px; padding: 2px 2px 2px 2px; font-size: 16px; text-align: center; background-color: #dbe8f0; border: 1px solid #6897bb; } </style> </head> <?php /*ФОРМА 4*/ /*===ПОИСК ДАННЫХ В БАЗЕ===*/ if($_REQUEST['find']) { function funFind() { //ключевое слово для поиска $word = $_POST['serch']; //открытие соединения с сервером базы данных (SQL сервер, SQL логин, SQL пароль) $connect = mysql_connect('localhost', 'root', '12345'); //выбор базы данных (база SQL, переменная соединения) mysql_select_db('abc', $connect); //запрос $query = "SELECT start, end, name, price, price_action, discount FROM t1"; //$query = "SELECT start, end, name, price, price_action, discount FROM t1 WHERE = 5"; //выполнение запроса и запись выполнения в переменную $run = mysql_query($query, $connect); //результат - массив $result = mysql_fetch_array($run) or die(mysql_error()); echo '<table><tr><th>начало акции</th><th>конец акции</th><th>название</th><th>обычная цена</th><th>акционная цена</th><th>скидка в %</th></tr>'; do{ echo '<tr>'; echo '<td>'.$result[0].'</td>'; echo '<td>'.$result[1].'</td>'; echo '<td>'.$result[2].'</td>'; echo '<td>'.$result[3].'</td>'; echo '<td>'.$result[4].'</td>'; echo '<td>'.$result[5].'</td>'; echo '</tr>'; } while($result = mysql_fetch_array($run)); echo '</table>'; //закрытие соединения mysql_close($connect); //отмена повторной отправки формы header("Location: ".$_SERVER["REQUEST_URI"]); exit; } } ?> <body> <form id="f4" name="form4" action="" method="post"></form> <input type="text" form="f4" name="serch" /> &nbsp;&nbsp; <input type="submit" form="f4" name="find" value="Искать данные" /> <br /> <br /> <?php funFind(); ?> </body> </html> 

This request is working.

 $query = "SELECT start, end, name, price, price_action, discount FROM t1"; 

This request (it is for comment) does not work anymore !!!

 $query = "SELECT start, end, name, price, price_action, discount FROM t1 WHERE = 5"; 

A query with input through the input field does not work either.

 $query = "SELECT start, end, name, price, price_action, discount FROM t1 WHERE id = ".(int)$word.""; 

What am I doing wrong? Tell me.

  • one
    First of all, in the second query, you have written WHERE = 5 but there should be at least WHERE id = 5 - Alexey Shimansky
  • Alexey Shimansky, I corrected WHERE id = 5 , data is not output - Anton
  • Alexey Shimansky, the most interesting thing is that if I make the same query in the MySQL console, the data is output, and not displayed on the site page. - Anton
  • @Anton scares me what a serial is worth with id - Shilgen
  • ShilgenThis is a special alias, applied to the record identifier columns, stands for: bigint unsigned not null auto_increment unique, where: bigint is a large integer type; unsigned - unsigned; not null - null values ​​are not allowed; auto_increment - automatically increased; unique - only unique values; - Anton

1 answer 1

And what is passed in the post-data keyword for searching the database columns "start, end, name, price, price_action, discount" or is the object ID still? Use the LIKE operator in the query to search for the desired columns.

In your case, the query might look like this:

 $query = "SELECT start, end, name, price, price_action, discount FROM t1 WHERE start LIKE '%$word%' OR end LIKE '%$word%' OR price LIKE '%$word%' OR price_action LIKE '%$word%' OR discount LIKE '%$word%'");