为什么我不能使用PHP访问mysql?

时间:2017-07-11 23:03:04

标签: php mysql sql web

我一直在处理这个问题。 我认为一切都是正确的,但它一直在告诉查询存在问题,实际情况就是如此。但我无法真正找到它。

以下是代码:

<?php
function query_check ($query)
{
    if(!$query)
    {
        die ("Database query failed");

    }
}

function Create($Name,$Last_Name,$Number)
{ 
global $conncetion;

$S_Name=mysqli_real_escape_string($conncetion,$Name);
$S_Last_Name=mysqli_real_escape_string($conncetion,$Last_Name);
$S_Number=mysqli_real_escape_string($conncetion,$Number);
$query="INSERT INTO phone_book.info (first_name,last_name,phone_number)
VALUES ({$S_Name},{$S_Last_Name},{$S_Number}); ";
$result=mysqli_query($conncetion,$query);
query_check($result);   
}

//Delete
function Del($Name,$Last_Name,$Number)
{   
    global $conncetion;

    $S_Name=mysqli_real_escape_string($conncetion,$Name);
    $S_Last_Name=mysqli_real_escape_string($conncetion,$Last_Name);
    $S_Number=mysqli_real_escape_string($conncetion,$Number);
    $query="DELETE FROM ";
    $query.="phone_book.info WHERE first_name={$S_Name} OR last_name={$S_Last_Name} OR phone_number={$S_Number}";
    $result=mysqli_query($conncetion,$query);
    query_check($result);

}

function Search($Name,$Last_Name,$Number)
{
    global $conncetion;

    $S_Name=mysqli_real_escape_string($conncetion,$Name);
    $S_Last_Name=mysqli_real_escape_string($conncetion,$Last_Name);
    $S_Number=mysqli_real_escape_string($conncetion,$Number);
    $query="select * from info where first_name={$S_Name} or last_name={$S_Last_Name} or phone_number={$S_Number}";
    $result=mysqli_query($conncetion, $query);
    query_check($result);
    if($search=mysqli_fetch_assoc($result))
    {
             return $search["first_name"];
             return $search["last_name"];
             return $search["phone_number"];
    } else
    {
        return "No such information was found";
    }

}
?>

我希望你能帮我找到问题。

2 个答案:

答案 0 :(得分:2)

在你的Sql中,你插入的是我假设的很多字符串值,但是没有引用其中一个,所以Sql会尝试将它们作为整数插入。

例如:

$query="INSERT INTO phone_book.info (first_name,last_name,phone_number) VALUES ({$S_Name},{$S_Last_Name},{$S_Number}); ";

应如下:

$query="INSERT INTO phone_book.info (first_name,last_name,phone_number) VALUES ('{$S_Name}','{$S_Last_Name}','{$S_Number}'); ";

您的删除和选择语句存在完全相同的问题。

答案 1 :(得分:0)

$query="INSERT INTO phone_book.info (first_name,last_name,phone_number) VALUES ('$S_Name','$S_Last_Name','$S_Number')";