[英]PHP: Grab SQL result and insert into cookie value
I am currently trying to run a SQL statement and pull the result out into its own variable and set it as a cookie value but am unsure of how to tackle this and being stuck on it for quite some time Id love some insight. 我目前正在尝试运行SQL语句,并将结果提取到其自己的变量中,并将其设置为cookie值,但不确定如何解决并在相当长的时间内被卡住,我很喜欢一些见识。 I am using phpmyadmin,what am I doing wrong? 我正在使用phpmyadmin,我在做什么错?
<?php
include("db_connect.php");
session_start();
$tbl_name="users_table"; // Table name
// username and password sent from form
$myusername=$_GET['inputEmail'];
$mypassword=$_GET['inputPassword'];
$sql="SELECT * FROM $tbl_name WHERE Email='$myusername' and Password='$mypassword'";
if ($result = $conn->query($sql)) {
/* determine number of rows result set */
$row_cnt = $result->num_rows;
if($row_cnt==1){
$_SESSION['username']=($myusername);
$_SESSION['password']=($mypassword);
$cookie_name1 = "user";
$cookie_name2 = "content";
setcookie($cookie_name1, $_SESSION['username'], time() + (86400 * 30), "/"); // 86400 = 1 day
$sql="SELECT Pref_Game FROM $tbl_name WHERE Email='$myusername'";
$result = $conn->query($sql);
$row = $result->fetch_object();
setcookie($cookie_name2,$row->Pref_Game, time() + (86400 * 30), "/"); // 86400 = 1 day
}
else {
echo "<script>
alert('Wrong email or password entered!');
</script>";
}
$result->close();
}
?>
I am assuming that you are using the Mysqli class to query the database. 我假设您正在使用Mysqli类查询数据库。 You first need to query the database as you are only setting the $result variable to the query. 首先,您需要查询数据库,因为仅将$ result变量设置为查询。
$result = $conn->query($sql);
$row = $result->fetch_object();
So if the $sql contains the last query shown above ("SELECT Pref_Game FROM $tbl_name WHERE Email='$myusername'") you can get the value from the $row object by $row->Pref_Game 因此,如果$ sql包含上面显示的最后一个查询(“ SELECT Pref_Game FROM $ tbl_name WHERE Email ='$ myusername'”),则可以通过$ row-> Pref_Game从$ row对象获取值。
You need to put the $row = $result->fetch_object();
您需要将$row = $result->fetch_object();
in a while loop if more than one row is being queried. 在while循环中,如果要查询多个行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.