Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中将串接字符串转换为单个字符串作为变量_Php_Mysql - Fatal编程技术网

在php中将串接字符串转换为单个字符串作为变量

在php中将串接字符串转换为单个字符串作为变量,php,mysql,Php,Mysql,我有一个表单字段,希望检查每个输入字段是否已经存在于数据库中 下面是我的php代码: $sql_query = "SELECT * FROM test_table"; if($result_query = $connect->query($sql_query)) { $items = array("firstName", "lastName", "country", "phone"); //$names variable contains stored values wh

我有一个表单字段,希望检查每个输入字段是否已经存在于数据库中

下面是我的php代码:

$sql_query = "SELECT * FROM test_table";
if($result_query = $connect->query($sql_query))
{
    $items = array("firstName", "lastName", "country", "phone");

    //$names variable contains stored values which i've declared at the beginning
    $names = array($firstName, $lastName, $country, $phone);

    foreach(array_combine($items, $names) as $item => $name)
    {
        $array_items = "";
        $check_query = "SELECT $item FROM test_table WHERE $item = '".$name."'";
        $result_check_query = $connect->query($check_query);
        if($row_query = $result_check_query->num_rows)
        {
         $array_items .= $item . ", ";
        }
        echo $array_items . "gettype()=> " . gettype($array_items);
        echo "<br>";
    }
    print_r ( "<br>" . $array_items); 
}
$sql\u query=“从测试表中选择*”;
如果($result\u query=$connect->query($sql\u query))
{
$items=数组(“firstName”、“lastName”、“country”、“phone”);
//$names变量包含我在开头声明的存储值
$names=数组($firstName、$lastName、$country、$phone);
foreach(数组\组合($items,$names)为$item=>$name)
{
$array_items=“”;
$check_query=“从测试_表中选择$item,其中$item='”“$name.”;
$result\u check\u query=$connect->query($check\u query);
if($row\u query=$result\u check\u query->num\u rows)
{
$array_items.=$item.“,”;
}
echo$array\u items.“gettype()=>”.gettype($array\u items);
回声“
”; } 打印(“
”$array\u项目); }
echo$array\u items
的结果如下:

问题是,当我打印/回显
$array\u items
外部
foreach
循环时,它只会得到
phone

如何将所有单个字符串作为一个字符串,并将其分配给php中的一个变量

提前谢谢

如果有旧帖子的链接,请给我提供链接。 我不知道用于搜索此类问题的术语。

请尝试此代码

$sql_query = "SELECT * FROM test_table";
if($result_query = $connect->query($sql_query))
{
    $items = array("firstName", "lastName", "country", "phone");

    //$names variable contains stored values which i've declared at the beginning
    $names = array($firstName, $lastName, $country, $phone);
    $array_items = "";
    foreach(array_combine($items, $names) as $item => $name)
    {

        $check_query = "SELECT $item FROM test_table WHERE $item = '".$name."'";
        $result_check_query = $connect->query($check_query);
        if($row_query = $result_check_query->num_rows)
        {
         $array_items .= $item . ", ";
        }
        echo $array_items . "gettype()=> " . gettype($array_items);
        echo "<br>";
    }
    print_r ( "<br>" . $array_items); 
}
$sql\u query=“从测试表中选择*”;
如果($result\u query=$connect->query($sql\u query))
{
$items=数组(“firstName”、“lastName”、“country”、“phone”);
//$names变量包含我在开头声明的存储值
$names=数组($firstName、$lastName、$country、$phone);
$array_items=“”;
foreach(数组\组合($items,$names)为$item=>$name)
{
$check_query=“从测试_表中选择$item,其中$item='”“$name.”;
$result\u check\u query=$connect->query($check\u query);
if($row\u query=$result\u check\u query->num\u rows)
{
$array_items.=$item.“,”;
}
echo$array\u items.“gettype()=>”.gettype($array\u items);
回声“
”; } 打印(“
”$array\u项目); }

代码的问题是在foreach循环中分配$array\u items变量。每次循环执行时,它都将值设置为空白($array_items=“”)

Oh my。。非常感谢你。将初始变量放入
foreach
循环有多愚蠢。是否希望最后的数组作为最终输出(将组字符串输出包含在单独的索引中)?这也会很有帮助。是的,请告诉我@AlivetoDieyou已经有了答案,并且它也被选中了