How - SELECT all data in a column WHERE...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Ghoz
Forum Commoner
Posts: 38
Joined: Thu Jul 12, 2012 2:32 am

How - SELECT all data in a column WHERE...

Post by Ghoz »

Hi,

How to SELECT all data in U_Id from pryd table WHERE Pr_Id=185?
Please write in a proper code as I'm still new in php. Please help. Thanks!

U_Id Pr_Id
32 185
33 185
84 185
32 100
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How - SELECT all data in a column WHERE...

Post by requinix »

Ghoz wrote:SELECT all data in U_Id from pryd table WHERE Pr_Id=185?
That is pretty damn close.
User avatar
mixa2000
Forum Newbie
Posts: 18
Joined: Fri Jun 22, 2012 4:50 pm

Re: How - SELECT all data in a column WHERE...

Post by mixa2000 »

Try this:

Code: Select all

<?php 
// Connects to your Database 
$host = "";       //  Your host address for example if this is your localhost it will be localhost  
$name = "";                   //  Name of your username that you connect with to your mysql server
$pass = "";                   //  Password that you use with that name
$db_name = "";           //  The name of the database which this table is found at

mysql_connect("$host", "$name", "$pass") or die(mysql_error()); 
mysql_select_db("$db_name") or die(mysql_error()); 


$check = mysql_query("SELECT U_Id FROM pryd WHERE Pr_Id = '185'")or die(mysql_error()); 
	while($info = mysql_fetch_array( $check )) 	 
	{ 
	$U_Id = $info['U_Id'];
              $Pr_Id = $info['Pr_Id'];
	print "$U_Id, ";
	} 

?>
Hope this is helpful! :) Please reply.
User avatar
mixa2000
Forum Newbie
Posts: 18
Joined: Fri Jun 22, 2012 4:50 pm

Re: How - SELECT all data in a column WHERE...

Post by mixa2000 »

One question, in the example you gave did you need the results to be; 32,33, and 84?
Ghoz
Forum Commoner
Posts: 38
Joined: Thu Jul 12, 2012 2:32 am

Re: How - SELECT all data in a column WHERE...

Post by Ghoz »

Yes. Going to try your suggestion. Thanks!
Ghoz
Forum Commoner
Posts: 38
Joined: Thu Jul 12, 2012 2:32 am

Re: How - SELECT all data in a column WHERE...

Post by Ghoz »

Thanks, it works!

Now how to insert the link to each result?

The link is something like below where uid result is taken from $U_Id
http://try000.com/index.php?section=profile&uid=29

Thanks!
User avatar
mixa2000
Forum Newbie
Posts: 18
Joined: Fri Jun 22, 2012 4:50 pm

Re: How - SELECT all data in a column WHERE...

Post by mixa2000 »

You might want to try this, but if there is going to be more than one U_Id, then I am not really sure how that will work, ask any questions if this doesn't work, please reply.
Add this to the end of that script...

Code: Select all

<div align="center"><a href="index.php?section=profile&uid="
<?php 
mysql_connect("$host", "$name", "$pass") or die(mysql_error()); 
mysql_select_db("$db_name") or die(mysql_error()); 


$check = mysql_query("SELECT U_Id FROM pryd WHERE Pr_Id = '185'")or die(mysql_error()); 
         while($info = mysql_fetch_array( $check ))       
         { 
         $U_Id = $info['U_Id'];
         print "$U_Id, ";
         } ?>
"><b>Here</b></a></div>
Ghoz
Forum Commoner
Posts: 38
Joined: Thu Jul 12, 2012 2:32 am

Re: How - SELECT all data in a column WHERE...

Post by Ghoz »

Hi,

It can't display more than 1 result. I knew how to do it if only 1 result.
How to display more than 1 result with each link?

Thanks!
User avatar
mixa2000
Forum Newbie
Posts: 18
Joined: Fri Jun 22, 2012 4:50 pm

Re: How - SELECT all data in a column WHERE...

Post by mixa2000 »

Can you explain me what it should show when you click on the link?
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: How - SELECT all data in a column WHERE...

Post by Pazuzu156 »

Here is a simple way of dong it, but overall I suggest doing this in an object oriented way.

Code: Select all

<?php

$c = mysql_query("SELECT `U_Id` FROM `pryd`");

while($r = mysql_fetch_array($c)) {
    echo "<a href=\"http://try000.com/index.php?section=profile&uid=" . $r['U_Id'] . "\">http://try000.com/index.php?section=profile&uid=" . $r['U_Id'] . "</a><br>\n";
}

?>
Simple, but that should show all links with all the dynamic UIDs you're fetching from the database. Again, I'd do this in an OOP way for security and simplicity.
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
Ghoz
Forum Commoner
Posts: 38
Joined: Thu Jul 12, 2012 2:32 am

Re: How - SELECT all data in a column WHERE...

Post by Ghoz »

Hi Pazuzu, you didn't directly give me the solution but your suggestion did gave me an idea of how to do it. Actually I was stucked with <a href....Somehow I can't print/echo <a href... I suppose because of the '<' symbol.

Thanks!!
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: How - SELECT all data in a column WHERE...

Post by Pazuzu156 »

You should be able to echo it out. Why you can't that's a bit odd. However, if you want to display the anchor tag to the world, replace < with <
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
Post Reply