douqinlu4217 2015-09-07 02:18
浏览 79
已采纳

将数据库中的MySQL数据放入变量中

I'm making a luck based website and I want to make sure they have enough spins on their account before they spin.

I did see http://www.w3schools.com/php/php_mysql_select.asp but its not really what I'm looking for.

I have these rows with the names: id username email password spins.

I can deduct amounts from spins but I can't put the exact amount of their spins on a PHP variable to put in a $SESSION for a different page.

Here's how much I have so far.

$numSpin = "SELECT * FROM $tbl_name WHERE spins";

Then put it in a $SESSION

$_SESSION['spinNum'] = $numSpin;

How would I go on to doing this? This does not work as is.

  • 写回答

1条回答 默认 最新

  • donglanying3855 2015-09-07 04:02
    关注

    Welcome to SO.

    It seems as if you are extremely new to coding so I'll try to help you out.

    Here is the code you can use and I'll explain below.

    <?php
    session_start();
    $host = 'localhost'; $db = 'db-name'; $user = 'db-user'; $pw = 'db-pwd';
    $conn = new PDO('mysql:host='.$host.';dbname='.$db.';charset=utf8', $user, $pw);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    try {
        $tbl_name = 'table-name-here';
        $un = $username;
        $sql = "SELECT * FROM $tbl_name WHERE username=:un";
        $query = $conn->prepare($sql);
        $query->bindValue(':un', $un, PDO::PARAM_STR);
        $query->execute();
        $row = $query->fetch(PDO::FETCH_ASSOC);
        $totalRows = $query->rowCount();
    } catch (PDOException $e) {
        die("Could not get the data: " . $e->getMessage());
    }
    $_SESSION['spinNum'] = $row['name-of-your-field-with-spin-numbers'];
    ?>
    

    Then...

    if($_SESSION['spinNum'] >= 1) {
        // allow them to do something
    } else {
        echo "I'm sorry. It looks like you don't have any spins left. Please try again later.";
    }
    

    This code is written using pdo_mysql. You might want to read up on it here.

    Line 2 starts your session

    Lines 3-5 creates a connection to your database. Make sure to replace "db-name", "db-user" & "db-pwd" with your information.

    On line 8 replace "table-name-here" with your database table name

    On line 9 you can set "10" to whatever minimum number you want to make sure the account holder has.

    On line 19 change "name-of-your-field-with-spin-numbers" to the actual name of the field in your database table that stores the account users available spins.

    Now you can use $_SESSION['spinNum'] on your other pages.

    Don't forget to use session_start(); at the top of any page where you want to use session variables.

    Happy coding!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路