Jump to content

syntax error


gBase

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.
Link to comment
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!  :)
Link to comment
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]
Link to comment
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]
Link to comment
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]
Link to comment
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...
Link to comment
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.
Link to comment
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]
Link to comment
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]?
Link to comment
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:
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.