Page 1 of 1

[SOLVED] detail record from URL

Posted: Thu Dec 04, 2003 12:05 pm
by hairyjim
HI,

Sorry Im new to this and I have a very basic question (No doubt!)

I have created a news database which holds news articles, I have created a script which returns a list of all articles and turns the title into a URL click through to read more.

Code: Select all

<a href="news_detail.php?id=".$data&#1111;'sid']."">".$data&#1111;'title']."</a>
So the URL looks similar to /news_detail.php?id=96

I want to then query the same database to bring back this record with that id number. All I get is a blank page though, what am I doing wrong?

I have the following code on the recipient page:

Code: Select all

<?php
$id = $_post&#1111;'id'];
$dbconn = mysql_connect("localhost", "username", "password");
$result = mysql_select_db("database", $dbconn);
if ( $result == false)
&#123;
		echo mysql_error();
&#125; else &#123;
		$sql = "SELECT title, maintext, cdate FROM impro_news WHERE sid = '$id'";
		$result = mysql_query( $sql  );
		if ( $result != false )
		&#123;
					$data = mysql_fetch_assoc( $result );
					echo "<table width="50%" border="0" cellspacing="0" cellpadding="0">";
  					echo "<tr>" ;
    				echo "<td width="75%">$data&#1111;title]</td>";
    				echo "<td width="25%">$data&#1111;cdate]</td>";
  					echo "</tr>";
  					echo "<tr>";
    				echo "<td colspan="2">$data&#1111;maintext]</td>";
  					echo "</tr>";
					echo "</table>";
				
		&#125; else &#123;
				echo mysql_error();
		&#125;
&#125;

?>

Posted: Thu Dec 04, 2003 12:08 pm
by Nay
1, I'm sure it's $_GET['id'] rather than $_POST['id']

and

2, mysql_query() needs two arguements, one the query and the other the link to the database. It should be mysql_query($sql, $dbconn);

3, Arrays with text for the key should be used as $data['title'] not $data[title].

4, you can do mysql_query() or die(mysql_error()); instead of needing to have an extra line.

-Nay

Posted: Thu Dec 04, 2003 12:21 pm
by Chambrln
All of what Nay said is correct, however, you can get away with only passing the SQL query to mysql_query() and not the dbconn. It may be sloppy coding, but it works.

Posted: Fri Dec 05, 2003 5:47 am
by hairyjim
I ammended the code as suggested. But I still get a blank page returned, any other thoughts on this?

Code: Select all

<?php
$id = $_get&#1111;'id'];

$dbconn = mysql_connect("localhost", "username", "password");
$result = mysql_select_db("improvision", $dbconn);
if ( $result == false)
&#123;
		echo mysql_error();
&#125; else &#123;
		$sql = "SELECT sid, title, maintext, cdate FROM impro_news WHERE sid = '$id'";
		$result = mysql_query( $sql, $dbconn  );
		if ( $result != false )
		&#123;
					$data = mysql_fetch_assoc( $result );
					echo "<table width="50%" border="0" cellspacing="0" cellpadding="0">";
  					echo "<tr>" ;
    				echo "<td width="75%">$data&#1111;'title']</td>";
    				echo "<td width="25%">$data&#1111;cdate]</td>";
  					echo "</tr>";
  					echo "<tr>";
    				echo "<td colspan="2">$data&#1111;'maintext']</td>";
  					echo "</tr>";
					echo "</table>";
				
		&#125; else &#123;
				echo mysql_error();
		&#125;
&#125;

?>

Posted: Fri Dec 05, 2003 5:53 am
by JayBird
not 100% sure, but i think this need to be uppercase

Code: Select all

$id = $_GET['id'];

Posted: Fri Dec 05, 2003 6:07 am
by hairyjim
Orhhh man - I did not realise it is case sensitive!

To further this post, I also noticed that if I was to add ' ' round my field calls they do not show! So is what you said true Nay?
Arrays with text for the key should be used as $data['title'] not $data[title].
I only ask because I want to get things right as I am a beginner and not get the bad/wrong habits :wink:

Posted: Fri Dec 05, 2003 6:14 am
by JayBird
weird, you should have to use ' ' around your keys

hmmm.....

Posted: Fri Dec 05, 2003 9:39 am
by hairyjim
I just get a blank record if I put '' round my fields.

Posted: Fri Dec 05, 2003 9:58 am
by Weirdan
2Bech
You are absolutely correct.
PHP manual wrote: Variables in PHP are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive.
on the other hand, function names isn't case-sensitive:
PHP manual wrote: Function names are case-insensitive, though it is usually good form to call functions as they appear in their declaration.