Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Jun 2004
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question NOOBIE in need of help with $query

    Hello...I am trying to quickly learn php...lol...

    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

  • #2
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 Code:
    <?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.)

  • #3
    New Coder
    Join Date
    Jun 2004
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #4
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That could be a slight typo in my first post, try:

    Code:
     $query_products = "SELECT id, name, info, price, FROM products WHERE id='".$_GET['item']."' order by id ASC";
    Which should (hopefully) work.

  • #5
    New Coder
    Join Date
    Jun 2004
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #6
    Regular Coder
    Join Date
    Jun 2003
    Location
    Australia
    Posts
    528
    Thanks
    8
    Thanked 8 Times in 8 Posts
    That's exactly what $_GET does - it gets the variable... So ?item=1 means $_GET['item'] = 1

  • #7
    New Coder
    Join Date
    Jun 2004
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    awesome....you guys are the best...im sure i will have many questions..as I move along....


    thank you,
    ~Jason

  • #8
    Regular Coder
    Join Date
    Jun 2002
    Location
    Sheffield, UK
    Posts
    552
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:
    PHP Code:
     $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
    "To be successful in IT you don't need to know everything - just where to find it in under 30 seconds"

    (Me Me Me Me Me Me Me Me Me)

  • #9
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #10
    Regular Coder
    Join Date
    May 2004
    Location
    sweden
    Posts
    236
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

    Code:
    $db->myXMLreader($_GET['secure'])
    gets mangled and misinterpreted. They have to be converted to a simplier form:

    Code:
    $simVar = $db->myXMLreader($_GET['secure']);
    So concatenation using "." is probably best.
    Carl McDade
    _____________
    Hiveminds Magazine
    for web publisher and community builders
    eRuby Tutorials


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •