how to display all row data of mysql field name - php

I am trying to display all the data of mysql field but having issue display it.
What I want to do is display all the first name and last name of the members which has a membership level of 1 so if there are 20 users with membership level 1, it would display 20 first name
$query = "SELECT first_name, last_name FROM " . $wpdb->prefix . "ss_table WHERE membership_level = 1";
echo $query[20];

Your sql query is wrong, you should first check out sql sites for writing query
For fetching a single column from a table you have to use
select first_name from ss_table
but for fetching more than one column you have to append "," after every column name except after the last column
select first_name, last_name from ss_table

You've written a query, but you need to execute it if you want to get results. Echoing $query[20] will just give you the 20th character of your query statement. Look into some docs: http://php.net/manual/en/book.mysqli.php or tutorials on how to connect to a database.

Related

MySQL - How to select only two columns from a row in one table

I've looked everywhere for this answer and haven't found a single link that has helped me out for this which is surprising! So my problem is the following...
I have a table in my database called users. In that table I have two columns out of a about 5 or 6 other ones. I need to select ONLY userid and username from each row in the users table.
Here is my current query which is only pulling in the username. I don't want to pull in everything (ie using *) because I don't think that would be secure and wise considering things like emails and passwords aren't necessary in this case.
My current query string...
$users = mysqli_query($sql, "SELECT username FROM users") or die($posts . "<br/>" . mysqli_error($sql));
Thanks!
Column identifiers are just a comma separated list after your SELECT statement and before the FROM clause
$users = mysqli_query($sql, "SELECT userid, username FROM users") or die($posts . "<br/>" . mysqli_error($sql));

How to retrieve duplicated data from database in PHP

I have in my database a table that Continent users ,the first field is NAME and this name can be duplicated .
I want to retrieve user's name . for example if i have in my table the name John 3 time and David 2 time and Katy 1 time,
I want the result to be john David Katy ,
If the name are duplicated i want to escape it ,and display just names once for every name, i want to do this using PHP and MYSQLI .
If you have a column with many duplicate values and only want to list the unique values you can use the DISTINCT keyword to only return those,
SELECT DISTINCT `NAME` FROM `TABLE_NAME`

mysql query for select from two mysql tables

I'm using codeigniter.
We have two mysql tables.
What I'm doing is taking all the details in the first table and feeding it to an html page to let all users to view it. But now I want to add some details from a second table related to a column in first table.
I mean, I have a column in first table call "Pending_on" in that column I have inserted some branch names
and in the second table I have inserted the contact details of those departments.
So, if we think in the first table fifth row pending_on column has the value "HR" then I want to display the telephone number of the HR department.
So, how can I write a query for this?
For your case, here is an example:
There are 2 tables named entry and subscription.
entry
field1 publisherId
field2 entry
subscription
field1 myId
field2 friendId
You can use a JOIN. In plain SQL:
SELECT e.publisherId, e.entry
FROM entry e
JOIN subscription s
ON s.friendId = e.publisherId
WHERE s.myId = 1234
this is the query i used to do my work
and thanks goes to MR. Yuvvaraj Bathrabagu
$this->db->select('*');
$this->db->from('new_users');
$this->db->join('contact', 'new_users.pending_on = contact.dep','left');
$query = $this->db->get();
$data = $query->result_array();
Let's take your example. You have a column name pending_on in your first table, and in your second table you have your department_name column. Find the below code for joining those two tables,
$this->db->select('firsttable.*,secondtable.contact_details');
$this->db->from('firsttable');
$this->db->join('secondtable', 'firsttable.pending_on = secondtable.department_name','left');
$query = $this->db->get();
So, The department_name in second table and pending_on in first table should have the same value as you mentioned "HR". And my suggestion is to have the id's of the table as reference instead of department names. Hope This helps.

mysql displaying all contains of the table

Is it possible to display the mysql table using php, i mean display the records with field names without specifying the row names.
example
|id|name |address |status | <--- this is the field in the mysql table
|1|name1 |address1 |status1| <-- this is the records
and more records...
Just an idea:
Run DESCRIBE tablename and get field names/type
Run SELECT * FROM tablename and get records
If I understand you correctly you want to display all records in the table?
$sql = mysql_query("SELECT * FROM tabel")
the star means you will select all rows. change table to your table name..
Now all your table information is stored in $sql

How do I perform a count from a table and then display the count value using PHP?

Currently I have two tables A and B. The DID is input by the user into into a input, and that is the first prompt in my program, to enter the DID.
A contains PID, DID, and MaxOccupancy. PID is professional id, DID is department id.
B contains CID, DID, and PID. CID is client ID, and DID and PID is same.
I'm outputting data from the first table into an html table which shows all that data.
However, I'm having trouble running a count on number of PID from table two. I have $countB= mysql_query ("select count (PID) from B where DID=('$_POST[DID])");
I am then writing
while($row = mysql_fetch_array($countB))
{
echo "Current occupancy:".$row['count(PID)'].;
}
Can someone help me? How do i do a where in my query for the count to get what the user put in for DID in the input box? Am i doing it wrong completely?
THANkS!
Try
$countB= mysql_query (sprintf("SELECT COUNT(PID) as total FROM B WHERE DID=('%s')",mysql_real_escape_string($_POST['DID'])));
$row = mysql_fetch_array($countB);
echo $row['total'] ;
Note .. always filter for SQL Injection
You can create an alias for the result of the COUNT which you can access in PHP:
mysql_query ("select count (PID) as PIDcount from B where DID=('$_POST[DID])");
// then reference it later
$row['PIDcount'];
The usage of $_POST[DID] in your query is subject to SQL Injection as well.

Categories