0

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
  • Ah, forgot. I'll just edit the post Commented Sep 7, 2015 at 2:27

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

21 Comments

Dude, you are the GOD! Most people don't actually write code for you here and explain indepth but you just DID! You're the first guy that is REALLY nice on stackoverflow. Most people just put "Too Broad", "Off Topic" and leave. Thanks again dude!
I'm just here to help. If I could tell you knew more about coding, I probably wouldn't have answered either. Everyone needs help when they're first starting. Some people seem to forget that AND seem to forget that at one point in their life they knew nothing about coding as well and needed a lot of help. Again, welcome to SO.
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2002] No such file or directory' in /srv/disk9/1921240/www/csgothrow.com/spin/checklogin.php:4 Stack trace: #0 /srv/disk9/1921240/www/csgothrow.com/spin/checklogin.php(4): PDO->__construct('mysql:host=;dbn...', NULL, NULL) #1 {main} thrown in /srv/disk9/1921240/www/csgothrow.com/spin/checklogin.php on line 4
I'm getting this error when running the file. What would the problem be? It seems there is a problem with the PDO?
Where are you running this code? xampp or on a server?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.