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 Having PHP Issues with Displaying SQL Data on a Dynamic Web Page. (https://gfy.com/showthread.php?t=1347102)

Publisher Bucks 08-02-2021 10:10 AM

Having PHP Issues with Displaying SQL Data on a Dynamic Web Page.
 
Any programmers able to see what I'm fucking up?

This is for a personal project I'm working on.

I should be able to open index.php to display a listing of 'recipes' in the database which will allow me to click through to viewmore.php where i have more information displayed on the page.

PHP Code:

<?php


// change the user_name and password
$db mysqli_connect("localhost""dbuser""dbpass");
 
// change the database_name
mysqli_select_db("dbname",$db);

$id $_GET['id'];

$result mysqli_query($con,"SELECT RecipeID FROM Recipes WHERE id = $id");

echo 
"<table width=100%>
<tr>
<th>ID</th>
<th>Title</th>
<th>Ingredients</th>
<th>Method</th>
<th>Keywords</th>
</tr>"
;

while(
$row mysqli_fetch_array($result))
{

$id $row['RecipeID'];

echo 
"<tr>";
echo 
"<td>" $row['RecipeID'] .  "</td>";
echo 
"<td> <a href='viewmore.php?id=$id'>" $row['Title'] . "</a> </td>";
echo 
"<td>" $row['Ingredients'] .  "</td>";
echo 
"<td>" $row['Method'] . "</td>";
echo 
"<td>" $row['Keywords'] . "</td>";
echo 
"</tr>";
}
echo 
"</table>";

?>

Which should, allow me to generate a link to viewmore.php?id=46 to show the data in the sql for that row, correct?

This is my viewmore.php page code...

PHP Code:

<?php

$id 
$_get['id'];

$result mysqli_query($con,"SELECT * FROM Recipes WHERE RecipeID='$id'");

echo 
"<table width=100%>
<tr>
<th>Title</th>
<th>Ingredients</th>
<th>Method</th>
<th>Keywords</th>
</tr>"
;

while(
$row mysqli_fetch_array($result))
{
echo 
"<tr>";
echo 
"<td> <a href='#'>" $row['Title'] . "</a> </td>";
echo 
"<td>" $row['Ingredients'] .  "</td>";
echo 
"<td>" $row['Method'] . "</td>";
echo 
"<td>" $row['Keywords'] . "</td>";
echo 
"</tr>";
}
echo 
"</table>";

?>

I'm sure its a simple issue that I'm missing but I've been up almost 48 hours and my mind just isn't seeing it :/

Any help would be greatly appreciated.

Colmike9 08-02-2021 10:18 AM

// change the user_name and password
$db = mysqli_connect("localhost", "dbuser", "dbpass");

// change the database_name
mysqli_select_db("dbname",$db);


Did you put in the right info?

Publisher Bucks 08-02-2021 10:35 AM

Quote:

Originally Posted by Colmike9 (Post 22894867)
// change the user_name and password
$db = mysqli_connect("localhost", "dbuser", "dbpass");

// change the database_name
mysqli_select_db("dbname",$db);


Did you put in the right info?

Yes, that was the first thing I checked as previously I've spent hours trying to debug something only to realize I made a typo on the username lol

I do know my host just recently upgraded a bunch of php so I also have a ticket in with them to make sure they didn't fuck anything up (its Hostgator who I use for my personal stuff).

Way3 08-02-2021 10:49 AM

I don't think it is making the database connection in the viewmore.php or the index.php file.

I don't believe the $con has actually been defined for the db connection.

Just my 2 cents. :)

Publisher Bucks 08-02-2021 11:29 AM

Quote:

Originally Posted by Way3 (Post 22894876)
I don't think it is making the database connection in the viewmore.php or the index.php file.

I don't believe the $con has actually been defined for the db connection.

Just my 2 cents. :)

Doesnt mysqli_connect establish that?

k0nr4d 08-02-2021 11:44 AM

Quote:

Originally Posted by Publisher Bucks (Post 22894883)
Doesnt mysqli_connect establish that?

Sure, if the info is correct

https://www.php.net/manual/en/mysqli.connect-error.php

k0nr4d 08-02-2021 11:45 AM

Nevermind, I just actually looked at your code.
You called your db connection $db but are called $con in your mysqli_query

$con = mysqli_connect("localhost", "dbuser", "dbpass");
mysqli_select_db("dbname",$con);

You also need to establish the db connection on the viewmore.php file

