How to display the result from mysql union query - php

I have a problem displaying the result from the following query:
$sql="SELECT CONCAT(threadID,'',memberID),'posts' as Type
from posts
where memberID=$userid
UNION
SELECT CONCAT(threadID,'',memberID),'replies',as Type
from replies
where memberID=$userid order by 1";
$result=mysqli_query($con,$sql);
while($row=mysqli_fetch_array($result)){
$postid=$row['thread'];
echo $postid;
}
The error I get is
undefined index threadID" $postid=$row['threadID'];

The problem is you don't have a column called "thread" in the output of your query...you aren't outputting that column, instead you're outputting the result of the CONCAT function.
If you write var_dump($row); as a debugging step inside your loop you would see what the row actually looks like.
A simple way to fix this is to give the calculated column an alias, so that it gets a sensible name in the $row variable. You can do this with the AS keyword in SQL. If you just call the alias "thread" then you won't have to change your PHP code at all:
SELECT CONCAT(threadID,'',memberID) AS thread,'posts' as Type
from posts
where memberID=$userid
UNION
SELECT CONCAT(threadID,'',memberID) AS thread,'replies',as Type
from replies
where memberID=$userid order by 1

Related

How can I find max value in SQL and read into PHP using PDO?

I am using PHP to create a website whereby a user inputs values into a new row on one page (values for Awinner, Arunner and so on) and on the next page those values are retrieved from the database. For this to work I need to find the most recent id which will always be the highest. What code can I put in between the hashtags below in order to do this?
$sql = "SELECT Awinner, Arunner, Bwinner, Brunner, Cwinner, Crunner, Dwinner, Drunner FROM groupStage";
$sql .= " WHERE id = #the maximum id#";
Then after this, I need to put each value (Awinner, Arunner, Bwinner, etc.) into its own variable in PHP so that I can display them all separately on the page. How can I do this? Sorry for lack of detail here but I am a newbie to PHP and SQL and am working on an important school project.
try like below by using subquery
"SELECT Awinner, Arunner, Bwinner, Brunner, Cwinner, Crunner, Dwinner, Drunner FROM groupStage where id= ( select max(id) from groupStage)"
Don't use a where use an order by with a limit to only return the 1 row.
SELECT Awinner, Arunner, Bwinner, Brunner, Cwinner, Crunner, Dwinner, Drunner
FROM groupStage
order by `the maximum id` desc
limit 1

how to get the last value from mysql and increment with php

i have a user database and i have to assign a user id for them for example AA01
AND after insert 1 person into database i have to insert another person with user id AA02 AND i have increment it and so on.
what i have already tried is
$sql_user_id=mysql_query("SELECT MAX(user_id) FROM `users` desc LIMIT 1");
$new_array = explode('AA',$sql_user_id);
$number=$new_array[1];
$newnumber=$number+1;
and i am getting wrong with result Resource id #5
You have to fetch the query results before you can use them.
$result=mysql_query("SELECT MAX(user_id) AS maxId FROM `users` desc LIMIT 1");
$sql_user_id = mysql_fetch_assoc($result);
$new_array = explode('AA',$sql_user_id['maxId']);
$number=$new_array[1];
$newnumber=$number+1;
FYI, I added an alias to your query as it makes referencing a value returned from a function much easier to do.
mysql_query() returns a resource, not a value you can operate directly. You have to fetch the value from this resource first:
$res = mysql_query('SELECT MAX(`user_id`) FROM `users`');
$val = mysql_fetch_row($res);
$new_array = explode('AA', $val[0]);
...
Also, notice that MAX() causes an implicit grouping to happen. So there is no order, and there is only one result. Specifying any of this in your query is futile.
In addition, notice that, since user_id is aparently a text, it might not work as expected. When sorting text, 2 comes after 10. You may need to use a different reference to fetch the latest user_id. If you can help it, don't use text to index your records.

PHP MySQL While loop for SELECT from two tables?

Hi there i am working on PHP code that is selecting columns from two tables.
Here is my code:
$result2 = mysql_query("SELECT *
FROM `videos`, `m_subedvids`
WHERE `videos.approved`='yes' AND
`videos.user_id`='$subedFOR'
ORDER BY `videos.indexer`
DESC LIMIT $newVID");
while($row2 = mysql_fetch_array($result2))
{
$indexer = addslashes($row2['videos.indexer']);
$title_seo = addslashes($row2['videos.title_seo']);
$video_id = addslashes($row2['videos.video_id']);
$title = addslashes($row2['videos.title']);
$number_of_views = addslashes($row2['videos.number_of_views']);
$video_length = addslashes($row2['videos.video_length']);
}
When i try to print $indexer with echo $indexer; it's not giving me any results.
Where is my mistake in this code?
It seems to me like the key 'indexer' isn't in your results. It's hard to tell, since you haven't listed a definition for your table and you're using SELECT * so we can't see the names.
It makes the program easier to read later, if instead of SELECT *..., you use SELECT col1, col2, .... Yes, SELECT * will save you some typing right now, but you'll lose that time later when you or anyone else who works on your code has to check the table definition every time they work with that line of code.
So, try changing your query to explicitly select the columns you use. If it's an invalid column you'll get an error right away rather than this silent failure you're getting now, and you'll thank yourself later as well.
So long as videos.indexer is a unique field name among all tables used in the query you can change
$indexer = addslashes($row2['videos.indexer']);
to
$indexer = addslashes($row2['indexer']);
You don't need to (or can not) use the table name when referring to the result.

MySQL COUNT showing it's working out

I would expect the following to output the number "5", since there are 5 rows in the database with with item 68 and user 1. But instead I'm getting this output "12345".
$resultb4 = mysql_query("SELECT COUNT(comparedRating) FROM recComparedRating WHERE user1='1' AND itemID='68' GROUP BY itemID AND user1");
while($rowb4 = mysql_fetch_array($resultb4)){
$countcomparedratings=$rowb4['COUNT(comparedRating)'];
}
echo $countcomparedratings;
What am I doing wrong?
The reason you are getting 12345 is because your query is returning 5 results and your code to output the count is simply outputting the concatenation of the returned array from the query.
Without understanding your database structure, I'm guessing that the reason you're getting the '12345' has something to do with your GROUP BY clause. Use a program like MySQLWOrkbench to connect to your database and test out your query before you include it into your code. It is a time saving technique to debug your queries.
Also, I would alias the COUNT value so that you simply refer to the alias when you refer to your column names.
SELECT COUNT(comparedRating) as ratingCount FROM recComparedRating WHERE user1='1' AND itemID='68' GROUP BY itemID AND user1");

codeigniter select as

how doHow do we do
select table.value as table_value from table in codeigniter?
The AS part doesnt work because when i try to access the value,
this doesnt work:
$qry_inp = 'select table.value as table_value from table '
$query = $this->db->query($qry_inp);
echo $query->row('table_value ');// this will be empty, but it shouldn`t be
doesn`t matter if its in AR or simple query
Pretty simple thing.
$this->db->select('COLUMN_ACTUAL_NAME as `COLUMN_NAME_YOU_WANT_TO_SHOW`');
i'm joining two tables in which column names are same so i separate both tables columns by using as keyword , this is how you can use AS in codeigniter
$this->db->select("departments.name AS 'dname'");
$this->db->select('positions.name');
Where is that behaviour documented? row doesn't take a column name as a parameter; it optionally takes a row number, and that's it. Access it like any other field:
echo $query->row()->table_value;

Categories