应该是
$ value = Suria
<?php
$value = print($D->myname);
$year = 1980;
?>
正确的方法是什么?
答案 0 :(得分:1)
假设您使用PDO
对数据库进行了请求。
执行查询后,获取将其用作array
。
$req = $bdd->prepare("YOUR QUERY STRING");
$req->execute();
$myarray = $req->fetch()
// The result of the request is saved in this variable as an array
// use $req->fetch(PDO::FETCH_ASSOC) if you want an associative array
echo $myarray['my_value'];
// my_value is your myname
此外,您将函数print()
提供给变量$value
。您应该为变量赋值并在以下后打印:
$value = $myarray['my_value'] // I continue with the code I wrote above
print($value); // will write your variable
答案 1 :(得分:-1)
基于w3schools的例子:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT name FROM YourTable";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "Name: " . $row["name"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>