Welcome to the GoFuckYourself.com - Adult Webmaster Forum forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Post New Thread Reply

Register GFY Rules Calendar
Go Back   GoFuckYourself.com - Adult Webmaster Forum > >
Discuss what's fucking going on, and which programs are best and worst. One-time "program" announcements from "established" webmasters are allowed.

 
Thread Tools
Old 08-02-2021, 10:10 AM   #1
Publisher Bucks
Confirmed User
 
Industry Role:
Join Date: Oct 2018
Location: New Orleans, Louisiana. / Newcastle, England.
Posts: 1,123
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.
Publisher Bucks is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 08-02-2021, 10:18 AM   #2
Colmike9
(>^_^)b
 
Colmike9's Avatar
 
Industry Role:
Join Date: Dec 2011
Posts: 7,224
// 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?
__________________
Join the BEST cam affiliate program on the internet!
I've referred over $1.7mil in spending this past year, you should join in.
I make a lot more money in the medical field in a lab now, fuck you guys. Don't ask me to come back, but do join Chaturbate in my sig, it still makes bank without me touching shit for years..
Colmike9 is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 08-02-2021, 10:35 AM   #3
Publisher Bucks
Confirmed User
 
Industry Role:
Join Date: Oct 2018
Location: New Orleans, Louisiana. / Newcastle, England.
Posts: 1,123
Quote:
Originally Posted by Colmike9 View Post
// 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).
Publisher Bucks is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 08-02-2021, 10:49 AM   #4
Way3
Confirmed User
 
Way3's Avatar
 
Industry Role:
Join Date: Mar 2007
Location: Phoenix, Arizona
Posts: 1,725
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.
__________________

FULLY Managed! FREE Control Panel! FREE Month on ALL Shared Accounts!!!
ICQ: 169-554-261
Email: info[at]way3{dot}com
Way3 is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 08-02-2021, 11:29 AM   #5
Publisher Bucks
Confirmed User
 
Industry Role:
Join Date: Oct 2018
Location: New Orleans, Louisiana. / Newcastle, England.
Posts: 1,123
Quote:
Originally Posted by Way3 View Post
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?
Publisher Bucks is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 08-02-2021, 11:44 AM   #6
k0nr4d
Confirmed User
 
k0nr4d's Avatar
 
Industry Role:
Join Date: Aug 2006
Location: Poland
Posts: 9,228
Quote:
Originally Posted by Publisher Bucks View Post
Doesnt mysqli_connect establish that?
Sure, if the info is correct

https://www.php.net/manual/en/mysqli.connect-error.php
k0nr4d is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 08-02-2021, 11:45 AM   #7
k0nr4d
Confirmed User
 
k0nr4d's Avatar
 
Industry Role:
Join Date: Aug 2006
Location: Poland
Posts: 9,228
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
k0nr4d is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 08-02-2021, 12:01 PM   #8
sarettah
see you later, I'm gone
 
Industry Role:
Join Date: Oct 2002
Posts: 14,057
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.

.
sarettah is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 08-02-2021, 12:28 PM   #9
Publisher Bucks
Confirmed User
 
Industry Role:
Join Date: Oct 2018
Location: New Orleans, Louisiana. / Newcastle, England.
Posts: 1,123
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!
Publisher Bucks is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 08-02-2021, 12:39 PM   #10
Klen
 
Klen's Avatar
 
Industry Role:
Join Date: Aug 2006
Location: Little Vienna
Posts: 32,235
It seems i was too late on party :D Still, always check how your variables contain data/functions.
Klen is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 08-02-2021, 12:42 PM   #11
Publisher Bucks
Confirmed User
 
Industry Role:
Join Date: Oct 2018
Location: New Orleans, Louisiana. / Newcastle, England.
Posts: 1,123
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
Publisher Bucks is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 08-02-2021, 12:49 PM   #12
sarettah
see you later, I'm gone
 
Industry Role:
Join Date: Oct 2002
Posts: 14,057
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
.
sarettah is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 08-02-2021, 12:58 PM   #13
vdbucks
Monger Cash
 
Industry Role:
Join Date: Jul 2010
Posts: 2,773
Quote:
Originally Posted by Publisher Bucks View Post
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();
?>
vdbucks is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 08-02-2021, 03:40 PM   #14
sarettah
see you later, I'm gone
 
Industry Role:
Join Date: Oct 2002
Posts: 14,057
LOL, miss a lot of stuff when you move too fast.

.
sarettah is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 08-03-2021, 02:19 AM   #15
Klen
 
Klen's Avatar
 
Industry Role:
Join Date: Aug 2006
Location: Little Vienna
Posts: 32,235
Quote:
Originally Posted by sarettah View Post
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.
Klen is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Post New Thread Reply
Go Back   GoFuckYourself.com - Adult Webmaster Forum > >

Bookmarks

Tags
page, viewmore.php, data, sql, click, row, code, database, correct, information, displayed, link, viewmore.php?id=46, generate, programmers, web, dynamic, php, issues, displaying, fucking, index.php, display, listing, personal



Advertising inquiries - marketing at gfy dot com

Contact Admin - Advertise - GFY Rules - Top

©2000-, AI Media Network Inc



Powered by vBulletin
Copyright © 2000- Jelsoft Enterprises Limited.