Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I need you to help me shorten this code. It works perfectly and everything. So I need not only a shorter way, but one that works just like this one does. I'm sort of new to PHP and I know there must be a shorter way to put this script. Here it is:
<?php $sqlMS = "SELECT * FROM Visits WHERE Company_ID = $cid AND MONTH(Date_Entered) = '1'";
$queryMS = mysqli_query($connection ,$sqlMS);
$AdvisoryMS = mysqli_num_rows($queryMS);
$sqlMS2 = "SELECT * FROM Visits WHERE Company_ID = $cid AND MONTH(Date_Entered) = '2'";
$queryMS2 = mysqli_query($connection ,$sqlMS2);
$AdvisoryMS2 = mysqli_num_rows($queryMS2);
$sqlMS3 = "SELECT * FROM Visits WHERE Company_ID = $cid AND MONTH(Date_Entered) = '3'";
$queryMS3 = mysqli_query($connection ,$sqlMS3);
$AdvisoryMS3 = mysqli_num_rows($queryMS3);
$sqlMS4 = "SELECT * FROM Visits WHERE Company_ID = $cid AND MONTH(Date_Entered) = '4'";
$queryMS4 = mysqli_query($connection ,$sqlMS4);
$AdvisoryMS4 = mysqli_num_rows($queryMS4);
$sqlMS5 = "SELECT * FROM Visits WHERE Company_ID = $cid AND MONTH(Date_Entered) = '5'";
$queryMS5 = mysqli_query($connection ,$sqlMS5);
$AdvisoryMS5 = mysqli_num_rows($queryMS5);
$sqlMS6 = "SELECT * FROM Visits WHERE Company_ID = $cid AND MONTH(Date_Entered) = '6'";
$queryMS6 = mysqli_query($connection ,$sqlMS6);
$AdvisoryMS6 = mysqli_num_rows($queryMS6);
$sqlMS7 = "SELECT * FROM Visits WHERE Company_ID = $cid AND MONTH(Date_Entered) = '7'";
$queryMS7 = mysqli_query($connection ,$sqlMS7);
$AdvisoryMS7 = mysqli_num_rows($queryMS7);
$sqlMS8 = "SELECT * FROM Visits WHERE Company_ID = $cid AND MONTH(Date_Entered) = '8'";
$queryMS8 = mysqli_query($connection ,$sqlMS8);
$AdvisoryMS8 = mysqli_num_rows($queryMS8);
$sqlMS9 = "SELECT * FROM Visits WHERE Company_ID = $cid AND MONTH(Date_Entered) = '9'";
$queryMS9 = mysqli_query($connection ,$sqlMS9);
$AdvisoryMS9 = mysqli_num_rows($queryMS9);
$sqlMS10 = "SELECT * FROM Visits WHERE Company_ID = $cid AND MONTH(Date_Entered) = '10'";
$queryMS10 = mysqli_query($connection ,$sqlMS10);
$AdvisoryMS10 = mysqli_num_rows($queryMS10);
$sqlMS11 = "SELECT * FROM Visits WHERE Company_ID = $cid AND MONTH(Date_Entered) = '11'";
$queryMS11 = mysqli_query($connection ,$sqlMS11);
$AdvisoryMS11 = mysqli_num_rows($queryMS11);
$sqlMS12 = "SELECT * FROM Visits WHERE Company_ID = $cid AND MONTH(Date_Entered) = '12'";
$queryMS12 = mysqli_query($connection ,$sqlMS12);
$AdvisoryMS12 = mysqli_num_rows($queryMS12);
You can vastly improve the efficiency of that code by using a single query:
$query = "SELECT MONTH(Date_Entered) AS month, COUNT(*) AS total FROM Visits WHERE Company_ID = $cid GROUP BY MONTH(Date_Entered)";
$result = mysqli_query($connection, $query);
while ($row = mysqli_fetch_assoc($result)) {
$AdvisoryMS[$row['month'] = $row['total'];
}
for ($i = 1; $i <= 12; $i++) {
$sql = "SELECT * FROM Visits WHERE Company_ID = $cid AND MONTH(Date_Entered) = '$i'";
$query = mysqli_query($connection ,$sql);
$AdvisoryMS[$i] = mysqli_num_rows($query);
}
This uses a for loop to loop $i through 1-12 and puts the number of rows in an array rather than 12 separate variables. For each result you access them through $AdvisoryMS[1-12]
Related
I'm having issues with making a page where only band members can access their own band pages.
Each band in my band table has four columns $bandm1 $bandm2 $bandm3 and $bandm4.
I tried to make a script that drew the session username, and then drew the band_id from the url, and that was successful. but when i tried:
the script didn't work. is it a problem with my AND/OR statements?
EDIT:
here's my full code:
$user = $_SESSION['user_name'];
$get_user = "
select *
from users
where user_name = '$user'
";
$run_user = mysqli_query($con,$get_user);
$row=mysqli_fetch_array($run_user);
$user_name = $row['user_name'];
if(isset($_GET['band_id'])) {
$band_id = mysqli_real_escape_string($con, $_GET['band_id']);
if (ctype_alnum($band_id)){
$q = "SELECT * FROM bands WHERE band_id = '$band_id' ";
$r = mysqli_query($con, $q);
if($r){
while($row=mysqli_fetch_array($r)){
$band_id = $row['band_id'];
$band_name = $row['band_name'];
}
}
}
?>
FROM bands
WHERE band_id = '$band_id'
and (bandm1 = $user_name) OR (bandm2 = $user_name)
OR (bandm3 = $user_name) OR (bandm4 = $user_name)
it works, BUT when i replace the select with:
SELECT * FROM bands WHERE band_id = '$band_id' and (bandm1 = $user_name) OR (bandm2 = $user_name) OR (bandm3 = $user_name) OR (bandm4 = $user_name)";
it stops working
Try adding parentheses to your query:
SELECT * FROM bands WHERE band_id = '$band_id' and ( (bandm1 = $user_name) OR (bandm2 = $user_name) OR (bandm3 = $user_name) OR (bandm4 = $user_name) )
Edit :
You probably need some quotes around these variables, not sure how your script is built, but something like this :
$query = "SELECT * FROM bands WHERE band_id = '".$band_id."' and ( bandm1 = '".$user_name."' OR bandm2 = '".$user_name."' OR bandm3 = '".$user_name."' OR bandm4 = '".$user_name."' )";
i'm trying to sum a column name "total". and i want to display the total sorting by id. if user A login he can see total booking in his account.
I keep get the error:
"Notice: Array to string conversion in Array."
can someone help me? I want to echo the total in input form.
this is my php code:
<?php
include ('connect.php');
$sql = "SELECT * FROM penjaga WHERE p_username = '".$_SESSION['username']."'";
$result = mysqli_query($conn,$sql);
$row = mysqli_fetch_assoc($result);
$id = $row['p_id'];
$sql2 = "SELECT SUM(total) as total FROM sitter_kucing WHERE sitter_fk = '$id'";
$row2 = mysql_fetch_array($sql);
$sum = $row['total'];
?>
Try this,
$sql2 = "SELECT SUM(total) as total FROM sitter_kucing WHERE sitter_fk = '$id'";
$result2 = mysql_query($sql2) or die(mysql_error());
$row2 = mysql_fetch_array($result2) or die(mysql_error());
$sum = $row['total'];
i got it! thanks this is my code
this is the code:
<?php
include ('connect.php');
$sql8 = "SELECT * FROM penjaga WHERE p_username = '".$_SESSION['username']."'";
$result8 = mysqli_query($conn,$sql8);
$row8 = mysqli_fetch_assoc($result8);
$id = $row8['p_id'];
$sql9 = "SELECT SUM(total) as total FROM sitter_kucing WHERE sitter_fk = '$id'";
$result9 = mysqli_query($conn,$sql9);
$row9 = mysqli_fetch_array($result9);
$sum = $row9['total'];
?>
if (isset($_POST['submitid'])) {
$itemid = $_POST['itemID'];
$cartno = mysqli_query($connection , "SELECT * FROM users");
while ($cartnorow = mysqli_fetch_assoc($cartno)) {
$existingcartno = $cartnorow['cartno'];
$existingtotal = $cartnorow['total'];
}
$updatecartno = $existingcartno + 1;
$updateprice = $itemlistprice+$existingcartno;
mysqli_query($connection ,
"UPDATE users SET cartno = '$updatecartno' WHERE id=1");
}
When I remove WHERE id = 1 it works fine.
I seriously need to update specific id thats why I need that WHERE id = 1.
how do i optimize the following code?
How I can reduce the 4 loops to one, they are almost always the same MySQL statements. Only the step value in the WHERE is always different.
Currently the loading time for this part in the code is about 5-7 sec and that is too long ... each loop make max. 200-300 passes.
// Tank battles heute MySQL
$tday = array();
$result2 = mysqli_query($db, "SELECT * FROM activity_day_account_vehicles WHERE account_id = '$me_id' AND step = '$step'");
while($row_tday = mysqli_fetch_assoc($result2)) {
$t_id = $row_tday['tank_id'];
$tday[$t_id] = $row_tday['statistics_battles'];
}
// Tank battles gestern MySQL
$tday2 = array();
$result3 = mysqli_query($db, "SELECT * FROM activity_day_account_vehicles WHERE account_id = '$me_id' AND step = '$step2'");
while($row_tday2 = mysqli_fetch_assoc($result3)) {
$t_id = $row_tday2['tank_id'];
$tday2[$t_id] = $row_tday2['statistics_battles'];
}
// Tank battles Woche MySQL
$tweek = array();
$result4 = mysqli_query($db, "SELECT * FROM activity_day_account_vehicles WHERE account_id = '$me_id' AND step = '$step3'");
$menge2 = mysqli_num_rows($result4);
if ($menge2 === 0) {
$result4 = mysqli_query($db, "SELECT * FROM activity_day_account_vehicles WHERE account_id = '$me_id' ORDER BY step ASC LIMIT $anzahl2");
}
while($row_tweek = mysqli_fetch_assoc($result4)) {
$t_id = $row_tweek['tank_id'];
$tweek[$t_id] = $row_tweek['statistics_battles'];
}
// Tank battles Monat MySQL
$tmonth = array();
$result5 = mysqli_query($db, "SELECT * FROM activity_day_account_vehicles WHERE account_id = '$me_id' AND step = '$step4'");
$menge2 = mysqli_num_rows($result5);
if ($menge2 === 0) {
$result5 = mysqli_query($db, "SELECT * FROM activity_day_account_vehicles WHERE account_id = '$me_id' ORDER BY step ASC LIMIT $anzahl2");
}
while($row_tmonth = mysqli_fetch_assoc($result5)) {
$t_id = $row_tmonth['tank_id'];
$tmonth[$t_id] = $row_tmonth['statistics_battles'];
}
Create indexes on account_id and step:
CREATE INDEX activity_day_account_vehicles_001
ON activity_day_account_vehicles (account_id);
CREATE INDEX activity_day_account_vehicles_002
ON activity_day_account_vehicles (step);
Im trying to generate an array but not sure how to go about it.
I'm currently getting my data like so:
$query = mysql_query("SELECT * FROM users WHERE userEmail LIKE 'test#test.com'");
$row = mysql_fetch_array($query);
$query1 = mysql_query("SELECT * FROM categories");
while($row1 = mysql_fetch_array($query1)){
$query2 = mysql_query("SELECT * FROM usersettings WHERE userId = ".$row['userId']." AND usersettingCategory".$row1['categoryId']." LIKE 'y'");
$isyes = mysql_num_rows($query2);
if($isyes > 0){
$cat1 = mysql_query("SELECT * FROM shops WHERE shopstateId = 1 AND (categoryId1 = ".$row1['categoryId']." OR categoryId2 = ".$row1['categoryId']." OR categoryId3 = ".$row1['categoryId'].")");
$cat1match = mysql_num_rows($cat1);
if($cat1match > 0){
while($cat1shop = mysql_fetch_array($cat1)){
$cat1msg = mysql_query("SELECT * FROM messages WHERE shopId = ".$cat1shop['shopId']." and messagestateId = 1");
while($cat1msgrow = mysql_fetch_array($cat1msg)){
echo $cat1msgrow['messageContent']." - ".$cat1msgrow['messageCode'];
$cat1img = mysql_query("SELECT shopimagePath FROM shopimages WHERE shopimageId = ".$cat1shop['shopimageId']);
$imgpath = mysql_fetch_array($cat1img);
echo " - ".$imgpath['shopimagePath']."<br/>";
}
}
}
}
}
But this can cause duplicates when a user has all 3 of a shops categories picked in their preferences. I am trying to find a way to just pull the message ID out instead of the whole thing and put it into an array giving me, for example:
1,3,5,7,1,3,5,2,4,7,8
Then I can just run a separate query to say get me all messages where the ID is in the array, but i am unsure of the most constructive way to build such an array and examples of array from a while loop I have seen do not seem to be what I am looking for.
Is there anyone out there that can push me in the right direction?
Can't help with this code. But if you want an array from a query without duplicate result, you can use " select DISTINCT (id) " in your query or for more simple solution :
$id_arr = array();
$sql = mysql_query("select id from id_table");
while ($id_result = mysql_fetch_array($sql) {
$id = $id_result['id'];
if (!in_array($id, $id_arr)) {
$id_arr[] = $id;
}
}
I have found a much easier way to create the required result. I think at 6am after a hard night coding my brain was fried and I was making things a lot more complicated than I needed to. A simple solution to my issue is as follows:
$query = mysql_query("SELECT * FROM users WHERE userEmail LIKE 'test2#test2.com'");
$row = mysql_fetch_array($query);
$categories = "(";
$query1 = mysql_query("SELECT * FROM categories");
while($row1 = mysql_fetch_array($query1)){
$query2 = mysql_query("SELECT usersettingCategory".$row1['categoryId']." FROM usersettings WHERE userId = ".$row['userId']);
$row2 = mysql_fetch_array($query2);
if($row2['usersettingCategory'.$row1['categoryId']] == y){
$categories .= $row1['categoryId'].",";
}
}
$categories = substr_replace($categories ,")",-1);
echo $categories."<br />";
$query3 = mysql_query("SELECT * FROM shops,messages WHERE shops.shopId = messages.shopId AND messages.messagestateId = 1 AND (shops.categoryId1 IN $categories OR shops.categoryId2 IN $categories OR shops.categoryId3 IN $categories)");
while($row3 = mysql_fetch_array($query3)){
$query4 = mysql_query("SELECT shopimagePath FROM shopimages WHERE shopimageId = ".$row3['shopimageId']);
$row4 = mysql_fetch_array($query4);
echo $row3['messageContent']." - ".$row3['messageCode']." - ".$row4['shopimagePath']."<br />";
}