get value from MySQL database with PHP

前端 未结 4 1083
无人及你
无人及你 2020-12-20 07:51
$from = $_POST[\'from\'];
$to = $_POST[\'to\'];
$message = $_POST[\'message\'];

$query  = \"SELECT * FROM Users WHERE `user_name` = \'$from\' LIMIT 1\";
$result = m         


        
相关标签:
4条回答
  • 2020-12-20 08:03

    Try this:

    $from = mysql_real_escape_string($_POST['from']);
    $to = mysql_real_escape_string($_POST['to']);
    $message = mysql_real_escape_string($_POST['message']);
    
    $query  = "SELECT * FROM Users WHERE user_name = '$from' LIMIT 1";
    $result = mysql_query($query) or die(mysql_error());
    
    while($row = mysql_fetch_assoc($result)) {
        $fromID = $row['user_id'];
    }
    

    Also, make sure that:

    • You have connected to the database
    • You do get data from the post, try var_dump with your vars eg var_dump($from)
    0 讨论(0)
  • 2020-12-20 08:20

    Use mysql_fetch_assoc instead

    0 讨论(0)
  • 2020-12-20 08:22

    though it should. try this code

    $from    = mysql_real_escape_string($_POST['from']);
    $to      = mysql_real_escape_string($_POST['to']);
    $message = mysql_real_escape_string($_POST['message']);
    
    $query  = "SELECT * FROM Users WHERE `user_name` = '$from' LIMIT 1";
    $result = mysql_query($query) or trigger_error(mysql_error().$query);
    $row = mysql_fetch_array($result, MYSQL_ASSOC);
    $fromID = $row['user_id'];
    echo $fromID;
    

    if it will throw no errors but still print no id, add this line

    var_dump($row);
    

    and post here it's output

    not that you shouldn't use a user name but user id to address particular user.

    0 讨论(0)
  • 2020-12-20 08:25

    while($row =mysql_fetch_assoc($result)){ $fromID = $row['user_id']; }

    0 讨论(0)
提交回复
热议问题