...

View Full Version : NOOBIE in need of help with $query



jm_0812
06-04-2004, 07:51 AM
Hello...I am trying to quickly learn php...lol... :eek: :eek:

but I am trying to get the results or a recordet to display by the id field that was designiated from a url variable...here i have a hard time explaining this I dont know all the terminlogies of php and mySQL..

the error i am getting is this :

Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in products_detail.php on line 4

originating url:
products_detail.php?item=1

which points to:

<?php
mysql_select_db($database_conn_bank, $conn_bank);
$query_products = "SELECT id, name, info, price, FROM products WHERE id='$_GET['item']' order by id ";
mysql_query($query_products, $conn_bank) or die(mysql_error());
$row_products = mysql_fetch_assoc($products);
$totalRows_products = mysql_num_rows($products);
?>

where "conn_bank" is an outside .php file that makes the connection to the mySQL database

and the table is "products" with columns "id", "name", "info", "price"

on the body of the page i am trying to simply display this querry with these tags:


<h2><?php echo $row_products['name']; ?> <span class="footer">Price = $<?php echo $row_products['price']; ?></span> </h2>
<p> <img src="<?php echo $row_products['graphic']; ?>"/>
<br/>
<?php echo $row_products['info']; ?> </p>
<p>you chose <?php echo $_GET['item']; ?> as your item </p>


from an eager NOOOBIE Thanx ahead for any help that any one can give....

~Jason

missing-score
06-04-2004, 12:05 PM
Ok, theres a couple of things to mention.

1. Make sure you are connecting to the database properly... One of my common mistakes that has me running round in circles is a mispelt user/pass.


2. Now, for your code, try something like this:



<?php
mysql_select_db($database_conn_bank, $conn_bank);

$query_products = "SELECT id, name, info, price, FROM products WHERE id='".$_GET['item']".' order by id ASC";
// You generally need ASC (ascending) or DESC (descending)

$products = mysql_query($query_products) or die(mysql_error());

$row_products = mysql_fetch_assoc($products);

$totalRows_products = mysql_num_rows($products);
?>


Everything else I can see looks ok.

Oh, just one final note, you can use the PHP tags built into VB for displaying php code. Use them as shown below but TAKE OUT the spaces:

[php ][ /php]

And put your code in between those (like they are HTML tags.)

jm_0812
06-04-2004, 08:53 PM
Hello,

Thank you for your help, I am sure that I am asking dumb questions but thats what NOOBIES do...lol....ok I know that the DB connection is ok, because I had it display the fields with a prior query, when I was doing testing.

I added the order by ASC like u had added, but I get this error now:

Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING in products_detail.php on line 4

So I am wondering that if it could be the use of quatation marks. I have read all over the forums here and other ones that you should start with a " and everyother one inside those should use ' ....could this be an issue?


$query_products = "SELECT id, name, info, price, FROM products WHERE id='".$_GET['item']".' order by id ASC";


Thank you again,

Jason

missing-score
06-04-2004, 10:29 PM
That could be a slight typo in my first post, try:


$query_products = "SELECT id, name, info, price, FROM products WHERE id='".$_GET['item']."' order by id ASC";

Which should (hopefully) work.

jm_0812
06-04-2004, 10:52 PM
yeah..I fixed that after looking at it...lol...

Can i use the $_GET to recieve a url variable and use that variable to assign which item # to call the database from?

ie :

URL : products_details.php? item=1

use the $_GET to recieve the 1 from the url

then use that as the WHERE item='1'


I think I explained it well...but I am definatly a NOOBIE so...lol...

Thank you again your help is gold to me,
~Jason

Taylor_1978
06-05-2004, 12:41 AM
That's exactly what $_GET does - it gets the variable... So ?item=1 means $_GET['item'] = 1

jm_0812
06-05-2004, 01:31 AM
awesome....you guys are the best...im sure i will have many questions..as I move along....


thank you,
~Jason

ReadMe.txt
06-05-2004, 01:59 AM
another method for using superglobals within double quoted strings is to wrap in braces. I've not seen this used in many places though.

This would make your code look like this:

$query_products = "SELECT id, name, info, price, FROM products WHERE id='{$_GET['item']}' order by id ASC";

which shouldn't produce a parse error and i reckon is a little fast then ending and restarting a string

missing-score
06-05-2004, 02:04 AM
Interesting, while I dont know exact figures, I would have said that the braces method would be slower, as the string would need to be parsed...

It will work fine... I always use the first method (i know what its called but no idea how to spell it (con...)) as it is easier to read with colour coding.

carl_mcdade
06-05-2004, 07:27 AM
When using the braces interpolation method you have to test the variable. This method does not work with complicated variables from a Class when using the Here doc print function and some others. Code variables like


$db->myXMLreader($_GET['secure'])

gets mangled and misinterpreted. They have to be converted to a simplier form:


$simVar = $db->myXMLreader($_GET['secure']);

So concatenation using "." is probably best.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum