我有一个数据库,其中存储不同传感器导出的数据。在数据库中搜索时,我知道一行中的一列中的内容,但我需要打印该行中以已知字符开头的不同列。我正在尝试这样做:
for($i = 1; $i < 301; $i++)
{
$result = mysql_query("SELECT * FROM full_dump WHERE file_name='J7091-01.TXT' AND ".$row['field_'.$i.'']." LIKE 'OS' ")or die(mysql_error());
while($row = mysql_fetch_array($result))
{
echo $row['field_'.$i.''];
}
}
我得到这个结果:
Notice: Undefined variable: row in C:\xampp\htdocs\t1.php on line 11
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 'LIKE 'OS'' at line 1
我尝试使用以下方式进行搜索:
... and LIKE '%OS%'
没有运气。 我做错了什么?
我的mysql版本是5.6.16
最佳答案
我认为您不能在查询中使用变量$row
,而是应该这样做-
$result = mysql_query("SELECT .....AND field_".$i." LIKE 'OS' ")or die(mysql_error());
关于PHP搜索查询MYSQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22401212/