/    Sign up×
Community /Pin to ProfileBookmark

Selecting a row?

I have a php script where I need to select all the columns of a certain row in a table in my db but it’s not working. I have another page where I have a link like this:

[b]http://www.mydomain.com/page.php?id=23[/b]

And when they click on that link they get sent to page.php with the var of ’23’ and I have a hidden field like so:

[b]<input type=”hidden” name=”id” value=”<?php $var = $_GET[‘id’]; echo $var; ?>” />[/b]

Which I use to retrieve the var. And then I refrence that hidden field in my script to try to display the row from the db table that has that id. Here is my script:

[code=php]<?php
include “config.php”;
mysql_connect($mysqlserv,$mysqluser,$mysqlpass);
mysql_select_db($mysqldbname);
$id = $_POST[‘id’];
$query = “SELECT * FROM news WHERE id = ‘$id'”;
$result = mysql_query($query);
while (list($id,$title,$time,$body) = mysql_fetch_row($result)){
echo “<div class=”mid”><p><b>Title:</b> $title<br /><b>Date:</b> $time<br /><b>Body:</b> $body</p></div>n”; }
?>[/code]

But like I said before, the script above doesn’t work for some reason. I go to the link on the one page and it is just blank on the next page. I’m not sure whats wrong but hopfully you guys can help. ?

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@ConorApr 04.2004 — im not sure i understand what you are trying to do. You want the users to go to page.php?id=23 and it will take them to like the aritcle with the ID of 23 from the database? if so i can help you i achieved this last night.
Copy linkTweet thisAlerts:
@JickauthorApr 04.2004 — Yeah that sounds about right. Plus if you can help me to just get the general stuff working I can modify it to do exactally what I need if required. Thanks for your help! ?
Copy linkTweet thisAlerts:
@ConorApr 04.2004 — ok i used 2 files one to list the links and one to output the articles by themselves.

Heres the links page. It will require modifcation though.

[code=php]
<?
include("header.php");
$un = "";
$pass = "";
$db = "";
$host = "localhost";
$sql = mysql_connect($host, $un, $pass) or die(mysql_error());
mysql_select_db($db, $sql) or die(mysql_error());

if (mysql_ping($sql))
{

$query = "SELECT * FROM blog ORDER BY ID DESC";
$result = mysql_query($query);
$rows = mysql_num_rows($result);

echo' <div class ="content">';
echo' <div class="top">Archive of All Blog Entries</div>';
echo'<h3>Archive of All Previous Blog Entries</h3>';


for ($y = 0; $y < $rows; $y++)
{
$ID = mysql_result($result, $y, 'ID');
$title = mysql_result($result, $y, 'title');
$content = mysql_result($result, $y, 'content');
$date = mysql_result($result, $y, 'date');




echo' <a href="entry.php?ID='.$ID.'">';
echo' '.$title;
echo' </a>';
echo'&nbsp;&nbsp;';
echo' '.$date;

echo'<br>';
}
echo'</div>';
}
else
{
echo'the connection is faulty';
}
include("footer.php");
?>
[/code]



That will make links for everything in my blog table in the format entry.php?ID=2

Then in entry.php i have

[code=php]
<?
$un = "";
$pass = "";
$db = "";
$host = "";
$sql = mysql_connect($host, $un, $pass) or die(mysql_error());
mysql_select_db($db, $sql) or die(mysql_error());
if (mysql_ping($sql))
{
$ID = $_GET['ID'];
$query = "SELECT * FROM blog WHERE ID='".$ID."'";
$result = mysql_query($query);
$title = mysql_result($result, 0, 'title');
$content = mysql_result($result, 0, 'content');
$date = mysql_result($result, 0, 'date');

}
?>
<?include("header.php");?>

<div class="content">
<div class="top"><?echo $title;?></div>

<h3><?echo $title;?></h3>
<br>
<p><?echo $content;?></p>
<br><br>
<i>Added on <?echo $date;?></i>
<?include("footer.php");?>[/code]


hope this is what you needed
Copy linkTweet thisAlerts:
@JickauthorApr 04.2004 — Well I just tried some different stuff incorporating some of your stuff but nothing worked. Anybody have any more ideas?
Copy linkTweet thisAlerts:
@redijediApr 04.2004 — What is the exact error that you are recieving?

I noticed that you used id = '$id' in your SQL statement. If id is a number (as they usually are) the syntax should be id = $id, without the single quotes.
Copy linkTweet thisAlerts:
@JickauthorApr 04.2004 — I figured it out on my own. Thanks anyway for the help. ?
×

Success!

Help @Jick spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...