简体   繁体   中英

Access denied for user 'name'@'localhost' (using password: NO)

I am trying to create a cron job in which it will send a mail when my inventory stocks are low. The email is just sending me the error code above, even though my database settings are accurate. So here is my code:

<?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);
}
?>

What seems to go wrong? Thanks! Here is the image of the email

You are currently mixing up the mysqli and mysql querys. When connecting via mysqli you cannot user functions like mysql_query.

Code should be something along the lines of as a quick example:

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

You'll have to change any mysql_ methods to the new mysqli in order for it to work correctly.

 <?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); } ?> 

try this code.You mix mysql or mysqli code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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