how to get unique row names from table in array format? - php

i have a table called site, which records the name of some sites, now the table can have a hundred rows called site.com, and/or site.net, site.me....
now what i need is to get only the unique names, like even if there is 1000 row but only 5 types of sites names exist i need to get those site names.
so far, ive tried without success this
<?php
//mysql goes here
$query = mysql_query(" SELECT * FROM SITES ");
while ($row = mysql_fetch_array($query)){
echo array_diff($row, NULL);
} ?>
nothing shows except argument error.

Use the distinct key word.
SELECT DISTINCT name FROM SITES

Related

MySQLi Inner Join issue [duplicate]

This question already has answers here:
How to include a PHP variable inside a MySQL statement
(5 answers)
Closed 2 years ago.
I have two tables on a forum that I am referencing via a SELECT query. One table is the phpBB_users table, which holds the basic data for a member while the second table, phpbb_profile_fields_data, holds details such as address, email, phone, etc. Both tables use user_id as the key. My form selects the $smode parameter, which lists the various column headings to search for and the $parameter variable, which enters the actual data to search for. The results are displayed in an HTML table.
Here's my code:
$conn = new mysqli('localhost',$user,$pass,$database);
$sql = "SELECT * FROM phpbb_users
INNER JOIN phpbb_profile_fields_data ON phpbb_users.user_id = phpbb_profile_fields_data.user_id
WHERE $smode = $parameter
ORDER BY username";
$result = $conn->query($sql);
If I select group_id (which is in the phpbb_users table) as my $smode variable and enter a given group number as the $parameter variable, I get a nice listing of the members within that group. If I change the $smode variable to pf_user_lastname (which is in the phpbb_profile_fields_data table) and enter a common last name I don't get any results. The same holds true if I use username as the $smode variable (which is in the phpbb_users table). I get no results. The group_id column is an integer while the other two are alphanumeric variables but when I change the $smode to pf_mh_year, which is a 4 digit integer in the phpbb_profile_fields_data table, I still don't get any results. I get no error messages in the error_log because $result->num_rows is zero - except for the first case where the listing displays.
I did echo the $sql and it looked fine. But when I tested it in PHPMyAdmin like you suggested it showed that I needed to place the $parameter value in single quotes. Not sure why it worked without them for the one integer column but not the other but either way, everything works now that the quotes are in place.

Associate a database row with a logged in user

I am currently creating a website through Joomla (creating a module) that is a members only site.
When members log in, I would want them to see their points (think about it like an airline flyer points site) which are unique. I have stored the points in a table in the database.
I have two tables:
#__users (the basic user table)
#__smiles_users (this stores the username, points etc etc)
Both tables have one similar column and this is "username".
I thought it would be wise to do it like this in an MYSQL query.
Connect to MYSQL
Find out the username that is logged in through #__users
Find the matching username in #__smiles_users table
Echo results that are in that specific row (that contains the correct username)
I have connected to MYSQL, echo the results, but I had to specify the WHERE function.
There are two main approaches to accomplish this:
You can perform two separate queries for every table and join them via a PHP associative array:
SELECT * FROM `__users` WHERE `username` = ?
And store the result to a var:
$user = $result->fetch();
Then do a second query for the remaining data of such user:
SELECT * FROM `__simles_users` WHERE `username` = ?
And join the data programatically like this:
$user_smiles_data = $result->fetch();
foreach($user_smiles_data as $key => $value) {
$user[$key] = $value;
}
Or something like this if you don't want to perform two different queries:
SELECT * FROM `__users`
INNER JOIN `__smiles_users`
ON `__users`.`username` = `__smiles_users`.`username`
This merges both tables by the username field at query time. The result will include all the fields of the two tables on a single row for the same username.
You are probably going to want to create a foreign key from one table to the other.
For example in SQL:
ALTER TABLE #__smiles_users
ADD FOREIGN KEY (username)
REFERENCES #__users(username)
This creates a link from one table to the other to ensure data integrity.

Multiple select from pages

I am using a code that reads pages from the database. It runs on PDO, PHP. Anyways I am wondering how would I make it so it reads from two tables? As if for example, at the current time it runs off 'pages' but could I make it also read pages from 'pages_default'. I dont mean as in it only displays pages that has both names in both tables but instead seperate tables with similar fields..
<?php
if (isset($_GET['p']))
{
$stmt = $dbh->prepare('SELECT * FROM pages WHERE shortname = :p');
if (!$stmt->execute(array(':p' => $_GET['p'])))
{//
exit('Could not exec query with param: '.$_GET['p']);
}
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
echo '<font size="6pt">'.$row["title"].'</font><div class="hr"><hr /></div><br>';
echo ' '.$row["content"].' ';
}
}
//disconnect:
$dbh = null;
?>
Hoping that I have understood your question, I assume you want to run one query and retrieve rows from two tables with different names and fields.
You use the UNION operator in your query, and get rows from the two tables, and then unifying them into one table. You must get the same number and type of fields from every table, and give the same alias names to every field you get.
Let's say you have tables t1 and t2 and want to get 3 similar fields from each, with each table having the same condition (yours shortname=:p).
SELECT <field1>, <field2>, <field3> FROM ((SELECT <t1field1> AS <field1>, <t1field2> AS <field2>, <t1field3> AS <field3>
FROM t1 WHERE <t1field2>=:p) UNION (SELECT <t2field1> AS <field1>, <t2field2> AS <field2>, <t2field3> AS <field3> FROM t2 WHERE <t2field2>=:p)) AS A
field1,field2,field3 are the aliases you set for the fields you retrieve, since the two tables may have different field names.

mysql_fetch_assoc can not access similars columns names

I have more than one table in my query
$query = mysql_query("SELECT * FROM reservation r, users u ...")
while( $fetch = mysql_fetch_assoc($query)){
if($fetch['u.id'] == $fetch['r.id'])
...
And here is the problem I have same column name on tables and when i try to compare the id of reservations with the id of user i get only the id of users!!!
I can replace the '*' with the column names and to change their name with AS (SELECT s.id as sid ...) but is there any easier way?
For e.x:
If your two tables both have a column with the same name, then you're only going to be able to access one of those fields in an associative array - the key is the name of the field, so the first one is over-written by the second.
The quick fix is, as you say, to explicitly rename the fields so they don't clash. Though you might be able to work around it by using mysql_fetch_array - that will give you all the fields, but you'll need to access fields by index number; so if you change table structures, you'll need to re-write code.
The better way is not to have two fields with the same name in different tables, if at all possible - if they were called reservationID and userID, you'd avoid this completely. I try not to re-use fieldnames unless it's a foreign key.

Mysql removing duplicates based on one column

I am using the query below
$result2 = mysql_query("select * from HandsetStock WHERE SubCategory NOT LIKE '%clearance%'", $dbh2);
I am getting a few duplicate results which I want to eliminate from the results however they are not completely duplicate rows. The column name is Make. I am guessing i need some kind of subquery but I am struggling to get it to work for me. Basically I need to select all records but where Make has the same value just the first record.
Thanks
$result2 = mysql_query("select * from HandsetStock
WHERE SubCategory NOT LIKE '%clearance%'
group by Make", $dbh2)

Categories