PHP 使用变量从 MySQL 获取单个值

问题描述

我似乎无法通过 UserEmail 从用户表中找到 UserID 的简单查询

我有一个简单的函数来假设返回 UserID functions.PHP

function get_userID($UEml) 
{
// Check database connection
  if( ($DB instanceof MysqLi) == false) {
    return array(status => false,message => 'MysqL connection is invalid');
  }
  $qsql = "SELECT UsID FROM Users WHERE UsEml=? LIMIT 1";
  $qsql = $DB->prepare($qsql);
  $UEml = $DB->real_escape_string($UEml);
  $qsql->bind_param("s",$UEml);
  $qsql->execute();
  $result = $qsql->get_result();
  while ($row = $result->fetch_row()) {
    return $row[0];
  }
//  return $row[0];
  if($qsql) {
    return array(status => true);
  }
  else {
    return array(status => false,message => 'Not Found');
  }
}

我从PHP脚本中调用check-User.PHP

<?PHP
require_once("db-config.PHP");
include 'functions.PHP';
...
$UsID = get_userID("joe@example.com");
echo 'UserID: <span style="color: blue">'. $UsID ."</span>";
...
?>

db-config.PHP

<?PHP
// Two options for connecting to the database:

define('HOST_DIRECT','example.com'); // Standard connection
define('HOST_LOCAL','127.0.0.1');    // Secure connection,slower performance

define('DB_HOST',HOST_DIRECT);         // Choose HOST_DIRECT or HOST_STUNNEL,depending on your application's requirements

define('DB_USER','dbUser');    // MysqL account username
define('DB_PASS','SecretPas');    // MysqL account password
define('DB_NAME','dbname');     // Name of database
 
// Connect to the database
$DB = new MysqLi(DB_HOST,DB_USER,DB_PASS,DB_NAME);

if ($DB->connect_error) {
  die("Connection Failed: " . $DB->connect_error);
}
//echo "Connected successfully";
?>

我尝试了许多变体,但没有运气,也在这里检查了许多类似的帖子,但无法使其正常工作。 谢谢

解决方法

这里有几个错误:

  • $DB 在函数中不可用
  • model = keras.Sequential([ layers.Dense(128) ]) 语句是错误的

这是没有这两个错误的代码:

echo

还有你的function get_userID($DB,$UEml) { // Check database connection if ( ($DB instanceof MySQLi) == false) { return array(status => false,message => 'MySQL connection is invalid'); } $qSQL = "SELECT UsID FROM Users WHERE UsEml=? LIMIT 1"; $qSQL = $DB->prepare($qSQL); $qSQL->bind_param("s",$UEml); $qSQL->execute(); $result = $qSQL->get_result(); while ($row = $result->fetch_row()) { return $row[0]; } // return $row[0]; // I do not know why you wrote this code. If you get an user this code will not be executed if ($qSQL) { return array(status => true); } else { return array(status => false,message => 'Not Found'); } }

echo