automatically create new pages with php and mysqli

2020-03-30 08:12发布

still getting my feet wet with php and mysqli, have so much to learn, but at this point this question is one of my most important priorities.

I did some research about this issue but am currently overwhelmed by pretty sophisticated stuff for my level, to be honest. I'd like to find the simplest most efficient way to "automatically" generate a great number of pages each with varying data in it.

the example of page 1's code below is extremely simplified, because the actual page actually has a lot more stuff, but the simplified example serves, I hope, to make my point.

<?php

$servername = "servername";
$username = "username";
$password = "password";
$db= "db";

$conn = mysqli_connect("servername","username","password","db");


$query = "SELECT word FROM demo WHERE group=1";
$result = $conn->query($query);
$row = mysqli_fetch_assoc($result);
$word = $row['word'];

echo $word;

?>

in my table I have / would have something like 500 entries (records?) in the 'group' column, numbered 1, 2, 3 etc all the way to 500. for my specific purpose, I absolutely need to create as many online pages as there are groups -- in this example, 500 pages.

page 2's echo would have to refer to group 2, page 3's echo would have to refer to group 3, and so on.

obviously, there's a way to do this without copying and pasting the code 500 times and manually changing the group in each! haha. but what's the simplest way?

thank you in advance for any understanding and help, and either way, have an awesome day.

标签: php mysqli
2条回答
聊天终结者
2楼-- · 2020-03-30 08:41

If I'm understanding you correctly, I believe you're waiting to create pages from the database Dynamically. You can use a get variable in the request http://yoursite.com/page.php?group=1. Then in your code update your query to do:

$query = "SELECT word FROM demo WHERE group=".$_GET['group'];

That query is insecure, as any user could inject raw mysql into the $_GET['group'] variable.

$group = mysqli_real_escape_string($conn, $_GET['group']);
$query = "SELECT word FROM demo WHERE `group`='$group'";

This is much safer.

查看更多
时光不老,我们不散
3楼-- · 2020-03-30 08:48

So PHP will look for a file called index.php by default in any directory that it accesses. You can place such a file in the root of public_html or www or where ever your site accesses. Now in this file you can do something like:

<?php
    if($_GET['group']){ //Make sure you have the var
         $query = "SELECT word FROM demo WHERE `group`=?"; //The query with param
        if ($stmt = mysqli_prepare($conn, query){ // try it out
            mysqli_stmt_bind_param($stmt, "i", $_GET['group']); // bind the data
            $stmt->execute(); //run it
            $result = $stmt->get_result(); // get results

            //use result to echo and stuff
        }
    } else {
        //Do something incase there is not a group specified.
        echo "Nothing here";
    }
?>

Now when you go to your site you will get something like 'localhost/index.php' and see Nothing here but if you type localhost/index.php?group='55' you will have access to the page 55 data in result.

查看更多
登录 后发表回答