/    Sign up×
Community /Pin to ProfileBookmark

Query Two Columns

Hi,

How do I query to columns from the same table at the same time?
[B]For example:[/B] A column containing names, and a column containing gender’s

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@chazzyMar 03.2008 — you mean

<i>
</i>select column_a,column_b from this_table
?
Copy linkTweet thisAlerts:
@DysanauthorMar 03.2008 — I trying to create a search script, that searches the name and gender columns contained in a database for keywords entered in a forms text box.

Why doesn't the following code work?

[CODE]<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die(mysql_error());
}

mysql_select_db("db", $con);

$keywords = mysql_real_escape_string($_POST['keywords']);

$result = mysql_query("SELECT * FROM persons WHERE name, gender LIKE '%$keywords%'");

while($row = mysql_fetch_array($result))
{
echo $row['name'];
echo $row['gender'];
}

mysql_close($con);
?>[/CODE]
Copy linkTweet thisAlerts:
@chazzyMar 03.2008 — I trying to create a search script, that searches the name and gender columns contained in a database for keywords entered in a forms text box.

Why doesn't the following code work?

[/quote]


It's not valid sql. You need to individually check each column

this is valid:
<i>
</i>SELECT * FROM persons WHERE name LIKE '%$keywords%' or gender like '%$keywords%';
×

Success!

Help @Dysan spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.3,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...