首页 > 解决方案 > 长文本字段的Mysql查询不返回任何内容

问题描述

我有一个长文本字段的 mysql 查询,但查询不返回任何内容。

我尝试用“短”文本替换该字段,看看它是否有效。它确实返回了简短的文本。当我把它转回长文本时它就没有了

这是我的php代码

$sql = "SELECT * FROM fact WHERE id = ".$id;
$tmp = $this->con->query($sql); 
return $tmp->fetch_assoc();

这是我的mysql表

-- Dumping structure for table tb_system.fact
CREATE TABLE IF NOT EXISTS `fact` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `fact` longtext NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=25 DEFAULT CHARSET=latin1;

-- Dumping data for table tb_system.fact: ~2 rows (approximately)
/*!40000 ALTER TABLE `fact` DISABLE KEYS */;
INSERT INTO `fact` (`id`, `fact`) VALUES
    (1, 'People with TB disease can pass TB germs to others. But if they take the TB medicine the right way, they won’t pass TB germs to others'),
    (2, 'To reduce exposure in households, houses should be adequately ventilated'),
    (3, 'To reduce exposure in households, while patients are smear positive, they should spend as little time as possible in places with large crowds'),
    (4, 'To reduce exposure in households, while patients are smear positive, they should spend as much time as possible outdoors'),
    (5, 'To reduce exposure in households, while patients are smear positive, they should, if possible, sleep alone in a separate, adequately ventilated room'),
    (6, 'To reduce exposure in households, while patients are smear positive, they should spend as little time as possible on public transport'),
    (7, 'If you forget your medicine more than one time, call your doctor or healthcare provider BEFORE you take the next dose'),
    (8, 'If you miss one dose or forget to take the pills ONE TIME, don’t worry. Just take the next dose when you are scheduled'),
    (9, 'Tip to stay on track with medicine plan from past tb patient James: “What kept me on track? I put a note in my wallet next to a picture of my family that said “James—stay healthy for your family.”'),
    (10, 'Taking medicine each day can be difficult. Remind yourself you are staying on your treatment plan to kill all the TB germs. You have done other tough things in your life and you can do this too!'),
    (11, 'You can pass TB germs to your family, friends, and others around you if you don’t take TB medicine the right way'),
    (12, 'You will need to take TB medicine correctly for at least 6 months to be cured'),
    (13, 'TB germs die very slowly. Staying on your medicine the way you are supposed to is the only way to cure TB'),
    (14, 'TB germs are strong and take a long time to die. Complete the treatment and take ALL of the doses of medicine to be cured of TB'),
    (15, 'Make sure you tell your doctor if you have HIV/AIDS or any other health problem. Sometimes taking certain medicines together can make you have a reaction'),
    (16, 'If you stop taking medicines for TB disease early or do not take them the right way, you can become sick again and stay sick for a longer time'),
    (17, 'If you stop taking medicines early or do not take them the right way, the medicines can stop working and you may have to take different medicines that have more side effects'),
    (18, 'If you stop taking medicines early or do not take them the right way, even the new medicines may not work to cure the TB'),
    (19, 'If you stop taking medicines early or do not take them the right way, you can pass TB germs on to others again'),
    (20, 'Keep windows open in your home, if possible, until your doctor says you cannot pass TB germs to others');
/*!40000 ALTER TABLE `fact` ENABLE KEYS */;

我希望输出是实际的“事实”值,即长文本

标签: phpmysql

解决方案


以下代码: $sql = "SELECT * FROM fact WHERE id = ".$id;不正确,变量应该在查询内部,引号之间,如下所示:

$sql = "SELECT * FROM fact WHERE id = '$id'";

此外,该return语句实际上并不打印结果,您需要使用echo它。


推荐阅读