gpt4 book ai didi

php - mysqli_query()预期为1

转载 作者:行者123 更新时间:2023-11-29 11:39:04 24 4
gpt4 key购买 nike

我从下面提供的代码中收到以下错误:


  警告:mysqli_query()期望参数1为mysqli,第7行的C:\ xampp \ htdocs \ index.php中给出的字符串
  
  警告:mysqli_error()期望恰好有1个参数,第8行的C:\ xampp \ htdocs \ index.php中给出的参数为0
  错误:


<?php
$conn = mysqli_connect("localhost","root","");//server, username and password are your server address and access details
if(!$conn)
die("cannot connect to mysql server" . mysqli_error());
mysqli_select_db( $conn,"website_data");
$sql = "INSERT INTO IP_Logs (IP) VALUES(" . $_SERVER['REMOTE_ADDR'] . ")";
if(!mysqli_query($sql,$conn))
die("ERROR: " .mysqli_error());
mysqli_close($con);
?>

最佳答案

您在mysqli_query调用中弄错了参数;您的数据库连接应该是第一个:

if(!mysqli_query($conn,$sql))


要解决其他错误,您需要将 $conn传递给 mysqli_error调用:

    die("ERROR: " .mysqli_error($conn));


现在,要解决您的SQL语法错误,请尝试在 $_SERVER['REMOTE_ATTR']周围的查询中添加引号,如下所示:

$sql = "INSERT INTO IP_Logs (IP) VALUES(\"" . $_SERVER['REMOTE_ADDR'] . "\")";

关于php - mysqli_query()预期为1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36104823/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com