Jump to content

Single Record php


TheCatapult

Recommended Posts

Hi, Here's my current code:

<?$username="";$password="";$database="";mysql_connect("",$username,$password);@mysql_select_db($database) or die( "Unable to select database");$query="$_GET['id']";$query="SELECT * FROM news WHERE id='$id'  ORDER BY id DESC LIMIT 3";$result=mysql_query($query);$num=mysql_numrows($result);mysql_close();?><?$i=0;while ($i < $num) {$bck = ($i % 2 == 0 ) ? " style=\"background:#A3FFA3\"" : " style=\"background: #B7B7E8;\"";$id=mysql_result($result,$i,"id");$title=mysql_result($result,$i,"title");$content=mysql_result($result,$i,"content");$poster=mysql_result($result,$i,"poster");?><div id="div" <? echo $bck ?>>	   <? echo $id;?>	    <? echo '<a href="http://www.mywebsite.com/index.php?id=' . $id . '">' . $title . '</a> <br/>'; ?><? echo $content; ?> <br> by <? echo $poster;?></div><?$i++;}			    ?>

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /srv/disk4/741839/www/catapultphpmysql.eu.pn/testwebsite/index.html on line 133 Okay, it's expecting a T_VARIABLE? What variable?

Link to comment
Share on other sites

Okay, I think it's really time for you to go back to the drawing board. You need to understand how to write and what to write, before you can put anything together, because you are clearly just throwing anything at the wall and hoping that it will stick. Review one of my previous posts. Reread the all the PHP tutorials, and then be able to articulate, in written language first, what you want your code to do, step by step (pseudo-code). Once you have presented that or are confident in your logic, then proceed to write it out in code, verifying each step as you go along through debugging and outputting of expected values.

Link to comment
Share on other sites

Okay, it's expecting a T_VARIABLE? What variable?
PHP doesn't understand or care what your code is trying to do. It's not going to tell you that you need to use a specific variable in a specific place. It's telling you that your code is not formatted correctly and it can't even understand the structure of it because it doesn't follow the rules. Specifically, it's telling you that it found a T_ENCAPSED_AND_WHITESPACE token in a place where it shouldn't be. That token is the name for when you have a variable inside a string, an encapsulated variable. The error is on this line: $query="$_GET['id']"; You trying to assign the value in $_GET['id'] to the variable $query, but why did you put quotes around the part on the right? If you're referring to a variable then just write the variable, it doesn't need to go in quotes. If you want to build a string that contains variables then the way to do it (and the reason why the above is wrong) is explained here: http://www.php.net/m....string.parsing Other than the syntax error, look at these two lines: $query="$_GET['id']";$query="SELECT * FROM news WHERE id='$id' ORDER BY id DESC LIMIT 3"; You're using a variable called $id in the query, where do you set the value of $id? It's not being set.
Link to comment
Share on other sites

Hi, Here's my current code:(CODE) Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /srv/disk4/741839/www/catapultphpmysql.eu.pn/testwebsite/index.html on line 133 Okay, it's expecting a T_VARIABLE? What variable?
This is the reason I told you to go back to the PHP tutorial and re-learn it. This is a simple case of a syntax error and until you're able to solve that on your own you can't continue.
Link to comment
Share on other sites

please post your updated code, when you make significant changes to them

I really don't know where to start. I have read about variables and some operators.
you must have read the ALL basic part of the tutorial of w3schools. It is not the complete one but easy to pick up if you need more further information read http://php.net/language.basic basics. that is the classic and compelete reference and please follow the links and read them which are being given to you already. they are giving the pointers to follow it. it may be slowdown your development progress which you are in but it will definitly help you in long term. dont avoid it.
Link to comment
Share on other sites

that's the whole point of variable, you can call them anything, and you can make them anything. Programming are meant for people to express themselves in a syntax that a computer can understand and process. Seriously, just think about what are you trying to do and spell it out for yourself. You want to get news articles from a database specified by a perticular id. Where do you get the id? You've determined it will be passed into the script via a querystring. It will be the value of a key/param called id. So, in your code, you will get the id from $_GET['id'] (the $_GET super global array); To make it meaningful, you would want to save it in a variable called $id. Then you need to use that $id in a query to get the news content. So, you make a variable called $query, to store the string value of the query in, including the id just assigned to the variable $id. Next is to execute the query, and in turn get a result set (if the query was successful). It would make sense to save that in a variable called $result. Then you can loop over the result set in a couple of ways. One example is here.http://www.w3schools.com/php/php_mysql_select.aspOr you can do it the current way, just as long as you know what each implementation is doing. And then In the loop you can use a counter to assign different styles to the output. This is why we keep saying to go back and read the tutorials. You should be able to write out your code in simple steps and accurately translate pseudo-code into real code, while you are learning. Throwing a bunch of code together on the page without any context or understanding is a very futile exercise, as you have experienced.

Link to comment
Share on other sites

Would that be like: <?$username="";$password="";$database=""; mysql_connect("",$username,$password);@mysql_select_db($database) or die( "Unable to select database");$id="$_GET['id']";$query="SELECT * FROM news WHERE id='$id' ORDER BY id DESC LIMIT 3";$result=mysql_query($query); $num=mysql_numrows($result);mysql_close(); ?>Ooops, if I would add ' ' in [$GET[id], it gets error. I really don't know what to do. :facepalm:Thanks for understanding me!

Link to comment
Share on other sites

read the posts! read the tutorials! why are you making a string out of $_GET['id']?

You trying to assign the value in $_GET['id'] to the variable $query, but why did you put quotes around the part on the right? If you're referring to a variable then just write the variable, it doesn't need to go in quotes.
$id = $_GET['id'];

If you don't understand the basics, nothing anyone is going to tell you will make sense.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...