打印出SQL查询的结果不起作用

时间:2018-08-23 21:04:50

标签: php mysql sql

我正在尝试执行SQL查询并在网页上打印出返回值。它应该从表中打印出一个名称列表,但是它只返回“ NULL”。有谁知道如何解决这个问题?

$sql = "SELECT name from Players_christmas where name not in (select name from Players_halloween");
$assoc = mysqli_fetch_assoc($sql);
var_dump($assoc);

2 个答案:

答案 0 :(得分:1)

请勿使用not in。尽管查询可能存在其他问题,但是如果子查询中的任何名称为not in,则NULL将不返回任何行。

相反,请习惯使用not exists

SELECT pc.name 
FROM Players_christmas pc 
WHERE NOT EXISTS (SELECT 1 FROM Players_halloween ph WHERE ph.name = pc.name);

答案 1 :(得分:1)

您没有执行查询。您需要这样做:

  public class EmailSetupController : ApiController
        {
            public string ToAddress { get; private set; }

            [HttpPost]
            [Route("api/EmailSetup/SendEmail/{ToEmail}")]
            public IHttpActionResult InsertUserAltAddRegTime([FromUri]string ToEmail)
            {
                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState);
                }

     int otp = new Random().Next(100000, 999999);
      session["CurrentOtp"] = otp;
    // Rest code for sending otp via email and it's working well
        :
        :

    return Ok();

            }



    } 

$sql = "SELECT name from Players_christmas where name not in (select name from Players_halloween"); $result = mysqli_query($conn, $sql); $assoc = mysqli_fetch_assoc($result); var_dump($assoc); $conn打开的连接。