Jump to content

Help with sql_query -> where statement.


jitu

Recommended Posts

I have a table with say 2000 members name.

 

Now when the viewers search for a name by first name and doesn't find that he got some suggestion for similar user name/ nick name.

 

Problem is when I have say 50 people with same nick name what should be the query statement. I worte something as below:

 

$result = mysql_query("SELECT * FROM theTable WHERE nickname='{$_GET['who']}' ORDER BY id ASC")

 

It shows me something like this:

Nick            ID
xyz        123456789
xyz        123456789
xyz        123456789
xyz        123456789

 

But I nedd something like

Nick            ID
xyz        123456789
xyz        987456321
xyz        521478935
xyz        357412599

   

 

What should be the statement ? Please help me.

Link to comment
Share on other sites

 

It shows me something like this:

Nick            ID
xyz        123456789
xyz        123456789
xyz        123456789
xyz        123456789

 

are these resutls are seen from CUI or PHP. If PHP, post the code.

Link to comment
Share on other sites

The code om simplest form:

<?php
//call datatbase
$result = mysql_query("SELECT * FROM theTableName WHERE nickname='{$_GET['who']}' ORDER BY id ASC")
or die(mysql_error());

$row = mysql_fetch_array($result);

//start table
echo("<table boder='0' cellpadding='0' cellspacing='3' width='500'><tr>");

//asign variable
$num = mysql_num_rows($result);
$i=0;

//loop start
while ($i < $num)
{
echo("<td width='50%' align='center'>".$row['nick']."</td><td width='50%' align='center'>".$row['id']."</td>");
$i++;
}
//end table
echo("</tr></table>");
?>

Link to comment
Share on other sites

fetch_array statement needs to be inside the loop, not before it, otherwise you just read the first record and repeat its contents.

 

<?php
while ($i < $num)
{
$row = mysql_fetch_array($result);

echo("<td width='50%' align='center'>".$row['nick']."</td><td width='50%' align='center'>".$row['id']."</td>");
$i++;
}

Link to comment
Share on other sites

Sorry for my fault, :P I mistype while re-writing, In my original script it is inside the loop,  :P

 

corrected code

 

<?php
//call datatbase
$result = mysql_query("SELECT * FROM theTableName WHERE nickname='{$_GET['who']}' ORDER BY id ASC")
or die(mysql_error());


//start table
echo("<table boder='0' cellpadding='0' cellspacing='3' width='500'><tr>");

//asign variable
$num = mysql_num_rows($result);
$i=0;

//loop start
while ($i < $num)
{

$row = mysql_fetch_array($result);

echo("<td width='50%' align='center'>".$row['nick']."</td><td width='50%' align='center'>".$row['id']."</td>");
$i++;
}
//end table
echo("</tr></table>");
?>

 

But still it's not working!!

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.