Jump to content

Archived

This topic is now archived and is closed to further replies.

gBase

syntax error

Recommended Posts

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.

Share this post


Link to post
Share on other sites
remove the ' ' from your table and field names.

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

Share this post


Link to post
Share on other sites
[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 (`)

Share this post


Link to post
Share on other sites
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!  :)

Share this post


Link to post
Share on other sites
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 results
mysql_free_result($request);

echo '
</table>';
?>
[/code]

Share this post


Link to post
Share on other sites
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!!

Share this post


Link to post
Share on other sites
[code]
<?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);
?>
[/code]

Share this post


Link to post
Share on other sites
Om here you go.  There was a } after the closing table tag.
[code]
<?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);
?>
[/code]

Share this post


Link to post
Share on other sites
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... :)

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Are you using a form to enter the member to search for?  If so then change $Name to $_POST['Name'].

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]
<?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);
?>
[/code]

Share this post


Link to post
Share on other sites
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]%'.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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(())

Share this post


Link to post
Share on other sites
lol...good catch, took those out but I'm still getting the same error...
???

Share this post


Link to post
Share on other sites
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]?

Share this post


Link to post
Share on other sites
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:

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.