Forum Moderators: coopster

Message Too Old, No Replies

Get a record by url?

First time I try this but I got parse error

         

fredisaksen

9:47 am on Jan 13, 2006 (gmt 0)

10+ Year Member



Hi, I'm a norwegian guy, php novise.

I'm making a website with articles stored in mysql.

I want to get a certain article by the var in the url like this:

[website.com...]

<?php
$host="#*$!.no";
$user="#*$!";
$password="xxx";
$link = mysql_connect($host,$user,$password);
$db = "xxx";
$tabell = "artikkel";

mysql_select_db($db, $link);

$result = mysql_query("SELECT * FROM $tabell WHERE id = '$_GET[id]'", $link);
$row = mysql_fetch_array($result);

echo '<span class="tit18">'.$row['title'].'</span><br/><br/>';
echo '<span class="ingr">'.$row['intro'].'</span><br/>';
echo $row['text'].'<br/><br/><br/>';

?>

But I got parse error "unexpected T_VARIABLE" in the line
"$result = mysql_query("SELECT * FROM $tabell WHERE id = '$_GET[id]'", $link);"

This is the phrase I never use before:
"SELECT * FROM $tabell WHERE id = '$_GET[id]'"

What do I do wrong?

The variables are ok, I can get data by list the content.

tomda

10:08 am on Jan 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Because you are novice, let me drop few remarks:

1/ You may want to use something else that "id". It is known that search engine do not like URL.php?id=1. Change "id" to "article" or a more appropriate keywords (URL.php?article=1)

2/ Always check your variable before doing any SQL query.

PREG_MATCH FUNCTION (0-9 ONLY)
******************************
function check1($var){if(!preg_match("/[^0-9]/",$var)) {return TRUE;} else {return FALSE;}}

CHECK VARIABLE (MAKE SURE VARIABLE IS SET, MAKE SURE IT IS AN INTEGER, IF NOT GIVE A DEFAULT VALUE)
**************
if(isset($_GET["article"])) {$article=$_GET["article"];
if(!check1($article)) {$article="1";} } else {$article="1";}

3/ The way you wrote your query is new to me. It may be correct but it seems to me that you connect to the db for every query.

$result = mysql_query("SELECT * FROM $tabell WHERE id = '$_GET[id]'", $link);
You should connect to the db once only. Place this at the top of the page
$connect=mysql_connect("$host","$user","$password") or die ("Unable to connect server");
$db_select=mysql_select_db("$db") or die ("Unable to get table");

4/ Then, you query will look like:

$query = "SELECT * FROM $tabell WHERE id='$article'";
$result=mysql_query($query) or die ("Fail to get article");
$pres=mysql_fetch_array($result);

If you get an error message, just use "echo $query" to see the query. But it should be OK.

fredisaksen

10:38 am on Jan 13, 2006 (gmt 0)

10+ Year Member



Thank you, I followed your advises, but now I got the parse error in this line:

$query = "SELECT * FROM $tabell WHERE id='$article'";

$host="#*$!.no";
$user="xxx";
$password="xxx";
$link = mysql_connect($host,$user,$password) or die ("Ikke tilgang til mysql-server!");
$db = "xxx";
$tabell = "artikkel";

function check1($var){if(!preg_match("/[^0-9]/",$var)) {return TRUE;} else {return FALSE;}}
if(isset($_GET["article"])) {$article=$_GET["pic"];
if(!check1($pic)) {$pic="";} } else {$article="1";}

mysql_select_db($db, $link) or die ("Ikke tilgang til database");

$query = "SELECT * FROM $tabell WHERE id='$article'";
$result=mysql_query($query) or die ("Fant ikke artikkel");
$row = mysql_fetch_array($result);

 echo '<span class="tit18">'.$row['title'].'</span><br/><br/>';
echo '<span class="ingr">'.$row['intro'].'</span><br/>';
echo $row['text'].'<br/><br/><br/>';

tomda

10:54 am on Jan 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try this! It should work now!
Note that the URL should be [website.com...]
$user="#*$!";
$password="#*$!";
$db = "xxx";
$tabell = "artikkel";

// MYSQL CONNECT
$connect=mysql_connect("$host","$user","$password") or die ("Ikke tilgang til mysql-server!");
$db_select=mysql_select_db("$db") or die ("Ikke tilgang til database");

// FUNCTION
function check1($var){if(!preg_match("/[^0-9]/",$var)) {return TRUE;} else {return FALSE;}}

// CHECK VARIABLE
if(isset($_GET["article"])) {$article=$_GET["article"];
if(!check1($article)) {$article="1";} } else {$article="1";}

$query = "SELECT title, intro, text FROM $tabell WHERE id='$article'";

// SHOW QUERY - IMPORTANT
echo $query;

$result=mysql_query($query) or die ("Fant ikke artikkel");
$row = mysql_fetch_array($result);

echo '<span class="tit18">'.$row['title'].'</span><br/><br/><span class="ingr">'.$row['intro'].'</span><br/>'.$row['text'].'<br/><br/><br/>';

Tip: use echo query to get the query. This will help you to find the error. Can't find it. The ultime method is to copy the query and paste it in MySQLAdmin and get the error message.

Last tip: you may want to rename the ID field in your database to ARTICLE_ID for better comprehension in case you have multiple table (USER_ID, ARTICLE_ID, PRODUCT_ID). In that case, your query will be $query = "SELECT title, intro, text FROM $tabell WHERE article_id='$article'";

[edited by: tomda at 11:00 am (utc) on Jan. 13, 2006]

tomda

10:59 am on Jan 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oups! I forgot to tell you
Welcome to WebmasterWorld Fredisaksen!

The week is over. I'll be back on Monday.

fredisaksen

12:00 pm on Jan 13, 2006 (gmt 0)

10+ Year Member



After eight hours of frustration I got the
answer of this mystic error:

I have recently began using Dreamweaver 8
When I finally opened this file in BBEdit
I noticed that DW had made a char like an upwards
arrow first in two lines. These were invisible in
DW's codeviewer!

Aaarrrg!