访问被拒绝用户'name'@'localhost'(使用密码:NO)

时间:2015-11-30 05:27:48

标签: php mysql

我正在尝试创建一个cron作业,当我的库存库存很低时,它会发送邮件。即使我的数据库设置准确,电子邮件也只是向我发送错误代码。所以这是我的代码:

<?php

//I just renamed them, but you get the concept
$db_host = "mydbhost";
$db_username = "myusername";
$db_password = "mypassword";
$db_name = "mydatabase";

$conn = mysqli_connect($db_host,$db_username,$db_password,$db_name) or die ('MySQL Not found // Could Not Connect.');

$recipient = "email@email.com";

$result = mysql_query("SELECT prod_count, restock_level FROM mydatabase WHERE prod_count <= restock_level;");
if ($result) {
  $row = mysql_fetch_assoc($result);
  $count = $row['prod_count'];
  $restocklvl = $row['restock_level'];
  if ($count <= $restocklvl) {
    $msg = "Your inventory is currently low. Please restock.";
    mail($recipient, "Inventory check below threshold", $msg);
  }
}
else {
  $msg = "An error occurred while checking inventory: " . mysql_error();
  mail($recipient, "Inventory check error", $msg);
}
?>

出现什么问题?谢谢! Here is the image of the email

2 个答案:

答案 0 :(得分:1)

您目前正在混淆mysqli和mysql查询。通过mysqli连接时,您无法使用mysql_query等用户函数。

代码应该是一个简单的例子:

$conn = mysqli_connect($db_host,$db_username,$db_password,$db_name)
$result = $conn->query($sql);
$row = $result->fetch_assoc();

您必须将任何mysql_方法更改为新的mysqli才能使其正常工作。

答案 1 :(得分:1)

<?php

//I just renamed them, but you get the concept
$db_host = "mydbhost";
$db_username = "myusername";
$db_password = "mypassword";
$db_name = "mydatabase";

$conn = mysqli_connect($db_host,$db_username,$db_password,$db_name) or die ('MySQL Not found // Could Not Connect.');

$recipient = "email@email.com";

$result = mysqli_query($conn,"SELECT prod_count, restock_level FROM mydatabase WHERE prod_count <= restock_level;");
if ($result) {
  $row = mysqli_fetch_assoc($result);
  $count = $row['prod_count'];
  $restocklvl = $row['restock_level'];
  if ($count <= $restocklvl) {
    $msg = "Your inventory is currently low. Please restock.";
    mail($recipient, "Inventory check below threshold", $msg);
  }
}
else {
  $msg = "An error occurred while checking inventory: " . mysqli_error($conn);
  mail($recipient, "Inventory check error", $msg);
}
?>

试试这个代码。你混合使用mysql或mysqli代码。