Jump to content

syntax error


gBase

Recommended Posts

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
https://forums.phpfreaks.com/topic/23027-syntax-error/#findComment-104402
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
https://forums.phpfreaks.com/topic/23027-syntax-error/#findComment-104410
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
https://forums.phpfreaks.com/topic/23027-syntax-error/#findComment-104459
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
https://forums.phpfreaks.com/topic/23027-syntax-error/#findComment-104463
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
https://forums.phpfreaks.com/topic/23027-syntax-error/#findComment-104483
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
https://forums.phpfreaks.com/topic/23027-syntax-error/#findComment-104503
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
https://forums.phpfreaks.com/topic/23027-syntax-error/#findComment-104512
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
https://forums.phpfreaks.com/topic/23027-syntax-error/#findComment-104546
Share on other sites

Archived

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

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