You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何编写SQL查询转'Hello World'为'Hello-World'及URL重写问题求助

Hey there, let's break down and solve your URL rewriting and database query issues one by one!

1. Fixing the Double-Word URL Rewrite & Query Problem

The root issue here is that your dest_name field stores values with spaces (like "New York"), but you're trying to query it using hyphen-separated strings (like "New-York") generated by your PHP function—so they don't match. Here are two solid solutions:

Option 1: Convert Hyphens Back to Spaces in PHP

When you capture the hyphenated location from the URL, turn it back into a space-separated string before querying the database. This plays nicely with your existing dest_name values and preserves index usage (critical for performance):

// Get the hyphenated location from your URL (e.g., "New-York")
$url_location = $_GET['location'];

// Convert hyphens back to spaces to match the database format
$db_location = str_replace('-', ' ', $url_location);

// Always use prepared statements to avoid SQL injection!
$stmt = $pdo->prepare("SELECT * FROM your_table WHERE dest_name = ?");
$stmt->execute([$db_location]);
$result = $stmt->fetch();

Option 2: Modify the MySQL Query to Replace Spaces with Hyphens

If you prefer handling it at the database level, use MySQL's REPLACE() function to transform the dest_name value on the fly. Note: This will bypass any indexes on dest_name, so only use this if your dataset is small:

SELECT * FROM your_table 
WHERE REPLACE(dest_name, ' ', '-') = 'New-York';

For larger datasets, create a stored generated column that auto-updates with the hyphenated version, then add an index to it:

-- Add the generated column
ALTER TABLE your_table 
ADD COLUMN dest_name_slug VARCHAR(255) AS (REPLACE(dest_name, ' ', '-')) STORED;

-- Add an index for fast queries
CREATE INDEX idx_dest_slug ON your_table(dest_name_slug);

Now you can query efficiently with:

SELECT * FROM your_table WHERE dest_name_slug = 'New-York';
2. Converting "Hello World" to "Hello-World" in MySQL

It's straightforward with the REPLACE() function—just target spaces and swap them with hyphens:

-- Test the conversion
SELECT REPLACE('Hello World', ' ', '-'); -- Outputs: Hello-World

-- If you want to update existing data into a new column
UPDATE your_table 
SET dest_name_slug = REPLACE(dest_name, ' ', '-');

Again, using a generated column (like above) is better because it auto-syncs when dest_name changes—no manual updates needed.

3. Quick URL Rewrite Rule Check

Make sure your web server's rewrite rules can capture hyphenated strings correctly. For example:

Apache (.htaccess)

RewriteEngine On
RewriteRule ^location/([a-zA-Z-]+)$ location.php?location=$1 [L]

The ([a-zA-Z-]+) pattern matches both single-word (e.g., "London") and hyphenated double-word (e.g., "New-York") URLs.

Nginx

location /location/ {
    rewrite ^/location/([a-zA-Z-]+)$ /location.php?location=$1 last;
}

内容的提问来源于stack exchange,提问作者Musa Butt

火山引擎 最新活动