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 7 of 7
  1. #1
    New Coder
    Join Date
    Apr 2005
    Posts
    55
    Thanks
    0
    Thanked 0 Times in 0 Posts

    searching mysql DB using PHP

    I'm using php to search a mysql db. The code is as follows:
    <?php
    require_once("C:\Server\Apache2\mysql_connect_york.inc");

    $typ=$_POST['type'];
    $code=$_POST['pcode'];
    $cit=$_POST['city'];
    $count=$_POST['county'];
    $ncit=$_POST['ncity'];


    $query_result=mysql_query( 'select location_id, owner_id, loc_name, type, address_1, address_2, city, county, post_code from location where type = $typ');

    while($row=mysql_fetch_array($query_result))
    {
    echo "<tr>";
    echo "<td>.$row['location_id'].</td>"; echo "<td>.$row['owner_id'].</td>";
    echo "<td>.$row['loc_name'].</td>";
    echo "<td>.$row['type'].</td>";
    echo "<td>.$row['address_1'].</td>";
    echo "<td>.$row['address_2'].</td>";
    echo "<td>.$row['city'].</td>";
    echo "<td>.$row['county'].</td>";
    echo "<td>.$row['post_code'].</td>";
    echo "</tr>";


    mysql_free_result ($query_result);
    mysql_close();


    ?>

    the code is falling down at the highlighted echo with he error:
    Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in C:\Server\Apache2\local-html\search_york.php on line 230

    any ideas on the problem? obviously it some sort of data mismatch , but how can i put it right?
    Cheers
    Steve

  • #2
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try either:

    PHP Code:
    echo "<td>".$row['location_id']."</td>";
    // With extra quotes
    // or...
    echo "<td>{$row['location_id']}</td>";
    // putting the variable in curly brackets 
    you will need to change all of your <td> elements to look like this...
    eg:

    echo "<td>".$row['owner_id']."</td>";

    and so on...

  • #3
    New Coder
    Join Date
    Apr 2005
    Posts
    55
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi, thanks for the reply. I'd already tried the 1st way, but that didn't wrk, it just bought up all the echos onto the screen. the second way seems to work, although now i get the same error on a line which contains
    </hmtl>!! what do you think?
    cheers
    steve

  • #4
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i think you may have forgotten to close your while loop too... try this:

    PHP Code:
    <?php
    require_once("C:\Server\Apache2\mysql_connect_york.inc");

    $typ=$_POST['type'];
    $code=$_POST['pcode'];
    $cit=$_POST['city'];
    $count=$_POST['county'];
    $ncit=$_POST['ncity'];


    $query_result=mysql_query'select location_id, owner_id, loc_name, type, address_1, address_2, city, county, post_code from location where type = $typ');

    while(
    $row=mysql_fetch_array($query_result))
    {
    echo 
    "<tr>";
    echo 
    "<td>".$row['location_id']."</td>"
    echo 
    "<td>".$row['owner_id']."</td>";
    echo 
    "<td>".$row['loc_name']."</td>";
    echo 
    "<td>".$row['type']."</td>";
    echo 
    "<td>".$row['address_1']."</td>";
    echo 
    "<td>".$row['address_2']."</td>";
    echo 
    "<td>".$row['city']."</td>";
    echo 
    "<td>".$row['county']."</td>";
    echo 
    "<td>".$row['post_code']."</td>";
    echo 
    "</tr>";
    }

    mysql_free_result ($query_result);
    mysql_close();

    ?>
    also, if the $_POST['type'] ($typ) variable is a string and not a number, you should put it in quotes in the query, eg:

    Code:
    $query_result = mysql_query("select location_id, owner_id, loc_name, type, address_1, address_2, city, county, post_code from location where type = '".$typ."'");
    Edit:
    You could use mysql_fetch_assoc() rather than mysql_fetch_array(). mysql_fetch_assoc() only returns associative array values (eg: $row['name']) whereas fetch_array returns associative ($row['name']) and indexed ($row[0]). mysql_fetch_assoc() is more efficient if you are only using associative array values. You can replace the function name 'mysql_fetch_array' with 'mysql_fetch_assoc' in the code above and it will still work the same, just be slightly faster.

  • #5
    New Coder
    Join Date
    Apr 2005
    Posts
    55
    Thanks
    0
    Thanked 0 Times in 0 Posts
    first thing, not closing the loop...doh!! got it working perfectly now, thanks a lot for your time mate
    steve

  • #6
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    cool. be sure to try out the mysql_fetch_assoc() method i mentioned.

  • #7
    New Coder
    Join Date
    Apr 2005
    Posts
    55
    Thanks
    0
    Thanked 0 Times in 0 Posts
    got it in there now
    cheers


  •  

    Posting Permissions

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