sarettah 08-02-2021 12:01 PM

This is just wrong unless I am really missing something.

You select recipeid from the table but then you are trying to echo out variables before you have them. You are treating the href as if it is an include.


while($row = mysqli_fetch_array($result))
{

// Here you pull in the ID field.
$id = $row['RecipeID'];

echo "<tr>";
echo "<td>" . $row['RecipeID'] . "</td>";

//Everything is fine till here.

// You attempt to build your href but it isnt going to work because
// you attempt to echo out title when you don't have the title yet.

echo "<td> <a href='viewmore.php?id=$id'>" . $row['Title'] . "</a> </td>";

// Then you attempt to echo out the other fields when, again, you don't have them yet.

echo "<td>" . $row['Ingredients'] . "</td>";
echo "<td>" . $row['Method'] . "</td>";
echo "<td>" . $row['Keywords'] . "</td>";
echo "</tr>";
}
echo "</table>";

.................................................. ......................

Try something like this instead:


$result = mysqli_query($con,"SELECT RecipeID, Title FROM Recipes WHERE id = $id");

echo "<table width=100%>
<tr>
<th>ID</th>
<th>Title</th>
</tr>";


while($row = mysqli_fetch_array($result))
{

$id = $row['RecipeID'];

echo "<tr>";
echo "<td>" . $row['RecipeID'] . "</td>";
echo "<td> <a href='viewmore.php?id=$id'>" . $row['Title'] . "</a> </td>";
echo "</tr>";
}
echo "</table>";

Then, in viewmore, as k0nr4d said, you need to establish a connection.

Not sure I caught all of it but that is the way I see it, like I said, unless I missed something somewhere.

.

Publisher Bucks 08-02-2021 12:28 PM

Appreciate the help guys, going to play around a little and see if I can put a working code together based on your feedback, thank you!

Klen 08-02-2021 12:39 PM

It seems i was too late on party :D Still, always check how your variables contain data/functions.

Publisher Bucks 08-02-2021 12:42 PM

Ive edited the code as suggested for both pages and now its throwing out a blank page with the 'ID' and 'Title' table but no data being inserted, i checked the error logs and its showing me this:

Quote:

[02-Aug-2021 14:30:31 America/Chicago] PHP Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given in /home2/blah/blah.com/index.php on line 6
[02-Aug-2021 14:30:31 America/Chicago] PHP Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, bool given in /home2/blah/blah.com/index.php on line 26
Here is the new code im using based on the feedback in this thread so far...

index.php

PHP Code:

<?php


// change the user_name and password
$con mysqli_connect("localhost""dbuser""dbpass");
mysqli_select_db("dbname",$con);
 

$id $_GET['RecipeID'];

// Check connection
if (mysqli_connect_errno()) {
  echo 
"Failed to connect to MySQL: " mysqli_connect_error();
  exit();
}

$result mysqli_query($con,"SELECT RecipeID, Title FROM Recipes WHERE id = $id");

echo 
"<table width=100%>
<tr>
<th>ID</th>
<th>Title</th>
</tr>"
;


while(
$row mysqli_fetch_array($result))
{

$id $row['RecipeID'];

echo 
"<tr>";
echo 
"<td>" $row['RecipeID'] . "</td>";
echo 
"<td> <a href='viewmore.php?id=$id'>" $row['Title'] . "</a> </td>";
echo 
"</tr>";
}
echo 
"</table>";

?>

and for the viewmore.php

PHP Code:

<?php

// change the user_name and password
$con mysqli_connect("localhost""dbuser""dbpass");
mysqli_select_db("dbname",$con);
 
$id $_GET['RecipeID'];

// Check connection
if (mysqli_connect_errno()) {
  echo 
"Failed to connect to MySQL: " mysqli_connect_error();
  exit();
}

$result mysqli_query($con,"SELECT RecipeID, Title FROM Recipes WHERE id = $id");

echo 
"<table width=100%>
<tr>
<th>ID</th>
<th>Title</th>
</tr>"
;


while(
$row mysqli_fetch_array($result))
{

$id $row['RecipeID'];

echo 
"<tr>";
echo 
"<td>" $row['RecipeID'] . "</td>";
echo 
"<td> <a href='viewmore.php?id=$id'>" $row['Title'] . "</a> </td>";
echo 
"</tr>";
}
echo 
"</table>";

?>

The page itself isn't throwing out any errors from the connection, and the ID and Title shows in the table but no data is being displayed.

The url im using is:

