$content
是一个带有“详细描述”的变量。
product_id
是名为 products
$content
) 的子字符串的列
我正在尝试创建一个选择语句,如果 product_id
包含在 $content
变量中,该语句将查找记录。然后我想使用 SELECT 语句中的 url
字段更新另一个名为 receive_sms
的表
在网站上进行研究后,我得出了以下结论......但它不起作用
$mysqlic = mysqli_connect("testsms.cloudaccess.net", "username", "password", "testsms2");
$prod_res=mysqli_query($mysqlic,"SELECT url from products
WHERE %product_id% LIKE %$content%");
mysqli_query($mysqlic,"INSERT INTO recieve_sms (comments) VALUES ('$prod_res')");
有什么想法吗?
最佳答案
应该是:
$prod_res = mysqli_query($mysqlic, "SELECT url from products
WHERE '$content' LIKE CONCAT('%', product_id, '%')");
或者:
$prod_res = mysqli_query($mysqlic, "SELECT url from products
WHERE LOCATE(product_id, '$content') != 0");
您需要将 $content
放在引号中。
实际上,如果您使用准备好的查询会更好,那么 '$content'
将成为占位符 ?
。
查询后,需要调用mysqli_fetch_assoc()
来获取列值:
$row = mysqli_fetch_assoc($prod_res);
$url = $row['url'];
关于PHP 和 MySQL SELECT 子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24099762/