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!

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

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况