[英]PHP, how to check if a MySQL table has a name
I am trying to connect to the MySQL database to check a table (builditblocks) to see if there is data the user gave in the "name" column. 我正在尝试连接到MySQL数据库以检查表(builditblocks)以查看用户在“name”列中是否有数据。
Here is what I have so far: 这是我到目前为止:
<?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
}
How do I check the table if it has the name? 如果有名称,我该如何查看表格?
At first, push names to new array and check with in_array
首先,将名称推送到新数组并使用
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";
You might want to try the "WHERE" clause combined with "LIKE". 您可能想尝试将“WHERE”子句与“LIKE”结合使用。
$temp = mysqli_query($con, "SELECT * FROM `builditblocks`.`module_index`.`name` WHERE `name` LIKE '%input%'");
The above code will give you only the records that contain "input" in their name. 上面的代码只会为您提供名称中包含“input”的记录。 Look http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html#operator_like for more information.
有关更多信息,请参阅http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html#operator_like 。
To find if a name exists in your database, all you need to do is add a WHERE
to your SQL query. 要查找数据库中是否存在名称,您需要做的就是在SQL查询中添加
WHERE
。
$name = $module['name'];
"SELECT * FROM 'builditblocks'.'module_index'.'name' WHERE name = '".$name."'"
Then check to see if that returns anything. 然后检查是否返回任何内容。 If so, the name exists, if not then it does not exist.
如果是,则名称存在,如果不存在则则不存在。
Try the following: 请尝试以下方法:
$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
}
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.