简体   繁体   中英

How to use variables and sessions in my sql statement?

I have been testing my sql statement, in different ways, but it's not working. I'm having trouble writing my variables and my session in the sql statement.

function onelike()
{
    $_SESSION['admin_id']
    $resultLikeCheck = "SELECT FROM tbladminXshouts WHERE postId = $row['id'] AND admin_id = $_SESSION['admin_id']"; 
    $results = mysql_query($resultLikeCheck) or die('');

    if(mysql_num_rows($results) == 0) 
    {
        echo ('

                 `<form action="" method="POST"> 
                       <input type="hidden" name="id" value="'.$row['id'].'"/>
                       <input type = "submit" name="Gilla" value = "Gilla"/> 
                  </form>`

              ');
    }
}

What is wrong with it?

$resultLikeCheck = "SELECT FROM tbladminXshouts WHERE postId = $row['id'] AND admin_id ='{$_SESSION['admin_id']}'";

使用“ {}”在字符串内插变量。

You can try this.

$adminID= $_SESSION['admin_id'];
$rowID= $row['id'];
onelike($adminID,$rowID);

    function onelike($adminID,$rowID)
    {


    $resultLikeCheck = "SELECT * FROM tbladminXshouts WHERE postId = '{$rowID}' AND admin_id = '{$adminID}'";

    $results = mysql_query($resultLikeCheck) or die('');

And I'd recommend using mysqli_ functions instead of mysql_

Edit: I think your problem is you are not passing your variables into the function. Functions in PHP have their own local variables and can't access variables outside themselves unless you send them in.
And yes, @Sajjad Merchant is right, the best way to interpolate variables into strings for queries is withuse of "{}"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM