PHP/MySQL Table Problems

Codes here !

Moderators: egami, macek, gesf

Post Reply
heyjim
New php-forum User
New php-forum User
Posts: 2
Joined: Tue Jul 11, 2017 1:52 pm

Tue Jul 11, 2017 1:58 pm

Hi all,

Hoping you can help with the below.

I basically have this table below which pulls the data from SQL however its only returning results from the 3rd query ($result_inventory_type) and I'm wanting to display the results from the other 3 queries within the same table.

Could someone point out where I'm going wrong? I'm new to PHP/SQL so probably doing it completely wrong :oops:

This is the SQL:

Code: Select all

$result_inventory 	= mysqli_query($con,"SELECT * FROM tbl_inventory WHERE company_id='$company_id' ORDER BY INET_ATON (ip_address)") or die(mysql_error());
$result_inventory_site 	= mysqli_query($con,"SELECT tbl_sites.name FROM tbl_inventory LEFT JOIN tbl_sites ON tbl_inventory.site = tbl_sites.site_id WHERE tbl_inventory.company_id='$company_id'") or die(mysql_error());
$result_inventory_type	= mysqli_query($con,"SELECT tbl_types.value,tbl_types.category FROM tbl_inventory LEFT JOIN tbl_types ON tbl_inventory.type = tbl_types.type_id WHERE tbl_inventory.company_id='$company_id' AND tbl_types.category='Device Type'") or die(mysql_error());
$result_inventory_os	= mysqli_query($con,"SELECT tbl_types.value,tbl_types.category FROM tbl_inventory LEFT JOIN tbl_types ON tbl_inventory.type = tbl_types.type_id WHERE tbl_inventory.company_id='$company_id'") or die(mysql_error());
And this is the table:

Code: Select all

<?php
	while($row = mysqli_fetch_array($result_inventory) and $row2 = mysqli_fetch_array($result_inventory_site) and $row3 = mysqli_fetch_array($result_inventory_type) and $row4 = mysqli_fetch_array($result_inventory_os))
		{
			echo "<td>{$row['ip_address']}</td>";
			echo "<td>{$row['hostname']}</td>";
			echo "<td>{$row2['name']}</td>";
			echo "<td>{$row3['value']}</td>";
			echo "<td>{$row['manufacturer']}</td>";
			echo "<td>{$row['model']}</td>";
			echo "<td>{$row4['value']}</td>";
			echo "<td>{$row['serial']}</td>";
			echo "<td>{$row['notes']}</td>";
		}
?>
Any help appreciated! :)

Thanks

chorn
php-forum Active User
php-forum Active User
Posts: 402
Joined: Fri Apr 01, 2016 2:18 am

Tue Jul 11, 2017 10:11 pm

if you query the database, always have a look for errors, you can set PDO into the mode that it automatically throws and exception.

to merge the data you may use SQL union, or PHP array_merge

heyjim
New php-forum User
New php-forum User
Posts: 2
Joined: Tue Jul 11, 2017 1:52 pm

Wed Jul 12, 2017 10:20 am

chorn wrote:
Tue Jul 11, 2017 10:11 pm
if you query the database, always have a look for errors, you can set PDO into the mode that it automatically throws and exception.

to merge the data you may use SQL union, or PHP array_merge
Thanks for the reply.

The queries all work fine when if I run the manually in phpMyAdmin so I think that side is ok.

I tried the array_merge however it fails with: Warning: array_merge(): Argument #1 is not an array

This is the PHP now:

Code: Select all

while($row = array_merge ($result_inventory, $result_inventory_site, $result_inventory_type,$result_inventory_os))
Any thoughts?

chorn
php-forum Active User
php-forum Active User
Posts: 402
Joined: Fri Apr 01, 2016 2:18 am

Wed Jul 12, 2017 9:39 pm

looks like you did not fetch any results after the query.

Post Reply