PHP mysqli_query()期望参数1为mysqli,在Confusing help needed中给出null

时间:2018-01-15 22:58:42

标签: php mysqli

我正在尝试将IP存储在MySQL数据库中,并且我遇到了一些问题,我能够解决这个问题,但是对于试图访问我网站的人来说,我一直遇到1个错误。因此,当有人进入我的网站时,他们的IP会显示一个时间戳,但只有当我连接到我的网站时它才有效。当我让我的朋友进入我的网站时,他得到一个错误,说明为什么你没有查询?这有助于我找出问题所在。现在的问题是,我一直在努力解决这个问题,过去2个小时没有运气:(
我的屏幕截图:My screen
我朋友的屏幕截图:Friends screen

         <html>
  <head>
    <title>Your IP!</title>
  </head>
  <body>
<?php
$db_host = '127.0.0.1';
$db_user = '***************';
$db_pwd = '*************';
$db = '***************';
// Find their IP and tell them what it is.
$con=mysqli_connect($db_host, $db_user, $db_pwd);

if (getenv('HTTP_X_FORWARDED_FOR')) {
  $pip = getenv('HTTP_X_FORWARDED_FOR');
  $ip = getenv('REMOTE_ADDR');
  echo "Your Proxy IP is: ".$pip."(via ".$ip.")";
} else {
  $ip = getenv('REMOTE_ADDR');
  echo "Your IP is: ".$ip;
}
echo "<br /><br />";
// Try to select the database.
if(!mysqli_select_db($con, $db)) {
//  die("why u no use db? ".mysql_error());
  die("why u no use db?");
}
// Try to perform query.
// This is a function so it may easily be called multiple times.
function do_query($query) { // Take in query.
    global $con;
  if(!$result = mysqli_query($con, $query)) {
//    die("why u no query? ".mysql_error());
    die("why u no query?");
  }
  return $result; // Give back result.
}
// Try to see if they are in the database already,
// and if not, then add them.
$result = do_query("select ip from ips where ip='".$ip."'");
$rows = mysqli_num_rows($result);
if($rows == 0) {
  do_query("insert into ips (ip) values ('".$ip."')");
}
// Now, display the table.
$result = do_query("select * from ips");
$cols = mysqli_num_fields($result);
echo "<table cellpadding=\"5\" bgcolor=\"#7F7F7F\"><tr>";
for($i = 0; $i < $cols; $i++) {
  echo "<td>".mysqli_fetch_field($result)->name."</td>";
}
echo "</tr>";
while($row = mysqli_fetch_row($result)) {
  echo "<tr>";
  for($i = 0; $i < $cols; $i++) {
    if($row[$i] == $ip) { // bold their IP.
      echo "<td><b>".$row[$i]."</b></td>";
    } else {
      echo "<td>".$row[$i]."</td>";
    }
  }
  echo "</tr>";
}
echo "</table>";
?>
  </body>
</html>

1 个答案:

答案 0 :(得分:0)

首先我改变了

function do_query($query) { // Take in query.
    global $con;
  if(!$result = mysqli_query($con, $query)) {
//    die("why u no query? ".mysql_error());
    die("why u no query?");

function do_query($query) { // Take in query.
    global $con;
  if(!$result = mysqli_query($con, $query)) {
//    die("why u no query? ".mysql_error());
    die(mysqli_error($con));

这显示了错误重复输入&#39; 0&#39;对于键&#39; PRIMARY&#39; ,问题是我没有在主键上设置AUTO_INCREMENT。