Hello! I have a bot on my site that automatically responds and has the function of sending a message:

function processMessage($message) { $message = mb_strtolower($message); if ($message === 'hi' || $message === 'hello' || $message === '/start') { return 'Hi, I am a bot . What status you want ?? For example: Love, Joke, about life, friendship'; } elseif ($message === 'love' || $message === 'about love' || $message === '/love') { $statuses = file('telegram_love.txt'); $status_id = array_rand($statuses); return trim($statuses[$status_id]); } elseif ($message === 'joke' || $message === 'about joke' || $message === '/joke') { $statuses = file('telegram_joke.txt'); $status_id = array_rand($statuses); return trim($statuses[$status_id]); } elseif ($message === 'life' || $message === 'about life' || $message === '/life') { $statuses = file('telegram_life.txt'); $status_id = array_rand($statuses); return trim($statuses[$status_id]); } elseif ($message === 'friendship' || $message === 'about friendship' || $message === '/friendship') { $statuses = file('telegram_friendship.txt'); $status_id = array_rand($statuses); return trim($statuses[$status_id]); } elseif ($message === 'about' || $message === '/about' || $message === '/info') { return "v 1.0 Developer Mail @mail @mail"; } else { return 'Or we do not have such type of status, or you make a mistake, use the fast function to find the statuses'; } } 

The script takes the answer from the .txt file and I want to make sure that the answers are taken from the DATABASE!

If not difficult, tell me where to start ??? Thank you in advance!

    1 answer 1

    Transfer all messages from files to a database with approximately the following structure:

     CREATE TABLE `messages` ( `id` int(11) NOT NULL, `tag` varchar(64) NOT NULL, `message` text NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; INSERT IGNORE INTO `messages` (`id`, `tag`, `message`) VALUES (1, 'love', 'Love message 1'), (2, 'love', 'Love message 2'), (3, 'love', 'Love message 3'), (4, 'joke', 'Joke message 1'), (5, 'joke', 'Joke message 2'), (6, 'joke', 'Joke message 3'), (7, 'life', 'Life message 1'), (8, 'life', 'Life message 2'), (9, 'life', 'Life message 3'), (10, 'friendship', 'Friendship message 1'), (11, 'friendship', 'Friendship message 2'), (12, 'friendship', 'Friendship message 3'), (13, 'hi', 'Hi, I am a bot . What status you want ?? For example: Love, Joke, about life, friendship'), (14, 'hello', 'Hi, I am a bot . What status you want ?? For example: Love, Joke, about life, friendship'), (15, 'start', 'Hi, I am a bot . What status you want ?? For example: Love, Joke, about life, friendship'), (16, 'about', 'v 1.0 Developer Name\r\n @mail\r\n @mail'), (17, 'info', 'v 1.0 Developer Name\r\n @mail\r\n @mail'); 

    And in the function to make a message selection by tag from this table

     function processMessage($message, $db_link) { $message = mb_strtolower($message); $message = str_replace('about ', '', $message); $message = trim($message, ' /'); $q = "SELECT `message` " ."FROM `messages` " ."WHERE `tag`='".mysqli_real_escape_string($db_link, $message)."' " ."ORDER BY RAND() " ."LIMIT 1"; $res = mysqli_query($db_link, $q); if ($res) { $status = mysqli_fetch_assoc($res); $status = $status['message']; } else { $status = 'Or we do not have such type of status, or you make a mistake, use the fast function to find the statuses'; } return $status; } 

    Using:

     $db_link = mysqli_connect('localhost', 'пользователь', 'пароль', 'имя базы') or die(mysqli_connect_error()); echo processMessage('about life', $db_link); 
    • but what about PDO ? - Vasily Barbashev
    • just like mysqli - Darevill
    • what?))) Why don't you use the class to work with the database? already ready, great working? - Vasily Barbashev
    • well, you can do it through PDO, of course, but the author asks "where to start?", so I decided that it is easier to set an example on mysqli - Darevill
    • Darevill, Thank you! - Access Denied