简体   繁体   English

无法从PHP中的MySQL查询获取结果

[英]Unable to get results from MySQL query in PHP

I'm unable to get results from a MySQL query in PHP. 我无法从PHP中的MySQL查询获得结果。 I've tried get_result() and bind_param() . 我已经尝试过get_result()bind_param() However, this ends up breaking my script and I'm unable to echo any of the results for testing. 但是,这最终破坏了我的脚本,并且我无法回显任何测试结果。

My table has three columns: username, password, and email. 我的表有三列:用户名,密码和电子邮件。

$usernameprep = $conn->prepare("SELECT 1, password FROM `table` WHERE username=?");
$usernameprep->bind_param("s", $_POST['username']);
$usernameprep->execute();
$usernameprep->bind_results($userexists, $userpass);
$usernameprep->close();

What I've tried: 我尝试过的

  • get_result() and bind_param() get_result()bind_param()
  • putting bind_param() after AND before execute() bind_param()放在AND之后,然后execute()
  • echoing $user and $userpass , only for my script to break afterwards 回显$user$userpass ,仅用于我的脚本随后中断
  • searching endless StackOverflow questions and none of them work for me 搜索无尽的StackOverflow问题,但对我来说都不起作用

Note: I am used to SQLite3, so I'm still getting used to how MySQL works. 注意:我已经习惯了SQLite3,所以我仍然习惯于MySQL的工作方式。

UPDATE: adding code for my connection for reference 更新:为我的连接添加代码以供参考

$host = 'localhost';
$user = 'root';
$password = 'root';
$db = 'database';

$conn = new mysqli($host, $user, $password, $db);

// Check connection
if ($conn->connect_error) {
    echo "Connection Error, please try again.";
    die("Connection failed: " . $conn->connect_error);
} 

Updated 更新

echo 'Here is your post data.  Make sure you have your username.<br>';
echo '<pre>';
print_r($_POST);
echo '</pre>';


$query = "SELECT password FROM `table` WHERE username=?";

$stmt = $conn->prepare($query);
$stmt->bind_param("s", $_POST['username']);
$stmt->execute();
echo("Error description: " . mysqli_error($conn)); //This will show you your errors after the execute();
$results = $stmt->get_result();

if($results->num_rows !== 0){

while($row = $results->fetch_assoc()){

  $data[] = array(

    'userexists' => 1,
    'userpass'  => $row['password']

  );

}

}else{

  echo "Your query failed to find any data.";

}

$stmt->close();

echo '<pre>';
echo 'Here is your data:<br>';
print_r($data);
echo '</pre>';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM