【发布时间】:2018-08-09 22:13:53
【问题描述】:
我正在尝试使用一个对象从 mysql 数据库中获取数据,该对象实际上是一个变量:
try {
$mysqlcon = new PDO($dbserver, $db['user'], $db['pass'], $dboptions);
// Selecting database "user" to get data about a specific client
if(($user_data = $mysqlcon->query("SELECT * FROM $dbname.user;")->fetchAll(PDO::FETCH_UNIQUE|PDO::FETCH_ASSOC)) === false)
{
echo '$#!+ happens, Critical DataBase Error!';// error on db select
}
echo 'Country: '.$user_data[$clientuid]['nation']; // This is not working, it gives nothing
echo 'Country: '.$user_data['OISnyJzyjA63XkPB6xla8hjms3M=']['nation']; // BUT This works perfectly
// DEBUGGING
echo $clientuid; // it outputs: OISnyJzyjA63XkPB6xla8hjms3M=
var_dump($clientuid); // and this is the important thing, it outputs this: object(TeamSpeak3_Helper_String)#27 (2) { ["string":protected]=> string(28) "OISnyJzyjA63XkPB6xla8hjms3M=" ["position":protected]=> int(0) }
}
catch (PDOException $e)
{
echo "Database Connection failed: ".$e->getMessage()."\n";
exit;
}
echo $clientuid; 输出:OISnyJzyjA63XkPB6xla8hjms3M=
这是行不通的,它什么也没提供:
echo 'Country: '.$user_data[$clientuid]['nation'];
这很好用:
echo 'Country: '.$user_data['OISnyJzyjA63XkPB6xla8hjms3M=']['nation'];
var_dump($clientuid); 产生这个:
object(TeamSpeak3_Helper_String)#27 (2) {
["string":protected]=> string(28) "OISnyJzyjA63XkPB6xla8hjms3M="
["position":protected]=> int(0)
}
【问题讨论】:
-
好吧,祝你好运。你有问题吗?
-
$user_data[$clientuid]['nation']; // This is not working, it gives nothing- 它应该给你一个警告。
标签: php mysql string variables protected