php中的SQL查询空白

时间:2014-12-08 12:12:43

标签: php sql

我可以打印好哈希码,然后我检查数据库中的值,它匹配正常。但是,当我运行get_user_id时 - 它根本没有提出任何东西。我对编程很新,所以我不确定我错过了什么希望有人可以指出它。

include("internals\config.php"); // DB info

$host       = DB_HOST;
$user       = DB_USER;
$password   = DB_PASS;
$dbname     = DB_NAME;
$prefix     = DB_TBL_PREFIX;

define('TBL_MAILBOX_CONVERSATION', $prefix.'mailbox_conversation');
define('TBL_MAILBOX_MESSAGE', $prefix.'mailbox_message');

$cxn = mysql_pconnect ($host, $user, $password);

mysql_select_db($dbname, $cxn);

$hash = ($_COOKIE['PHPSESSID']!='' ? $_COOKIE['PHPSESSID'] : 'Guest');

function get_user_id() 
    {   
        $userid = NULL;

        if (!empty($hash)) 
        {
            $result = mysql_query("SELECT `profile_id` FROM `skadate_profile_online` WHERE `hash` = '".$hash."' ");

            if ($row = mysql_fetch_array($result)) 
            {
                $userid = $row[0];
            }
        }

        return $userid;
    }

$profile_id  = get_user_id();
print $profile_id;

2 个答案:

答案 0 :(得分:1)

function get_user_id(); $ hash将始终为空,因为该值永远不会传递给函数。

此外, Mysqli -

固定:

function get_user_id($funcHash) 
    {   
        $userid = NULL;

    if (!empty($funcHash)) 
    {
        $result = mysql_query("SELECT `profile_id` FROM `skadate_profile_online` WHERE `hash` = '".$funcHash."' ");

        if ($row = mysql_fetch_array($result)) 
        {
            $userid = $row[0];
        }
    }

    return $userid;
}

所以当你运行代码时,你将$ hash值传递给函数:

$profile_id  = get_user_id($hash);

编辑: 第1点 - 请考虑使用MySQLi而不是标准MySQL,因为现在已完全弃用。

第2点 - 我故意改变了函数中的$ hash变量,因为这很容易让人感到困惑,你在函数的上下文中有一个$ hash,而在文件的上下文中有另一个变量$ hash,所以函数一我重命名为$ funcHash,

答案 1 :(得分:0)

您没有将$hash传递给get_user_id()

更改

$profile_id  = get_user_id();

$profile_id  = get_user_id($hash);
相关问题