How to add an SQL querie to data in SQL table?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Publisher Bucks
    Confirmed User
    • Oct 2018
    • 1330

    #1

    Tech How to add an SQL querie to data in SQL table?

    Is this possible to do?

    I have some HTML stored in a database and I want to add a query to the HTML code that gets pulled when it gets displayed on a page, is this possible to do? Im not finding anything on Google so I'm thinking it might not be?

    If not, are there any alternates to storing a query in a database and pulling it through SQL other than including the query itself in an include.php file and inserting that into the .php page?

    Basically, pulling one sql querie that includes another in the data that is being pulled, if that makes sense?
    Extreme Link List - v1.0
  • brassmonkey
    Pay It Forward
    • Sep 2005
    • 77396

    #2
    stackflow for free fast help
    TRUMP 2026 KEKAW!!! - The Laken Riley Act Is Law!
    DACA ENDED - SUPPORT AZ HCR 2060 52R - email: brassballz-at-techie.com

    Comment

    • k0nr4d
      Confirmed User
      • Aug 2006
      • 9231

      #3
      Originally posted by Publisher Bucks
      if that makes sense?
      Unfortunately, none of what you wrote makes any sense
      Mechanical Bunny Media
      Mechbunny Tube Script | Mechbunny Webcam Aggregator Script | Custom Web Development

      Comment

      • EddyTheDog
        Just Doing My Own Thing
        • Jan 2011
        • 25433

        #4
        Damn - That gave me a headache - You need to start that chapter again ...

        Comment

        • Publisher Bucks
          Confirmed User
          • Oct 2018
          • 1330

          #5
          index.php - pulls header.php as an SQL query.
          header.php - is a blank page that contains an SQL query to pull the html code from a database.

          Can I include another SQL query in the header.php HTML even though the code is stored in the SQL database, not in the actual header.php file.

          I know that I can include an SQL query in a regular server side php include, that contains HTML code, not an SQL query to pull the HTML code.

          Can I do the same thing pulling the code out of my SQL database?

          <head>
          <base href="https://gfy.com/" /><!--[if IE]></base><![endif]-->
          <base href="https://gfy.com/" /><!--[if IE]></base><![endif]-->
          <base href="https://gfy.com/" /><!--[if IE]></base><![endif]-->
          <title>Untitled 1</title>
          </head>

          <body>
          <?php
          $servername = "localhost";
          $username = "user";
          $password = "pass";
          $dbname = "db";

          // Create connection
          $conn = new mysqli($servername, $username, $password, $dbname);
          // Check connection
          if ($conn->connect_error) {
          die("Connection failed: " . $conn->connect_error);
          }

          $sql = "SELECT * FROM Table WHERE Body != '' LIMIT 1";
          $result = $conn->query($sql);

          if ($result->num_rows > 0) {
          echo "";
          // output data of each row
          while($row = $result->fetch_assoc()) {
          echo "".$row["Body"]."";
          }
          echo "";
          } else {
          echo "0 results";
          }$conn->close();
          ?>
          </body>

          </html>
          In the SQL table 'body' contains this:

          <a href="urldotcom">keyword</a>
          Can I have it (the body data in the table) include the following:

          <a href="urldotcom"><?php
          $servername = "localhost";
          $username = "user";
          $password = "pass";
          $dbname = "db";

          // Create connection
          $conn = new mysqli($servername, $username, $password, $dbname);
          // Check connection
          if ($conn->connect_error) {
          die("Connection failed: " . $conn->connect_error);
          }

          $sql = "SELECT * FROM Table WHERE Keyword != '' LIMIT 1";
          $result = $conn->query($sql);

          if ($result->num_rows > 0) {
          echo "";
          // output data of each row
          while($row = $result->fetch_assoc()) {
          echo "".$row["Keyword"]."";
          }
          echo "";
          } else {
          echo "0 results";
          }$conn->close();
          ?></a>
          An SQL query inside an SQL query in effect?

          It doesnt work when I do it like that, but its entirely possible I'm not using the right method of calling the 2nd query, if it is even possible.
          Extreme Link List - v1.0

          Comment

          • Klen
            • Aug 2006
            • 32235

            #6
            You want to pull code from SQL ?

            Comment

            • k0nr4d
              Confirmed User
              • Aug 2006
              • 9231

              #7
              I've re-read this like 5 times, and I still don't know what you are trying to accomplish.
              Are you asking if you can store PHP code within a mysql database column? Yes, you can. This is an absolutely terrible idea, but yes, it can be accomplished using eval().

              I am unable to determine from your post what your end goal is but if eval() is the solution to your problem, then you need to take a step back and redesign this. You should not be storing SQL queries or PHP code in a mysql column.

              Please explain what you are trying to actually do, without any of your code examples.
              Mechanical Bunny Media
              Mechbunny Tube Script | Mechbunny Webcam Aggregator Script | Custom Web Development

              Comment

              • k0nr4d
                Confirmed User
                • Aug 2006
                • 9231

                #8
                Again I have no idea what you are doing and why you are even using loops if you are doing LIMIT 1, but maybe this will steer you in the right direction.

                PHP Code:
                <?php
                $servername = "localhost";
                $username = "user";
                $password = "pass";
                $dbname = "db";
                $conn = new mysqli($servername, $username, $password, $dbname);
                if ($conn->connect_error) {
                    die("Connection failed: " . $conn->connect_error);
                }
                
                $result = $conn->query("SELECT * FROM Table WHERE Body != '' LIMIT 1");
                if ($result->num_rows > 0) {
                    while($row = $result->fetch_assoc()) {
                        $sresult = $conn->query("SELECT * FROM Table WHERE Keyword != '' LIMIT 1");
                        while($srow = $sresult->fetch_assoc()) {
                            echo "<a href=\"urldotcom\">".$srow['Keyword']."</a>";
                        }
                    }
                } else {
                    echo "0 results";
                }
                ?>
                Mechanical Bunny Media
                Mechbunny Tube Script | Mechbunny Webcam Aggregator Script | Custom Web Development

                Comment

                • ZTT
                  Confirmed User
                  • Apr 2019
                  • 659

                  #9
                  I like PB, and some of his help threads are reasonable, but I don't know why anyone bothers seriously replying to parody level nonsense like this, where the only possible excuse is trolling, and it's not that.

                  What he wants is simple to understand, it's just so idiotic that it can't possibly be that. But it is.

                  If you don't know anything about MySQL, or even what a database is supposed to be for, the solution isn't being spoonfed more code to paste into the spaghetti monster you have already, inspiring a new thread tomorrow, it's to stop using it until you know how to use it.
                  __________________

                  Comment

                  • Klen
                    • Aug 2006
                    • 32235

                    #10
                    Originally posted by ZTT
                    I like PB, and some of his help threads are reasonable, but I don't know why anyone bothers seriously replying to parody level nonsense like this, where the only possible excuse is trolling, and it's not that.

                    What he wants is simple to understand, it's just so idiotic that it can't possibly be that. But it is.

                    If you don't know anything about MySQL, or even what a database is supposed to be for, the solution isn't being spoonfed more code to paste into the spaghetti monster you have already, inspiring a new thread tomorrow, it's to stop using it until you know how to use it.
                    A challenge ?

                    Comment

                    • Publisher Bucks
                      Confirmed User
                      • Oct 2018
                      • 1330

                      #11
                      Originally posted by k0nr4d
                      Again I have no idea what you are doing and why you are even using loops if you are doing LIMIT 1, but maybe this will steer you in the right direction.

                      PHP Code:
                      <?php
                      $servername = "localhost";
                      $username = "user";
                      $password = "pass";
                      $dbname = "db";
                      $conn = new mysqli($servername, $username, $password, $dbname);
                      if ($conn->connect_error) {
                          die("Connection failed: " . $conn->connect_error);
                      }
                      
                      $result = $conn->query("SELECT * FROM Table WHERE Body != '' LIMIT 1");
                      if ($result->num_rows > 0) {
                          while($row = $result->fetch_assoc()) {
                              $sresult = $conn->query("SELECT * FROM Table WHERE Keyword != '' LIMIT 1");
                              while($srow = $sresult->fetch_assoc()) {
                                  echo "<a href=\"urldotcom\">".$srow['Keyword']."</a>";
                              }
                          }
                      } else {
                          echo "0 results";
                      }
                      ?>
                      The code above was just copy & pasted to show what I was asking about, not the code I'm actually using, was just open in notepad lol


                      Basically, its for a makeshift CMS type admin area to manage a couple of sites via a crud system and on the external side of things, wanted to generate the pages using SQL outputs to save time (and be able to use predefined templates) but I also wanted to include a query in those templates to pull some niche specific data instead of having to hand code it into each template individually, I figured having that data stored in a seperate table and pulling it into the html code (stored in another table) would be the quickest way of doing that?
                      Extreme Link List - v1.0

                      Comment

                      • Why
                        MFBA
                        • Mar 2003
                        • 7230

                        #12
                        i really dont know what the confusion was. i understood your question quite easily. i will however agree with someone above who said what you want to do is a terrible idea.

                        also, no one is going to write the code for you, unless you pay us. otherwise the best youll get is psuedo code or a link to more help. asking people to work for free is kinda silly, IMO. will you come do some random stuff for me for free?

                        youd be better off to save those templates to disk, as opposed to in a database, it will be quicker and cheaper.

                        what you want to do is perfectly possible youll just have to play with delimiting things correctly so they come in and out of the database correctly. but not recommended.

                        Comment

                        • Publisher Bucks
                          Confirmed User
                          • Oct 2018
                          • 1330

                          #13
                          Originally posted by Why
                          i will however agree with someone above who said what you want to do is a terrible idea.
                          Can I ask why?

                          also, no one is going to write the code for you, unless you pay us.
                          I'm not expecting to have the code written, that would just defeat the purpose of my learning to code myself

                          Pointers in the right direction are always appreicated, as are links to tutorials, etc.

                          The only way I'll learn is by doing things (and messing things up) myself
                          Extreme Link List - v1.0

                          Comment

                          • Why
                            MFBA
                            • Mar 2003
                            • 7230

                            #14
                            templates will be faster to load(less work for the system) and safer, also a lot less complexity as you wont have to deal with the issues you are currently trying to solve.

                            what you what to do is quite literally the purpose of templating systems.

                            diving deeper into what you are doing....

                            you should never put sql credentials IN a file that is in a public directory. If PHP is broken for some reason in the future it would allow anyone viewing that page to see the database credentials. Always store credentials and other important stuff outside of the public directory and use an include to access it inside code that lives in public directory.

                            Comment

                            • k0nr4d
                              Confirmed User
                              • Aug 2006
                              • 9231

                              #15
                              Originally posted by Publisher Bucks
                              Can I ask why?
                              If you have php code in your db, and are running it via eval(), then any sql injection exploit (which there likely is, given you are new to this) will basically mean remote code execution.
                              Mechanical Bunny Media
                              Mechbunny Tube Script | Mechbunny Webcam Aggregator Script | Custom Web Development

                              Comment

                              Working...