Forum Moderators: coopster
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.
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.
$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/>';
// 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]