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 Is this possible with SQL? (https://gfy.com/showthread.php?t=1349116)

Publisher Bucks 10-13-2021 03:11 PM

Is this possible with SQL?
 
I have a table of recipes, all have unique id's.

What I'd like to be able to do is to add an article to a separate table, then, below that article on the .php page, display a listing of recipes from the first table that contain a specific keyword, such as 'bacon' to go with the article about bacon.

Is it possible to run an SQL querie in the .php code to display the article AND, have another array below that that displays say, 5 records from the recipe table.

I have a single article.php file that pulls based on the ID in the table containing articles, but not what the article is based on so article.php?id=1 for example, pulls up the bacon article.

How do I make it so that I can also pull up JUST 5 recipes that also contain 'bacon' in their ingredients list?

What is that called? Pulling from 2 different tables based on a specific keyword?

If I need to I can add a category to both tables if that makes it any easier overall to achieve what I need?

This is what I have currently for the article.php page:

Quote:

<?php

$id=intval($_GET['id']);

if($id==0)

{

header( "Location: .404/" ); //or any other page like 404

exit;

}

$pdo=hookitup();

$table = "Articles";

$sql="select * from " . $table . " where ID=" . $pdo->quote($id);

//echo "sql=" . $sql . "<br>";

$stmt=$pdo->query($sql);

//echo "rows back=" . $stmt->rowcount() . "<br>";

$row=$stmt->fetch(PDO::FETCH_ASSOC);

$title = $row['Title'];
$site = "Site Name";

?>
Followed by this to display the 5 recipes:

Quote:

<?php

$con=mysqli_connect("localhost","user","pass","db" );

$result = mysqli_query($con,"SELECT * FROM Recipes WHERE Ingredient REGEXP 'bacon ' ORDER BY RAND() LIMIT 5;");

echo "<table border='0'>

<tr>

</tr>";

while($row = mysqli_fetch_array($result))

{

$link = "recipe.php?id=".$row['ID'];

echo "<a class=\"style15\">";
echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b><a href=". $link .">". $row['Title'] ."</a></b><br />";
echo "<a class=\"style15\">";

}

echo "</table>";

mysqli_close($con);

?>
Is there a way I can use a single mysqli query to show the article AND 5 related recipes?

I was thinking I could use an include but that does not seem to be possible (?) :Oh crap

Any pointers as to what I should be looking into (if even possible) would be appreciated :thumbsup

ZTT 10-13-2021 08:21 PM

Try looking up JOIN and/or UNION.

Publisher Bucks 10-13-2021 08:51 PM

Quote:

Originally Posted by ZTT (Post 22924298)
Try looking up JOIN and/or UNION.

Perfect, thanks :thumbsup


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

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