[英]How to get single value from MySQL query in PHP
I know this post is similar to many other Stack Overflow questions. 我知道这篇文章与许多其他Stack Overflow问题类似。 However, they quite didn't help to sort out this issue. 但是,他们完全没有帮助解决这个问题。 I want the query to return single value and store it in variable. 我希望查询返回单个值并将其存储在变量中。 The SQL query is working fine in the database but not with PHP. SQL查询在数据库中运行良好,但不适用于PHP。 Where have I gone wrong here? 我在哪里错了?
$datelink = $_GET['bdate'];
$nid = $mysqli->query("select `newsletterId` from `newsletter` where ndate='$datelink'")->fetch_object()->name;
$datelink=$_GET['bdate'];
$nid= $mysqli->query("SELECT `newsletterId` FROM `newsletter` WHERE ndate='$datelink' LIMIT 1")->fetch_object()->name;
You should initiate your connection before quering the database. 在查询数据库之前,您应该启动连接。
$mysqli = new mysqli("example.com", "user", "password", "database");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: " . $mysqli->connect_error;
}
It's also dangerous to just append your string from $_GET. 仅从$ _GET追加字符串也很危险。 Your site can be attacked with SQL injection. 您的网站可能会受到SQL注入的攻击。 You definitely should use PDO with prepared statements. 您绝对应该在准备好的语句中使用PDO 。 Or some another library like that, I like dibi for example. 或其他类似的库,例如,我喜欢dibi 。
Update: You are accessing column name
that doesn't exist. 更新:您正在访问的列name
不存在。 It should throw a notice ( PHP Notice: Undefined property: stdClass::$name
). 它应该引发一个通知( PHP Notice: Undefined property: stdClass::$name
)。 If you don't see notices and you are developing locally (and you definitely should), you can enable it in your php.ini
file with error_reporting = E_ALL
. 如果您没有看到通知并且您正在本地开发(当然应该),则可以在error_reporting = E_ALL
php.ini
文件中启用它。
You probably want column newsletterId
. 您可能需要column newsletterId
。 So altogether: 总共:
$mysqli = new mysqli('example.com', 'user', 'password', 'database');
if ($mysqli->connect_errno) {
echo 'Failed to connect to MySQL: ' . $mysqli->connect_error;
}
$dateRaw = $_GET['bdate']; // for example '22.12.2012', if this value is already in format YYYY-MM-DD, you can step next line and use it
$date = date('Y-m-d', strtotime($dateRaw));
$statement = $mysqli->prepare("SELECT `newsletterId` FROM `newsletter` WHERE `ndate`=? LIMIT 1");
$statement->bind_param('s', $date); // replace the question mark with properly escaped date
$statement->execute();
$result = $statement->get_result();
if ($result->num_rows) {
$newsletterObject = $result->fetch_object();
$newsletterId = $newsletterObject->newsletterId; // you had $newsletterObject->name here
} else {
echo "Newsletter with the date doesn't exist";
}
$statement->close();
Initially I thought that there is easy way to fetch single value but couldn't find it. 最初,我认为有一种简单的方法来获取单个值,但找不到它。 So have to use full code as below to get the value. 因此必须使用下面的完整代码来获取值。
$sql3="select newsletterId from newsletter where ndate='$bdate' limit 1";
$result3 = $conn->query($sql3);
if ($result3->num_rows > 0) {
while($row3 = $result3->fetch_assoc()) {
$nid=$row3['newsletterId']; //Here is where the single value is stored.
}
} else {
echo "0 results";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.