简体   繁体   English

PHP,如何检查MySQL表是否有名称

[英]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.

相关问题 如何检查Mysql表有没有变化? - How to check Mysql table has changed or not? 使用PHP检查MySQL查询中哪个表的结果? - Check which table has the result in MySQL query, using PHP? PHP &amp; MySQL:检查表的数据是否在没有轮询的情况下发生了变化? - PHP & MySQL: Check if table's data has changed without polling? 如何不打印与使用 php 脚本登录的用户的会话名称相等的 mysql 表值 - how to not print a mysql table value which is equal to the the session name of the user who has logged in using a php script 如何检查表中是否已有名称? 查询Mysql每天的总和,然后通过PHP填充HTML数据 - How to check if name already in the table? Querying Mysql for sum of each day and then populating an HTML data through PHP with it PHP PDO检查mySQL数据库中是否存在具有特定名称的表 - PHP PDO check if a table with specific name exists in mySQL database 如何检查通过php是否可访问的mysql表 - How to check a mysql table accessible or not via php 如何检查新数据是否已插入到mysql表中 - how to check whether new data has been inserted in mysql table mysql join时如何查入另一张表没有数据? - How to check in another table has no data while doing mysql join? 如何检查MySQL表是否具有Java / Android中的下一行 - How to check if MySQL table has next row from Java/Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM