GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   Tech How to add an SQL querie to data in SQL table? (https://gfy.com/showthread.php?t=1351981)

Publisher Bucks 02-04-2022 09:41 PM

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? :Oh crap

Basically, pulling one sql querie that includes another in the data that is being pulled, if that makes sense?

brassmonkey 02-05-2022 12:00 AM

stackflow for free fast help

k0nr4d 02-05-2022 02:51 AM

Quote:

Originally Posted by Publisher Bucks (Post 22961862)
if that makes sense?

Unfortunately, none of what you wrote makes any sense :thumbsup

EddyTheDog 02-05-2022 03:02 AM

Damn - That gave me a headache - You need to start that chapter again :upsidedow...

Publisher Bucks 02-05-2022 03:20 AM

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?

Quote:

<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:

Quote:

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

Quote:

<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.

Klen 02-05-2022 04:49 AM

You want to pull code from SQL ?

k0nr4d 02-05-2022 05:41 AM

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.

k0nr4d 02-05-2022 05:50 AM

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";
}
?>


ZTT 02-05-2022 06:11 AM

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.

Klen 02-05-2022 09:39 AM

Quote:

Originally Posted by ZTT (Post 22961916)
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 ? :1orglaugh

Publisher Bucks 02-05-2022 11:37 AM

Quote:

Originally Posted by k0nr4d (Post 22961912)
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?

Why 02-05-2022 12:17 PM

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.

Publisher Bucks 02-05-2022 12:22 PM

Quote:

Originally Posted by Why (Post 22961977)
i will however agree with someone above who said what you want to do is a terrible idea.

Can I ask why?

Quote:

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 :1orglaugh

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 :thumbsup

Why 02-05-2022 12:45 PM

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.

k0nr4d 02-05-2022 02:21 PM

Quote:

Originally Posted by Publisher Bucks (Post 22961978)
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.


All times are GMT -7. The time now is 09:37 AM.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123