[英]PHP, how to check if a MySQL table has a name
我正在尝试连接到MySQL数据库以检查表(builditblocks)以查看用户在“name”列中是否有数据。
这是我到目前为止:
<?php
$temp=mysqli_query($con, "SELECT * FROM `builditblocks`.`module_index`.`name`");
$i = 0;
while($module = mysqli_fetch_array($temp))
{
"moduleArray[".$i."]=\"" . $module["name"]."\";" ;
$i++
//stuck here I need to know how to
//store them all in an array and check
//to see if the input matches any array elements
}
如果有名称,我该如何查看表格?
首先,将名称推送到新数组并使用in_array
检查
$modules = array();
while($module = mysqli_fetch_array($temp))
{
$modules[] = $module['name'];
}
if(in_array($_POST['input'], $modules))
echo "I found";
else
echo "Not found";
您可能想尝试将“WHERE”子句与“LIKE”结合使用。
$temp = mysqli_query($con, "SELECT * FROM `builditblocks`.`module_index`.`name` WHERE `name` LIKE '%input%'");
上面的代码只会为您提供名称中包含“input”的记录。 有关更多信息,请参阅http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html#operator_like 。
要查找数据库中是否存在名称,您需要做的就是在SQL查询中添加WHERE
。
$name = $module['name'];
"SELECT * FROM 'builditblocks'.'module_index'.'name' WHERE name = '".$name."'"
然后检查是否返回任何内容。 如果是,则名称存在,如果不存在则则不存在。
请尝试以下方法:
$name = $module["name"]; // Assign your user input to a variable
$temp = mysqli_query($con, "SELECT * FROM builditblocks.module_index WHERE name = '" . $name . "'"; // Get all fields from table where the name field equals the user input.
if ($temp) {
// If any records were returned the name exists
} else {
// Otherwise the name doesn't exist
}
希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.