gpt4 book ai didi

php - 我将如何更改此 MySql 语句

转载 作者:行者123 更新时间:2023-11-29 13:15:22 24 4
gpt4 key购买 nike

嗨,目前我正在尝试创建一个登录系统,但在为用户设置权限时遇到了问题,这是我用来从 phpMyAdmin 数据库查找用户名和密码的 MySql 语句。

 $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

我想知道是否有人可以帮助我编辑此代码,以便我可以将 account_type 回显或打印到页面,以便我可以在 PHP if 语句中使用它来限制用户看到的内容。

我已经尝试过

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword' and account_type ='myaccount'";    
$result=mysql_query($sql);

这是完整的页面代码

<?php

ob_start();
$host="127.0.0.0"; // Host name
$username="username"; // Mysql username
$password="password"; // Mysql password
$db_name="dbname"; // Database name
$tbl_name="table"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Define $myusername and $mypassword
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);

print_r($row);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){

// Register $myusername, $mypassword and redirect to file "login_success.php"

session_start();
$_SESSION['username'] = $myusername;
$_SESSION['password'] = $mypassword;




//session_register("myusername");
//session_register("mypassword");
//echo "correct";
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}

ob_end_flush();
?>

最佳答案

您缺少 mysql_fetch_array() 调用。

$sql = "SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);

之后就可以访问$row数组;要查看其内容,请使用:

print_r($row);

要访问特定字段,请使用$row['fieldname']

关于php - 我将如何更改此 MySql 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21531183/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com