Jay Cochran Jay Cochran - 20 days ago 5
MySQL Question

PHP/MySQL Redirect page if table value is 0

I need to check a database value assigned to a user account and redirect when loading a page if the value is 0 (or less than 1?)

code is have for the function is:

// ******************************************************
// JAY CHECK SCREENINGS FUNCTION
function CheckScreenings() {

if (!empty($_SESSION['userId'])) {
$conn = connectToDB();

if (!$conn) { die('Could not connect: ' . $conn->errorInfo()); }
$checkquery = ("SELECT screenings FROM screener_users WHERE user_id = '" . $_SESSION['userId'] . "';");
foreach ($conn->query($checkquery) as $row)
{
$screenings = $row[0];
}

if ($screenings == 0)
{
header("Location: http://example.com/myOtherPage.php");
die();
}
}
}
// ******************************************************


I also, on another page need to hide 2 hyperlinks if this same value is 0.

<li>
<a href="/screener-test">Take a new test</a>
</li>
<li style="margin-bottom: 20px;">
<?php $lastTest = getUserLatestIncompleteTest($user_id); ?>
<a href="/screener-test?test_id=<?php echo $lastTest[0]; ?>">Continue last incomplete test</a>
</li>


Any ideas?

Thanks,
J

Answer

If your functions do what I think they do, something written like

$result_rows = $conn->query($checkquery);
if (count(result_rows) == 0) {
    header("Location: http://example.com/myOtherPage.php");
}

Should work.