Trying to get my site to show two different mysql tables

Currently I have my site showing info from one database table, but I want to get it to show information from another table. I have tried learning how to use Join but I can’t get it to work with what I have.
I have it connected to the database in a different file and that file is included.

$userData = “SELECT * FROM support_panel WHERE id = '”.$_SESSION[‘userid’]."’";
$result = $con->query($userData);
$row = $result->fetch_assoc();

I recall all of the data like this

<?php echo $row['username'] ?>

Where is your JOIN attempt?

What data is you are trying to get?

What are the table name(s) you want to get data from?

Do both tables share some of the same data?

I removed the join attempts to get rid of the errors. The data from the support_panel is just user information and the second is an_panel which is announcements. They don’t share any data.

Welcome to the site, MrBell11…

In your example you select data from table “support_panel”. That is one table.
Just use another table for the second data query. Simple.
$userData = “SELECT * FROM support_panel WHERE id = '”.$_SESSION[‘userid’]."’";
$userPosts = “SELECT * FROM user_posts WHERE user_id = '”.$_SESSION[‘userid’]."’";
Just an example. When you create a site using a database, you first need to create the database.
You need to layout all of the data you need and sort them into tables. Then, you select the data you need.
Normally, you place all your user data such as name, addresses, user-level-access info, passwords, etc
into a ‘users’ table. Then, place all the data for the site into other tables. You run a query to select all of
the data from the various table as needed.

I explained that as it appears you are new to using tables. Ask any further questions. We will help!

Also, if you need to join data from two different tables, you would need to think of the the data as two different query where you can merge data into the results. This is tricky sometimes. Perhaps you should explain what data you have in general and how you want to use the two or more table’s data. Then, we could help you sort it all out.

I just tried this, and it said Parse error : syntax error, unexpected ‘an_panel’ (T_STRING)

an_panel is the table name

On one side of the website I am getting everything from support_panel.
This is pulling general information up for the user such as there steamid, steam username, and the level of access that they have on the website. All of this is currently working.

On the other side of the website I would like to make it so that it will show announcements. The announcements are stored inside of (an_panel). All I want to pull is announcements, but I also have timestamp and staff just in case I want to see who posted it and when it was posted inside of the database.

More info always helps. So, this new info tells me you want normal tables.
In the “an_panel”, you need to attach the announcement to the user. Usually, you would have a filed in
the second table, like “poster_id” or “user_id” or something that makes sense to you. Then, the code
would process this way…

$query = “SELECT * FROM support_panel WHERE id = '”.$_SESSION[‘userid’]."’";
$result = $con->query($query);
$userData = $result->fetch_assoc();

Which would give you the data from the user’s table. Then, in the “an_panel” table, use a field “userid” or
whatever works for you. When an announcement is posted, assign the support_panel’s userid to it.
Then, you can just run another query like:

$query = “SELECT * FROM an_panel WHERE userid = '”.$userData[‘userid’]."’";
$result = $con->query($query);
$anData = $result->fetch_assoc();

Then, you have the data from the an_panel table loaded into the $anData variable that pertains only to
the userid for the current user. No JOIN is need for this. Also, note that you would allow more than one
announcement for one user, therefore to display them, you need to loop thru $anData results and make
sure you display all of them.

The examples are not tested just off the top of my head. But, Hope all that helps. Good luck.

Also, the syntax error is most likely a problem with quotes and double-quotes.
You would need to show us the query code for us to fix that error.

ok so this is what I have


$userData = “SELECT * FROM support_panel WHERE id = '”.$_SESSION[‘userid’]."’";
$result = $con->query($userData);
$row = $result->fetch_assoc();

$userData = “SELECT * FROM an_panel WHERE userid = '”.$row[‘userid’]."’";
$result = $con->query($userData);
$row2 = $result->fetch_assoc();


I am getting this as the error now,
Notice : Undefined index: userid in PAGE on line 8

Fatal error : Uncaught Error: Call to a member function fetch_assoc() on boolean

I have gotten the fetch_assoc() error before by putting two fetch_assoc(); on the same page so I have just stuck to not putting that on the same page twice, but I do not know how else to get around it

I also tried changing userid to id in the second line of code but I got this error
Notice: Undefined index: userid
Second line would look like this v
$userData = “SELECT * FROM an_panel WHERE id = '”.$row[‘userid’]."’";

Well, this is saying that there is no FIELD COLUMN named “userid” in your database an_panel table.
Did you create the field in the database? Also, do you have a sample announcement in the table for testing?

The error is not due to two queries on one page. You can put hundreds on a page. The problem is that one query is not finding the correct fieldname’s in the table. If you bring up your MySQL control panel, you should be able to look at the structure of the an_panel table and see what you named the field for the user who posted it. I gave you just examples and do not know how you set up your tables.

So, first check your table and see how it is designed and then fix the query as needed.

I’d just like to thank you a crap ton, your answers helped me a ton, after playing with it a bit and understanding my stupidity on that last post I was able to get it to work.

Great! Always fun to solve a puzzle, right? Post again when you get stuck again.

Sponsor our Newsletter | Privacy Policy | Terms of Service