PHP代码中的解析错误意外T_VARIABLE

时间:2017-08-04 07:05:02

标签: php

我正在尝试运行此代码

<?php
$mysqli = new mysqli("localhost", "root", "", "localhost");

$res = $mysqli->query("SELECT id, email FROM users WHERE id = 1");
$row = $res->fetch_assoc();

printf("id = %s (%s)\n", $row['id'], gettype($row['id']));
printf("email = %s (%s)\n", $row['email'], gettype($row['email']));
?>

但它正在返回解析错误

  

解析错误:语法错误,意外的'$ mysqli'(T_VARIABLE)in   第4行的D:\ xampp \ htdocs \ index.php

任何人都知道它可能是什么?

1 个答案:

答案 0 :(得分:0)

请以这种方式尝试您的代码,

<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$sql="SELECT id, email FROM users WHERE id = 1";
$result=mysqli_query($con,$sql);


// Associative array
$row=mysqli_fetch_assoc($result);
printf("id = %s (%s)\n", $row['id'], gettype($row['id']));
printf("email = %s (%s)\n", $row['email'], gettype($row['email']));

// Free result set
mysqli_free_result($result);

mysqli_close($con);
?>