Jump to content


Photo

syntax error


  • Please log in to reply
28 replies to this topic

#1 gBase

gBase
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 04 October 2006 - 11:05 PM

Hey, I'm getting a syntax error with a php file I'm creating to make my database searchable:

$query = "SELECT * FROM 'table01' NATURAL JOIN 'ID' WHERE 'table01'.'Name' like '%$qstring%'";

Is this outdated syntax?  I'm using PHP 5.  Tried googling with little success.  Thanks for any help.

#2 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 05 October 2006 - 02:59 PM

remove the ' ' from your table and field names.

$query = "SELECT * FROM table01 NATURAL JOIN ID WHERE table01.Name like '%$qstring%'";

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 05 October 2006 - 03:19 PM

remove the ' ' from your table and field names.


If you're going to set your columns off with some sort of delimiter, you need to use ticks (`)
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#4 gBase

gBase
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 05 October 2006 - 05:52 PM

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!  :)

#5 JayBachatero

JayBachatero
  • Members
  • PipPipPip
  • Advanced Member
  • 296 posts
  • LocationQueens NY

Posted 05 October 2006 - 06:00 PM

Try this.
<?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 results
mysql_free_result($request);

echo '
	</table>';
?>

JayBachatero
SMF Developer && Converter Specialist

#6 gBase

gBase
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 05 October 2006 - 06:08 PM

Looks good...tried it but I got a parse error:

Parse error: syntax error, unexpected '}' on line 22

WTF...There's no } there!!

#7 JayBachatero

JayBachatero
  • Members
  • PipPipPip
  • Advanced Member
  • 296 posts
  • LocationQueens NY

Posted 05 October 2006 - 06:15 PM

Post that current code that you have.
JayBachatero
SMF Developer && Converter Specialist

#8 gBase

gBase
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 05 October 2006 - 06:48 PM

<?php
include('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 results
mysql_free_result($request);

echo '
	</table>';
}
// Close the connection
mysql_close($connection);
?>


#9 JayBachatero

JayBachatero
  • Members
  • PipPipPip
  • Advanced Member
  • 296 posts
  • LocationQueens NY

Posted 05 October 2006 - 06:50 PM

Om here you go.  There was a } after the closing table tag.
<?php
include('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 results
mysql_free_result($request);

echo '
	</table>';

// Close the connection
mysql_close($connection);
?>

JayBachatero
SMF Developer && Converter Specialist

#10 gBase

gBase
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 05 October 2006 - 07:00 PM

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... :)

#11 JayBachatero

JayBachatero
  • Members
  • PipPipPip
  • Advanced Member
  • 296 posts
  • LocationQueens NY

Posted 05 October 2006 - 07:04 PM

Did you define the $searchTerm var?  Need to give it some values.
JayBachatero
SMF Developer && Converter Specialist

#12 gBase

gBase
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 05 October 2006 - 07:13 PM

Right now I have this:

$request = mysql_query("SELECT ID, Name, Organization, Title, Street, City, State, Zip FROM table01 WHERE ID LIKE '%$Name%'");

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...

#13 JayBachatero

JayBachatero
  • Members
  • PipPipPip
  • Advanced Member
  • 296 posts
  • LocationQueens NY

Posted 05 October 2006 - 07:15 PM

Where did you define $Name?  Also it's mysql_query with a _ in between.
JayBachatero
SMF Developer && Converter Specialist

#14 gBase

gBase
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 05 October 2006 - 07:18 PM

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.

#15 JayBachatero

JayBachatero
  • Members
  • PipPipPip
  • Advanced Member
  • 296 posts
  • LocationQueens NY

Posted 05 October 2006 - 07:20 PM

Are you using a form to enter the member to search for?  If so then change $Name to $_POST['Name'].
JayBachatero
SMF Developer && Converter Specialist

#16 gBase

gBase
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 05 October 2006 - 07:27 PM

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.

#17 JayBachatero

JayBachatero
  • Members
  • PipPipPip
  • Advanced Member
  • 296 posts
  • LocationQueens NY

Posted 05 October 2006 - 07:30 PM

Can you post the whole script?
JayBachatero
SMF Developer && Converter Specialist

#18 gBase

gBase
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 05 October 2006 - 07:35 PM

Sure...here's my form:

<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>


and here's the php file:

<?php
include('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 results
mysql_free_result($request);


// Close the connection
mysql_close($connection);
?>


#19 JayBachatero

JayBachatero
  • Members
  • PipPipPip
  • Advanced Member
  • 296 posts
  • LocationQueens NY

Posted 05 October 2006 - 07:45 PM

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]%'.
JayBachatero
SMF Developer && Converter Specialist

#20 gBase

gBase
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 05 October 2006 - 07:54 PM

Ok...tried that, searched again and got

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING on line 21





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users