So I think there's will be a simple and efficient code to count data with PHP and get the data output from different MySQL tables. This implementation is used to display how much data in my dashboard page.
But I prefer to use PHP procedural style because I'm still learning.
So here is my dirty code:
$count_admin = mysqli_query($conn, "SELECT COUNT(*) AS count_admin FROM employee WHERE level_id = 1");
$count_officer = mysqli_query($conn, "SELECT COUNT(*) AS count_officer FROM employee WHERE level_id = 2");
$count_goods = mysqli_query($conn, "SELECT COUNT(*) AS count_goods FROM goods");
$count_customer = mysqli_query($conn, "SELECT COUNT(*) AS count_customer FROM customer");
$row_admin = mysqli_fetch_assoc($count_admin);
$row_officer = mysqli_fetch_assoc($count_officer);
$row_goods = mysqli_fetch_assoc($count_goods);
$row_customer = mysqli_fetch_assoc($count_customer);
echo $row_admin['count_admin'];
echo $row_officer['count_officer'];
echo $row_goods['count_goods'];
echo $row_customer['count_customer'];
I am new to PHP coding and just trying to fix some functionality on my site that was left over from the lead developer.
The site, [Vloggi], is a marketplace. So I need to show the name of the job poster in the assignments page . The table I have the jobs in only has the ID, not the name.
So I need a join, but I've tried and it breaks the entire site.
The SQL has 17 tables, I need to display the User Name (usr_name) contained in table 3, the organisation contained in table 7 (usrg_orgname) with the job posting user (vlop_usr_id) details in table 14.
The primary key is users.usr_id, which is linked to users_gor.usrg_usr_id and vlog-ops.vlog_usr_id.
Table 3: users
usr_id, usr_email, usr_password, usr_fbuser, usr_fbtoken, usr_name, usr_loc_name, usr_loc_lat1, usr_loc_lon1, usr_loc_lat2, usr_loc_lon2, usr_status, usr_gor, usr_vgr, usr_token, usr_regtoken,
table 7: users_gor
usrg_usr_id, usrg_creditops, usrg_creditvlog, usrg_creditvlogette, usrg_destination, usrg_orgname, usrg_orgtype, usrg_location, usrg_website, usrg_jobtitle, usrg_phone, usrg_address1, usrg_address2, usrg_state, usrg_postcode, usrg_country
Table 14: vlog-ops
vlop_id, vlop_title, vlop_description, vlop_tags, vlop_deadline, vlop_quantity, vlop_quantityposted, vlop_vser_id, vlop_usr_id,vlop_loc_name, vlop_loc_lat1, vlop_loc_lon1, vlop_loc_lat2, vlop_loc_lon2, vlop_campaign, vlop_rules, vlop_tips, vlop_status
So in main.php i have written the following Sql lookup
in main.php, I have the following SQL lookups:
$sql = "SELECT * FROM users_gor WHERE usrg_usr_id = ".$db->quote($user_info['usr_id'])." LIMIT 1";
$rows = $db->select($sql);
$users_gor = $rows[0];
$sql = "SELECT * FROM users_vgr WHERE usrv_usr_id = ".$db->quote($user_info['usr_id'])." LIMIT 1";
$rows = $db->select($sql);
$users_vgr = $rows[0];
$sql = "SELECT * FROM users WHERE usr_id = ".$db->quote($user_info['usr_id'])." LIMIT 1";
$rows = $db->select($sql);
$users = $rows[0];
$sql = "SELECT * FROM vlog-ops WHERE vlop_usr_id ".$db->quote($user_info['usr_id'])." LIMIT 1";
$rows = $db->select($sql);
$users = $rows[0];
$sql = "SELECT usr_name AS vlop_usr_name FROM users WHERE usr_id = ".$db->quote($user_info['usr_id'])." LIMIT 1";
$rows = $db->select($sql);
$users = $rows[0];
And then in the page template itself, I have written
<?php echo $vlop['vlop_vser_id'] ?>
<?php echo $vlop['vlop_usr_name'] ?>
The first one works, the second doesn’t. What I want eventually is to display the user name and the organisation name in a table.
Whenever I try a JOIN or a NATURAL JOIN or a LEFT JOIN it breaks and the entire site goes blank.
Any help for a newbie would be appreciated with a million thanks.
When you use JOIN you need to specify how you're joining them.
In the query below I'm assuming you're looking for the fields in bold from your question.
$query='SELECT u.usr_name, g.usrg_orgname, v.vlop_usr_id FROM users u
JOIN vlog-ops v on u.usr_id = v.vlop_usr_id
JOIN users_gor g on u.usr_id = g.usrg_usr_id';
I believe I got the name of the fields right but if not just replace them with the correct ones.
Once you have the data fetched, you just loop through the results:
$result = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc($result)) {
echo 'User name = ' . $row['u.usr_name'];
echo 'Org name = ' . $row['g.usrg_orgname'];
echo 'Job posting user id = ' . $row['v.vlop_usr_id'];
}
I'm tring to get a hold of foreign keys, as I need to use it for a gallery, where each uploaded image is assigned to a photographer, these photographers also need to be showed in the menu.
I have followed this guide and everything went well. I now need to output the data with PHP - this I can't figure out.
<?php
$sql = "SELECT * FROM borrowed WHERE employee.id = 'Reck' JOIN employee ON employee.id = borrowed.employeeid";
$result = mysqli_query($db, $sql);
while($row = mysqli_fetch_array($result)) {
?>
<? echo $row['lastname']; ?>
<?php
}
?>
I get the error Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /Applications/MAMP/htdocs/galleri/test.php on line 12
Line 12 is the while loop.
An error within your query please refer query structure How to use joins
Within your query join is after where condition but it must be before where. It should be like this
$sql = "SELECT * FROM borrowed JOIN employee ON employee.id = borrowed.employeeid WHERE employee.id = 'Reck'";
and your code
<?php
$sql = "SELECT * FROM borrowed JOIN employee ON employee.id = borrowed.employeeid WHERE employee.id = 'Reck'";
$result = mysqli_query($db, $sql);
while($row = mysqli_fetch_array($result)) {
echo $row['lastname'];
}
?>
I often read that mysql(i) queries used in while loops are not really performance friendly and I'm going to rewrite my code because I think it get better. But here's my question if a JOIN can work here. It's a single post viewing function but you should see every comment which got posted below. I got the comment loop in the loop which the post is printed.
<?php
$result = (empty($_GET['hash'])) ? $_GET['hash'] = '' : mysqli_query($conn, "SELECT * FROM `userposts` WHERE `hash`='".$_GET['hash']."' LIMIT 1");
while($rows = mysqli_fetch_array($result)){
//Output of single post
$commentresult = mysqli_query($conn, "SELECT * FROM `comments` WHERE `postid`='".$rows['id']."'");
while($commentrows = mysqli_fetch_array($commentresult)){
//Output of all comments
}
}
So I think it gets better but the query with JOIN should be like:
$result = (empty($_GET['hash'])) ? $_GET['hash'] = '' : mysqli_query($conn, "SELECT * FROM `userposts` JOIN comments ON userposts.id = comments.postid WHERE `hash`='".$_GET['hash']."' LIMIT 1");
But if I'm trying to post the data of the comments in the while loop which is the singlepost printed it just prints one comment. So what can I do?
regards
Your code is a bit of a mess. You are sometimes call mysql_fetch_results with a string, which is weird.
What you want is this:
"SELECT * FROM comments LEFT JOIN userposts ON comments.PostID = userposts.ID WHERE userposts.Hash = '$_GET['hash']'"
EDIT: sorry, you want LEFT JOIN with comments on the left. Gives you all the comments
EDIT 2: I set this up and it worked as I expected, even with multiple posts with the same hash.
<?php
$hash = $_GET['hash'];
$result = mysqli_query($conn, "SELECT * FROM comments LEFT JOIN userposts ON comments.PostID = userposts.ID WHERE userposts.Hash = '$hash'");
if ( $row = mysql_fetch_array($result)) {
// output of post.
// output first comment
}
while($row = mysqli_fetch_array($result)){
//Output other comments
}
?>
I used this code to connect to a database and fetch results. This worked perfectly until i tried to work in another query to the images table to get associated images. I'm not very experienced with OO programming. So hopefully someone can see where ive gone wrong and help me out.
<?php
global $__CMS_CONN__;
$sql = "SELECT * FROM ecom_products";
$stmt = $__CMS_CONN__->prepare($sql);
$stmt->execute(array($id));
while ($row = $stmt->fetchObject()) {
$imagesql = "SELECT * FROM ecom_product_images where id = $row->id && where primaryImage = '1'";
$imagestmt = $__CMS_CONN__->prepare($sql);
$imagestmt->execute(array($id));
$imageName = $imagestmt->fetchObject();
echo ''.$row->productNm.''.$imageName;
}
?>
Modify your SQL query:
$imagesql = 'SELECT * FROM ecom_product_images where id = ' . $row->id . ' AND primaryImage = "1"';
Have you declared before $id variable ?
Have you got any errors with your code ?
If your primaryImage column is int type then skip apostrophes in query
primaryImage = 1
if enum then it is ok.
You need not OO programming, but SQL one.
SELECT p.*, imageName
FROM ecom_products p, ecom_product_images i
WHERE p.id = i.id AND primaryImage = 1;