gBase Posted October 4, 2006 Share Posted October 4, 2006 Hey, I'm getting a syntax error with a php file I'm creating to make my database searchable:[code]$query = "SELECT * FROM 'table01' NATURAL JOIN 'ID' WHERE 'table01'.'Name' like '%$qstring%'";[/code]Is this outdated syntax? I'm using PHP 5. Tried googling with little success. Thanks for any help. Quote Link to comment Share on other sites More sharing options...
.josh Posted October 5, 2006 Share Posted October 5, 2006 remove the ' ' from your table and field names. [code]$query = "SELECT * FROM table01 NATURAL JOIN ID WHERE table01.Name like '%$qstring%'";[/code] Quote Link to comment Share on other sites More sharing options...
obsidian Posted October 5, 2006 Share Posted October 5, 2006 [quote author=Crayon Violent link=topic=110534.msg447188#msg447188 date=1160060348]remove the ' ' from your table and field names. [/quote]If you're going to set your columns off with some sort of delimiter, you need to use ticks (`) Quote Link to comment Share on other sites More sharing options...
gBase Posted October 5, 2006 Author Share Posted October 5, 2006 Hmmm...I'm not sure if this is doing what I need it to do...I want the user to be able to search for strings for fields like 'Name,' and this code will query the database for the ID (primary key) of any records with that 'Name', and display the records. Is there a query that would work better? Thanks! :) Quote Link to comment Share on other sites More sharing options...
JayBachatero Posted October 5, 2006 Share Posted October 5, 2006 Try this.[code]<?php$request = mysql_query("SELECT ID, Name FROM tableName WHERE colName LIKE '%$searchTerm%'");echo ' <table>';// Loop through the results.while ($row = mysql_fetch_assoc($request)) echo ' <tr> <td>', $row['ID'], '</td> <td>', $row['Name'], '</td> </tr>';// free the resultsmysql_free_result($request);echo ' </table>';?>[/code] Quote Link to comment Share on other sites More sharing options...
gBase Posted October 5, 2006 Author Share Posted October 5, 2006 Looks good...tried it but I got a parse error:[code]Parse error: syntax error, unexpected '}' on line 22[/code]WTF...There's no } there!! Quote Link to comment Share on other sites More sharing options...
JayBachatero Posted October 5, 2006 Share Posted October 5, 2006 Post that current code that you have. Quote Link to comment Share on other sites More sharing options...
gBase Posted October 5, 2006 Author Share Posted October 5, 2006 [code]<?phpinclude('db_login.php');$connection = mysql_connect($db_host, $db_username, $db_password);$db_select = mysql_select_db($my_database); $db = "my_database"; mysql_select_db($db) or die("Could not select the database '" . $db . "'. Are you sure it exists?");if (!$connection){ die ("Could not connect to the database: <br />". mysql_error());}$request = mysql_query("SELECT ID, Name FROM tableName WHERE colName LIKE '%$searchTerm%'");echo ' <table>';// Loop through the results.while ($row = mysql_fetch_assoc($request)) echo ' <tr> <td>', $row['ID'], '</td> <td>', $row['Name'], '</td> </tr>';// free the resultsmysql_free_result($request);echo ' </table>';}// Close the connectionmysql_close($connection);?>[/code] Quote Link to comment Share on other sites More sharing options...
JayBachatero Posted October 5, 2006 Share Posted October 5, 2006 Om here you go. There was a } after the closing table tag.[code]<?phpinclude('db_login.php');$connection = mysql_connect($db_host, $db_username, $db_password);$db_select = mysql_select_db($my_database); $db = "my_database"; mysql_select_db($db) or die("Could not select the database '" . $db . "'. Are you sure it exists?");if (!$connection) die ("Could not connect to the database: <br />". mysql_error());$request = mysql_query("SELECT ID, Name FROM tableName WHERE colName LIKE '%$searchTerm%'");echo ' <table>';// Loop through the results.while ($row = mysql_fetch_assoc($request)) echo ' <tr> <td>', $row['ID'], '</td> <td>', $row['Name'], '</td> </tr>';// free the resultsmysql_free_result($request);echo ' </table>';// Close the connectionmysql_close($connection);?>[/code] Quote Link to comment Share on other sites More sharing options...
gBase Posted October 5, 2006 Author Share Posted October 5, 2006 Ok...now I'm not getting any more errors, but when I search for a person's name, I get nothing on the next page (at all.) Thanks for your help tho... :) Quote Link to comment Share on other sites More sharing options...
JayBachatero Posted October 5, 2006 Share Posted October 5, 2006 Did you define the $searchTerm var? Need to give it some values. Quote Link to comment Share on other sites More sharing options...
gBase Posted October 5, 2006 Author Share Posted October 5, 2006 Right now I have this:[code]$request = mysql_query("SELECT ID, Name, Organization, Title, Street, City, State, Zip FROM table01 WHERE ID LIKE '%$Name%'");[/code]I want the search string to be someone's Name, and the code to find the IDs (primary key) in my database that correspond with that name and display the available records... Quote Link to comment Share on other sites More sharing options...
JayBachatero Posted October 5, 2006 Share Posted October 5, 2006 Where did you define $Name? Also it's mysql_query with a _ in between. Quote Link to comment Share on other sites More sharing options...
gBase Posted October 5, 2006 Author Share Posted October 5, 2006 I'm not sure how to define it/give it values...I thought searchTerm was where I put the search field for the user to search for. Quote Link to comment Share on other sites More sharing options...
JayBachatero Posted October 5, 2006 Share Posted October 5, 2006 Are you using a form to enter the member to search for? If so then change $Name to $_POST['Name']. Quote Link to comment Share on other sites More sharing options...
gBase Posted October 5, 2006 Author Share Posted October 5, 2006 Yes, I'm using a form which takes the 'Name' string from the user and passes it to a php file that's supposed to find the IDs in the database that correspond to that name and display the records. Thanks, I tried $_POST['Name'] but still am getting a blank result when I try to search for a Name. Quote Link to comment Share on other sites More sharing options...
JayBachatero Posted October 5, 2006 Share Posted October 5, 2006 Can you post the whole script? Quote Link to comment Share on other sites More sharing options...
gBase Posted October 5, 2006 Author Share Posted October 5, 2006 Sure...here's my form:[code]<p><b><font color="blue">Search Records:</b><br /></font><form action="search.php" method="post" >Name:<br /> <input type="text" name="Name" /><br /><input type="submit" value="Submit" /><input type="reset" value="Reset"></form></p>[/code]and here's the php file:[code]<?phpinclude('db_login.php');$connection = mysql_connect($db_host, $db_username, $db_password);$db_select = mysql_select_db($my_database); $db = "my_database"; mysql_select_db($db) or die("Could not select the database '" . $db . "'. Are you sure it exists?");if (!$connection){ die ("Could not connect to the database: <br />". mysql_error());}$request = mysql_query("SELECT ID, Name, Organization, Title, Street, City, State, Zip FROM table01 WHERE ID LIKE '%$_POST['Name']%'");// Loop through the results.while ($result_row = mysql_fetch_row(($result))){ echo 'ID: '.$result_row[0] . '<br />'; echo 'Name: '.$result_row[1] . '<br />'; echo 'Organization: '.$result_row[2]. '<br />'; echo 'Title: '.$result_row[3]. '<br />'; echo 'Street: '.$result_row[4] . '<br />'; echo 'City: '.$result_row[5]. '<br />'; echo 'State: '.$result_row[6]. '<br />'; echo 'Zip: '.$result_row[7]. '<br /><br />';}// free the resultsmysql_free_result($request);// Close the connectionmysql_close($connection);?>[/code] Quote Link to comment Share on other sites More sharing options...
JayBachatero Posted October 5, 2006 Share Posted October 5, 2006 Ok I see the problem. You are looking for the name on the ID column. Change the "WHERE ID LIKE" to "WHERE Name LIKE". Also change '%$_POST['Name']%' to '%$_POST[name]%'. Quote Link to comment Share on other sites More sharing options...
gBase Posted October 5, 2006 Author Share Posted October 5, 2006 Ok...tried that, searched again and got[code]Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING on line 21[/code] Quote Link to comment Share on other sites More sharing options...
JayBachatero Posted October 5, 2006 Share Posted October 5, 2006 Ok for the [code=php:0]while ($row = mysql_fetch_row($result))[/code]. It should be just one set of mysql_fetch_row() not two mysql_fetch_row(()) Quote Link to comment Share on other sites More sharing options...
gBase Posted October 5, 2006 Author Share Posted October 5, 2006 lol...good catch, took those out but I'm still getting the same error...??? Quote Link to comment Share on other sites More sharing options...
JayBachatero Posted October 5, 2006 Share Posted October 5, 2006 You changed this line [code=php:0]$request = mysql_query("SELECT ID, Name, Organization, Title, Street, City, State, Zip FROM table01 WHERE ID LIKE '%$_POST['Name']%'");[/code] to this [code=php:0]$request = mysql_query("SELECT ID, Name, Organization, Title, Street, City, State, Zip FROM table01 WHERE ID LIKE '%$_POST[Name]%'");[/code]? Quote Link to comment Share on other sites More sharing options...
gBase Posted October 5, 2006 Author Share Posted October 5, 2006 Ok, cool...now it works (sort of.) it displays the correct fields (ID, Name, Organization, etc.) but with no values attached to them, not matter what name I search for. Any thoughts?In other words, I get:ID:Name:Organization:Title:Street:City:State:Zip: Quote Link to comment Share on other sites More sharing options...
JayBachatero Posted October 5, 2006 Share Posted October 5, 2006 while ($row = mysql_fetch_row($result)) -> while ($row = mysql_fetch_row($request)) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.