php - SELECT same values from table - mysql...


#1

So, i’m creating a login membership. When the user logs in, they have an unique ID that was placed in a MYSQL table from when they registered.

For example, one user has an ID of 21 and at the moment they have 3 rows with the ID 21.

My question is:

How to I get those rows and only those rows to display them in php for each unique ID (user) depending on how many they have? So, for this example, it would display the number “21” three times…

Thanks

Timothy


#2
SELECT * FROM something WHERE id = 21

Fiddle http://sqlfiddle.com/#!2/3006ee/1


#3
abhimanyusharma003 said
SELECT * FROM something WHERE id = 21

Fiddle http://sqlfiddle.com/#!2/3006ee/1

Hey, thanks for the speedy reply! I see how that was done… but I’m looking for a way to do this in php to echo is out.

If I do something like this:

$sql = "SELECT * FROM albums WHERE id = 21 LIMIT 0, 30 ";
echo $sql;

Would that make sense? Doesn’t seem to be working though…

Thanks again!


#4

are you first connecting to your database ?


#5

Here you go, this is the full code that I will use

<?php
// configuration
$dbtype		= "mysql";
$dbhost 	   = "localhost";
$dbname		= "something";
$dbuser		= "root";
$dbpass		= "mysql";

// database connection
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);


// query
$sql = "SELECT * FROM something WHERE id = :id";
$q = $conn->prepare($sql);
$q->execute(array('id' => 21));


$q->setFetchMode(PDO::FETCH_BOTH);

// fetch
while($r = $q->fetch()){
  print_r($r);
}


#6
Australia said

are you first connecting to your database ?

Yup! of course :slight_smile:

I got it working now! Thanks guys, you rock!

$sql = "SELECT * FROM albums WHERE id = 21 LIMIT 0, 30 ";
						
$something= mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_assoc($something)) {
echo $row['id'];
}

#7
abhimanyusharma003 said

Here you go, this is the full code that I will use

<?php
// configuration
$dbtype		= "mysql";
$dbhost 	   = "localhost";
$dbname		= "something";
$dbuser		= "root";
$dbpass		= "mysql";

// database connection
...........

Cool, thanks again!


#8

Glad your sorted, I wasnt being fa-see-shus ( can never spell it ) , but the amount of times I run the query before connection, I need punching


#9
Australia said

Glad your sorted, I wasnt being fa-see-shus ( can never spell it ) , but the amount of times I run the query before connection, I need punching

haha oh, I know. I do it all the time.