Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SELECT

Author  Topic 

tascam424
Starting Member

2 Posts

Posted - 2012-04-06 : 12:14:23
Hi, firstly i am an absolute novice, so please be patient, i'm trying to produce a search function for a web page from a db. The database is one of names, addresses etc etc .. so firstly i would like to pull all the cities from the database, which i have done like so

// Formulate Query
// This is the best way to perform a SQL query
// For more examples, see mysql_real_escape_string()
$query = sprintf("SELECT DISTINCT Cities FROM PBM");

// Perform Query
$result = mysql_query($query);

// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}

echo "<br />";
echo "<table border='0'>";

// Use result
// Attempting to print $result won't allow access to information in the resource
// One of the mysql result functions must be used
// See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc.
while ($row = mysql_fetch_assoc($result))
{
echo "<tr><td>";
echo "</td><td align='left'>";
echo $row['Cities'];
echo "</td></tr>";
}

echo "</table>";

// Free the resources associated with the result set
// This is done automatically at the end of the script
mysql_free_result($result);


So this works and produces a nice list of all the cities with all duplicates removed .. unfortunately this is as far as i have got as i am an absolute novice.

What i need is to firstly produce the above results in a selectable drop down, instead of a table.

Then when a City is selected from the drop down, it will return results of all Names and adresses etc .. i hope i have made myself clear ..

Thanks
// Formulate Query // This is the best way to perform a SQL query // For more examples, see mysql_real_escape_string() $query = sprintf("SELECT DISTINCT Cities FROM PBM"); // Perform Query $result = mysql_query($query); // Check result // This shows the actual query sent to MySQL, and the error. Useful for debugging. if (!$result) { $message = 'Invalid query: ' . mysql_error() . "\n"; $message .= 'Whole query: ' . $query; die($message); } echo "<br />"; echo "<table border='0'>"; // Use result // Attempting to print $result won't allow access to information in the resource // One of the mysql result functions must be used // See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc. while ($row = mysql_fetch_assoc($result)) { echo "<tr><td>"; echo "</td><td align='left'>"; echo $row['Cities']; echo "</td></tr>"; } echo "</table>"; // Free the resources associated with the result set // This is done automatically at the end of the script mysql_free_result($result);
// Formulate Query // This is the best way to perform a SQL query // For more examples, see mysql_real_escape_string() $query = sprintf("SELECT DISTINCT Cities FROM PBM"); // Perform Query $result = mysql_query($query); // Check result // This shows the actual query sent to MySQL, and the error. Useful for debugging. if (!$result) { $message = 'Invalid query: ' . mysql_error() . "\n"; $message .= 'Whole query: ' . $query; die($message); } echo "<br />"; echo "<table border='0'>"; // Use result // Attempting to print $result won't allow access to information in the resource // One of the mysql result functions must be used // See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc. while ($row = mysql_fetch_assoc($result)) { echo "<tr><td>"; echo "</td><td align='left'>"; echo $row['Cities']; echo "</td></tr>"; } echo "</table>"; // Free the resources associated with the result set // This is done automatically at the end of the script mysql_free_result($result);

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-04-06 : 12:39:02
SQLTeam is on Microsoft SQL Server.

for MySQL try
http://forums.mysql.com/
http://www.dbforums.com/mysql/


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

tascam424
Starting Member

2 Posts

Posted - 2012-04-06 : 14:48:03
Whoops .. Thank you for your direction
Go to Top of Page
   

- Advertisement -