-1

I am looking to have a page with a redirect based on what is in the database. I have a table called "nametable" and 2 columns "id" and "switch" The id never changes, only the switch entry does. Sometimes it will have "on" as the entry, and sometimes it will have "off" as the entry (depending on what I enter in there at the time)

So I want a page where the website visitor will go to, and if the database says "on" then they will be redirected to, lets say "pageon.php" and if it says off, the visitor will be redirected to "pageoff.php"

I managed to do a simple echo to show on or off in text on the page. But I don't have the foggiest on how to have them redirected based on that.

Any thoughts on what I should search for to make this happen? And advice is appreciated.

PS. I tend to get a -1 because the site thinks I'm not specific on what I am wanting to do. If I am unclear, please tell me so I can revise before closing or -1

Thank you

EDIT: Based on the advice I was given in the comments, I have made this so far. I'm only getting a blank page though. Any thoughts?


    <?php
    $servername = " ";
    $username = " ";
    $password = " ";
    $dbname = " ";
    
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
      die("Connection failed: " . $conn->connect_error);
    }
    
    $sql = "SELECT id, switch FROM nametable";
    $result = $conn->query($sql);
    
    
    if ($row['switch'] == "on") header("Location: off.php"); else header("Location: on.php");
    
    
    
    $conn->close();
    ?>

 
ADyson
  • 57,178
  • 14
  • 51
  • 63
  • Does this answer your question? [How do I make a redirect in PHP?](https://stackoverflow.com/questions/768431/how-do-i-make-a-redirect-in-php) – Zak Jul 13 '22 at 15:56
  • All you would need to do is read that question, and apply that answer to your current if/else logic .. – Zak Jul 13 '22 at 15:57
  • @Zak no that doesn't answer it because that is just a redirect but not based on the database information. Thank you for suggesting it though – New Developer 2021 Jul 13 '22 at 15:58
  • Without any more information (code you have written) , or knowing your database structure and what fields, we're not going to be able to help much -- [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) -- [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) -- I am not downvoting in hopes to see your question edited, so we can better assist you! – Zak Jul 13 '22 at 15:59
  • 2
    @NewDeveloper2021 Assuming you know how to make a database query, and how to write an `if` statement, then the only thing you should need to solve this question is how to do the redirect. Why doesn't that answer the question? – Barmar Jul 13 '22 at 16:01
  • @Zak I have that in my question. As it says, I have a database with the table called "nametable" and columns named "id" and "switch". The id never changes, but the entry in "switch" does depending on what I enter at the time (sometimes I have "on" entered in it, sometimes I have "off" entered in it) – New Developer 2021 Jul 13 '22 at 16:02
  • 2
    So do the database query, then `if ($row['switch'] == "on") header("Location: pageoff.php"); else header("Location: pageon.php");` – Barmar Jul 13 '22 at 16:03
  • @Barmar yes as a newbie, I only know how to echo what is in the database. So right now the page shows what is in the "Switch" column. That is as far as I have got. I don't know what direction to look for to make the function happen – New Developer 2021 Jul 13 '22 at 16:04
  • 1
    @Barmar Ahhh I was making my comment just as you were putting in yours. Thats very helpful thank you – New Developer 2021 Jul 13 '22 at 16:04
  • So your real question was probably "how to use database data to determine what action to take" or possibly even more fundamentally "how to use database data as a variable". You generally get a better response if you focus the question and explain precisely where you're stuck, rather than asking for something broad which is likely to be covered by other questions (or by pieceing them together). Even then though, I'd expect this is kind of covered in some tutorial somewhere about using the results of a query in some other way than just echoing them. – ADyson Jul 13 '22 at 16:06
  • 1
    IMHO you are out of your depth if you are asking basic if / else questions. You might benefit from a quick PHP tutorial before writing code that you (as expressed by yourself) don't understand. If you don't get the fundamentals down now .. It will make learning more complicated coding a bear in the future. – Zak Jul 13 '22 at 16:06
  • @ADyson that is quite possible. Sorry everyone. I'm too new to know that termonology to even put those words to a question. All I know how to do is ask in laimans terms. Apologies – New Developer 2021 Jul 13 '22 at 16:07
  • I edited my question based on what @ADyson said. Thank you – New Developer 2021 Jul 13 '22 at 16:10
  • I edited the question again with script that I have so far. I hope that helps my question more – New Developer 2021 Jul 13 '22 at 16:19

2 Answers2

1

Try this:

<?php
    $servername = " ";
    $username = " ";
    $password = " ";
    $dbname = " ";
    
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
      die("Connection failed: " . $conn->connect_error);
    }
    
    $sql = "SELECT id, switch FROM nametable";
    $result = $conn->query($sql);
    
    $row = $result->fetch_assoc();
    
    if ($row['switch'] == "on"){ 
        header("Location: off.php"); 
    } else { 
        header("Location: on.php"); 
    }
    
    $conn->close();
    
?>
Edgaras
  • 11
  • 2
1

I got it. Thanks to the help in the comments, and the answer by @Edgaras except I made a tiny switch to have the == off, and that made it work. thank you all so much. Here is the solution.

<?php
    $servername = " ";
    $username = " ";
    $password = " ";
    $dbname = " ";
    
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
      die("Connection failed: " . $conn->connect_error);
    }
    
    $sql = "SELECT id, switch FROM nametable";
    $result = $conn->query($sql);
    
    $row = $result->fetch_assoc();
    
    if ($row['switch'] == "off"){ 
        header("Location: off.php"); 
    } else { 
        header("Location: on.php"); 
    }
    
    $conn->close();
    
?>