domain.com/index.php?id=X (where X is a valid RecipeID in the table)
domain.com/viewmore.php?id=X (where X is a valid RecipeID in the table)

I'm going to get a few hours sleep, but if ya'll happen to spot anything amiss it'd be appreciated.

Thanks again for the help so far :thumbsup

sarettah 08-02-2021 12:49 PM

mysqli_select_db("dbname",$con);

Should be mysqli_select_db($con,"dbname");

PHP.net is your friend.

https://www.php.net/manual/en/mysqli.select-db.php

Quote:

Procedural style

mysqli_select_db(mysqli $mysql, string $database): bool
.

vdbucks 08-02-2021 12:58 PM

Quote:

Originally Posted by Publisher Bucks (Post 22894865)
Any programmers able to see what I'm fucking up?

This is for a personal project I'm working on.

I should be able to open index.php to display a listing of 'recipes' in the database which will allow me to click through to viewmore.php where i have more information displayed on the page.


Which should, allow me to generate a link to viewmore.php?id=46 to show the data in the sql for that row, correct?



I'm sure its a simple issue that I'm missing but I've been up almost 48 hours and my mind just isn't seeing it :/

Any help would be greatly appreciated.

There are a handful of things wrong here...

1. Your stated goal is to display a list of recipes on index.php with a link to the individual recipe info on viewmore.php. The problem is, your index.php script is expecting a recipe ID to be passed as a query string, ala index.php?id=123.

2. Your SELECT query on index.php only ever asks for 'ReceipID' from the db, but you're attempting to display other data. Not to mention, you're trying to get the ReceipID from the db while also telling the db what the RecipeID is.

3. Your output on index.php and viewmore.php are effectively the same, assuming the queries worked.

4. You're using <table> in 2021.

At any rate, going by what your stated goal is, I think something like this is more in line with what you want:

Code:

<?php
/*** index.php ***/

        // change hostname, username, password, and dbname
        $db = new mysqli( 'hostname', 'username', 'password', 'dbname');
        if ($db->connect_errno)
                die("Connect failed: %s\n", $db->connect_error);
       
        $getRecipes = "SELECT ReceipID, Title FROM Recipes";
        $response = $db->query($getRecipes);

        if ($response->num_rows) {
                echo "<table width=100%>
                                <tr>
                                <th>ID</th>
                                <th>Title</th>
                                </tr>";

                $row = $db->get_results($response);

                foreach($row as $key => $value) {

                        $id = $value->RecipeID;
                        $title = $value->Title;

                        echo "<tr>";
                        echo "<td>{$id}</td>"; // not sure why you want to display the recipe id, but okay.
                        echo "<td> <a href=\"viewmore.php?id={$id}\">{$title}</a></td>";
                        echo "</tr>";
                }

                echo "</table>";
        }

        $db->close();
?>

Code:

<?php
/*** viewmore.php ***/

        // change hostname, username, password, and dbname
        $db = new mysqli( 'hostname', 'username', 'password', 'dbname');
        if ($db->connect_errno)
                die("Connect failed: %s\n", $db->connect_error);

        $id = $_GET['id'];

        $getRecipe = "SELECT * FROM Recipes WHERE ReceipID = {$id}";
        $response = $db->query($getRecipe);

        if ($response->num_rows) {
                echo "<table width=100%>
                                <tr>
                                <th>Title</th>
                                <th>Ingredients</th>
                                <th>Method</th>
                                <th>Keywords</th>
                                </tr>";

                $row = $db->get_results($response);

                foreach($row as $key => $value) {
                        echo "<tr>";
                        echo "<td>{$value->Title}</td>";
                        echo "<td>{$value->Ingredients}</td>";
                        echo "<td>{$value->Method}</td>";
                        echo "<td>{$value->Keywords}</td>";
                        echo "</tr>";
                }
                echo "</table>";
        }

        $db->close();
?>


sarettah 08-02-2021 03:40 PM

LOL, miss a lot of stuff when you move too fast.

.

Klen 08-03-2021 02:19 AM

Quote:

Originally Posted by sarettah (Post 22894906)
mysqli_select_db("dbname",$con);

Should be mysqli_select_db($con,"dbname");

PHP.net is your friend.

https://www.php.net/manual/en/mysqli.select-db.php



.

Yep, php.net is still one of most valuable resources, tho funny thing is how solution is often found in comments, not in official entry.


All times are GMT -7. The time now is 12:23 PM.

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