-
Posts
5,448 -
Joined
-
Days Won
174
Everything posted by mac_gyver
-
Question about a query I thought would be a lot simpler!
mac_gyver replied to jbradley04's topic in MySQL Help
if you didn't want duplicates, you should have mentioned that up front as the method to accomplish that is different. you cannot sneak up on a problem in programming as computers don't like sneaky programmers. -
see the posts near the end of this thread - http://forums.phpfreaks.com/topic/279088-question-about-a-query-i-thought-would-be-a-lot-simpler/
-
that's correct, except that early versions of php introduced a number of short-cuts that were depreciated, turned off by default, with some having finally been removed in the latest php5 version. so actually, well written and up to date php4 scripts should work in php5. you will need to debug each problem or simply go through each script and update it taking into account the deprecated features the code may be using (this will require that you have knowledge of what these features are and can identify where they are being used at in the code). if you want us to help, you need to post specific code and any errors or incorrect result it produces.
-
mysql_real_escape_string() not escaping single quotes
mac_gyver replied to glen-rogers's topic in PHP Coding Help
about the only way the posted code could produce that error is if the ' character is actually a multi-byte encoded character. So, either that's not the actual code that is running or the character encoding isn't being taken into account by the mysql_real_escape_string function. are you sure about the posted code? could you have a version without the mysql_real_escape_string in it that is actually what is running on your server? what's the character encoding defined for your database table? are you specifically selecting that character encoding after your open a database connection in your code? -
mysql_real_escape_string() not escaping single quotes
mac_gyver replied to glen-rogers's topic in PHP Coding Help
what error do you get? -
mysql_real_escape_string() not escaping single quotes
mac_gyver replied to glen-rogers's topic in PHP Coding Help
you are probably not using the output from the mysql_real_escape_string function. post your code to get help with it. -
your use of properties is suspect. the point of properties (class variables) are for things that must be referenced from outside of where they are assigned. this includes values that are used in more than one method inside the class and values you want to reference in the scope of where your class is called. none of your properties are referenced outside the method where they are created. you don't need to use a property for every variable. local variables inside of methods are perfectly valid. in the code you posted, there's no need for any of the properties you have shown. the only thing that is dynamic in the code you posted is the content section (though the <title> ... </title> should be.) the rest of your code is just a page layout/template that you have broken up and wrapped class methods around, requiring more code to produce the result. your class methods/properties should be concerned with accepting the input that is dynamic, preforming the processing you need on that input (using htmlentities() on the text you output on a page would be some good processing to include in your code), and returning the result. the main code that uses your class should be only concerned with USING your class and it should be as simple as possible. your main code should not care about things that don't involve information that doesn't change. your header/footer add code is completely static and your main code shouldn't know or care about that. you main code should only be - <?php include " your class definition file "; $mob_page = new create_page(); //add dynamic content to the page template and echo the result echo $mob_page->new_page("page1", "content - can be dynamic or static :)", "footer");
-
the DECIMAL data type is the best for storing your money, inventory, prices as it avoids floating point conversion errors. if your inventory is whole units only (integers) you should use an INT type for that.
-
Question about a query I thought would be a lot simpler!
mac_gyver replied to jbradley04's topic in MySQL Help
it turns out a REGEX in the query directly solves this - SELECT word FROM words WHERE word REGEXP '^[drea]+$' and if you throw in a length check, it will do your second assignment - SELECT word FROM words WHERE word REGEXP '^[drea]+$' AND CHAR_LENGTH(word) = 4 -
Question about a query I thought would be a lot simpler!
mac_gyver replied to jbradley04's topic in MySQL Help
i'm not sure you can do all of this in a query, but one fairly efficient way (over forming all the permutations of the target letters) would be - 1) use a query to fetch all words that have any of the target letters. you should be able to use a mysql REGEXP match to do this without building the list of (... like) OR (... like )... terms. 2) when retrieving the words from the query, remove all the target letters from the words and any result that is zero length was made up of only the target letters. any result that has a non-zero length after removing the target letters isn't a possible word. -
Second pair of eyes, why is the query failing to execute.
mac_gyver replied to Failing_Solutions's topic in PHP Coding Help
you cannot run multi-queries in one mysql_query() statement. you must run each query separately edit: or make one multi-value insert query. -
Newbie in tangled web of php/sql, help untangling
mac_gyver replied to elso's topic in PHP Coding Help
you need to look past the actual database library that Barand used and 'look' at the program logic that he showed you. it's the program logic in his code that does what you want. -
here's a reorganization of your code the way you probably intended it to be - <?php $currentTime = time(); // current time (seconds since UNIX epoch) require_once($_SERVER['DOCUMENT_ROOT'] . '/includes/dbconn.php'); /* Get average wages for all companies */ $sth = $dbh->query("SELECT AVG(employee_wage) FROM company_facilities"); $avg_wage = $sth->fetchColumn(); /* prepare the Get average unit_price for all companies query */ $pq_select_ave_price = $dbh->prepare("SELECT AVG(unit_price) FROM company_facilities WHERE unit_price > 0 AND specialty = ?"); /* prepare the Update facility units query */ $pq_update_co_fac = $dbh->prepare("UPDATE company_facilities SET units_in_stock = units_in_stock - ?, sold_last_hour = ? WHERE id = ?"); /* prepare the Update company money query */ $pq_update_co = $dbh->prepare("UPDATE companies SET money = money + ? WHERE id = ?"); /* prepare the Insert into company finance ledger query */ $pq_insert_co_fin = $dbh->prepare("INSERT INTO company_finance (date, company_id, company_name, type, amount, description) VALUES (?, ?, ?, ?, ?, ?)"); $pq_insert_co_fin->bindParam(1, $date); $pq_insert_co_fin->bindParam(2, $company_id); $pq_insert_co_fin->bindParam(3, $company_name); $pq_insert_co_fin->bindParam(4, $type); $pq_insert_co_fin->bindParam(5, $amount); $pq_insert_co_fin->bindParam(6, $description); /* Grab the company facility info from the company_facilities table */ $stmt = $dbh->query("SELECT * FROM company_facilities"); while($data = $stmt->fetch()) { // the following 6 values are already in $data $wage = $data['employee_wage']; $employees = $data['employees']; $managers = $data['managers']; $equipment = $data['equipment']; $price = $data['unit_price']; $units_in_stock = $data['units_in_stock']; /* Get average unit_price for all companies */ // SELECT AVG(unit_price) FROM company_facilities WHERE unit_price > 0 AND specialty = ? $pq_select_ave_price->execute(array($data['specialty'])); $avg_price = $pq_select_ave_price->fetchColumn(); /* Calculate employee to equipment ratio */ $ratio = $equipment/$employees; if($ratio > 1){ $ratio = 1; } else{ $ratio = $ratio; } /* Calculate the difference between wage and average wage */ $difference = $wage-$avg_wage; /* Calculate Wage Modifier */ $wage_modifier = ($difference/$avg_wage)*1.8; if($wage_modifier > 0){ $mod = $difference/$avg_wage; $wage_modifier = (pow(sqrt($mod),1.2))*1.4; } /* Get the units per hour number */ if($data['field'] == "Mining"){ $UPH = 0.672; } if($data['field'] == "Farming"){ $UPH = 0.672; } if(in_array($data['specialty'],array("Aluminum","Steel","Glass","Rubber","Fabric","Plastic"))){ $UPH = 0.336; } if(in_array($data['specialty'],array("Leather","Chassis","Body","Window","Tires","Interior","Engine"))){ $UPH = 0.168; } if($data['specialty'] == "Vehicle"){ $UPH = 0.084; } if($data['field'] == "Retail"){ $UPH = 0.042; } /* Calculate preliminary sales number */ $pre_sales = $UPH*$employees; /* Calculate the equipment modifier */ $equipment_modifier = $ratio/4; /* Calculate the Manager modifier */ $manager_modifier = sqrt(($pre_sales*(0.1*$managers))); $manager_mod_max = $employees/10; if($manager_modifier > $manager_mod_max){ $manager_modifier = $manager_mod_max; } // modifiers only affect production, not sales, for the following facilities. if(in_array($data['field'],array("Mining","Farming","Production"))){ $wage_modifier = 0; $equipment_modifier = 0; $manager_modifier = 0; } /* Calculate the price modifier */ $price_modifier = (($price-$avg_price)*0.25)*-1; /* Finally, calculate the total sales */ $sales = floor($pre_sales+$equipment_modifier+$wage_modifier+$price_modifier+$manager_modifier); if($units_in_stock < $sales){ $sales = floor($units_in_stock); } $revenue = $sales*$price; /* Update facility units */ // UPDATE company_facilities SET units_in_stock = units_in_stock - ?, sold_last_hour = ? WHERE id = ? $pq_update_co_fac->execute(array($sales,$sales,$data['id'])); /* Update company money */ // UPDATE companies SET money = money + ? WHERE id = ? $pq_update_co->execute(array($revenue,$data['company_id'])); /* Insert into company finance ledger */ $date = $currentTime; $company_id = $data['company_id']; $company_name = $data['company_name']; $type = "revenue"; $amount = $revenue; $description = "Product Sales ({$data['facility_name']})"; // INSERT INTO company_finance (date, company_id, company_name, type, amount, description) VALUES (?, ?, ?, ?, ?, ?) $pq_insert_co_fin->execute(); } a couple of suggestions - 1) you should store dates/times in your database using a DATE or DATETIME data type. do not store unix timestamps as they are problematic due to the time zone/dst conversion needed to display them in a human readable format. if you store a YYYY-MM-DD HH:MM:SS value in your database, it will always be that value regardless of any timezone or daylight savings settings in effect on your server. you will also be able to directly use all the mysql date/time functions in your queries to manipulate/process your date/time values. 2) you should not store both the company_id and company_name in the company_facilities and company_finance tables. what happens when someone wants to change their company name? the company_name should only be stored in one place, where it is associated with the company_id (probably in your companies table.) to display the company_name using the company_id, you would simply join whatever table you are querying with the companies table to access the company_name value.
-
Barand's questions weren't about what the values should be, but were for you to look at what the values actually are. because your code is testing what the values are and your code is detecting that they aren't what you expect, knowing what they actually are would point to the cause of the problem. stated another way, your code is trying to use data values that would only be present for a successful upload, before your code has even tested that the upload worked. check if the upload worked first, then try to test the data values from the upload. for a successful upload, $_FILES["file"] will be set and $_FILES["file"]['error'] will be equal to a zero.
-
as a continuation of the above reply, it appears that your final INSERT INTO company_finance query should just be executed inside the main loop, which is where the correct $revenue value for each row exists. also, one of the points of using prepared queries is if you need to execute them more than once with different data, you prepare them once (not inside of any looping) and execute them when you need to.
-
the only place you are calculating $revenue is inside the loop above that one. are you sure the zero isn't the value leftover from the last pass through that previous loop? have you echoed $revenue at the point you are trying to use it so that you know what is in it? BTW - you are killing your server with all those queries inside of loops retrieving the same data you already retrieved in the first query and in the case of the $avg_wage, you are retrieving the same value over and over. you have at least two-times too much code and about 8 times too many queries.
-
what have you done to troubleshoot the problem? is the query running without any errors (you have no error checking logic so that you even know if the query ran or not)? when you echoed the query is it exactly what you expect it to be (i know of one thread of your's that i helped in where you had an extra space in the data your form submitted)? are you 100% sure you have matching data in your table (have you ran the echoed query directly against your database using your favorite database management tool)? you have several hundred posts on this forum. you should be able to troubleshoot what your code is doing at this point, rather than just posting it and expecting others to come up with suggestions to try or to see the reason why it is not working.
-
you have got a fatal parse/syntax error on line 13. if you had stated in the first post in the thread what result you were getting, the first reply wouldn't have been wasted. you need to have php's error_reporting set to E_ALL and display_errors set to ON in your php.ini to show fatal parse/syntax errors in your main file. as to what is causing that error, you need to research the syntax for a foreach(){} statement. a foreach(){} statement loops over ONE array.
-
Trying to display data from databse in a php table
mac_gyver replied to BrainBoxMad's topic in PHP Coding Help
your actual data output doesn't contain any <tr><td>...</td>...</tr> tags, so it is just output as is. you would probably want to add those tags if you want the data to be displayed in the table. you will also want to close the table after all the output with a </table> tag. -
since you haven't stated what processing and result you do want and under what conditions you want it, no one can help with the problem. your posted code and statements in the thread doesn't tell us that information. if your code demonstrated what you were trying to accomplish, your code would be working and you wouldn't be posting on a help forum. defining the specific and detailed inputs, processing, and output/result is a key first step in all programming and clearly communicating that definition would be required if you want someone to help you.
-
you need to define what you want that block of code to do and if you want us to help, share that definition. what are the input(s), what processing do you want, and what are the output(s). the posted code has no external input (other than being requested.) best i can tell is you want to find if there are any row(s) with 'empty' as a value and delete one or all of them. you can do that with one query.
-
most of the people reading this charge in the neighborhood of $100 US per hour, minimum of one hour. are you sure you wouldn't like to attempt to learn and solve this yourself? the Php Coding Help forum is for programmers to get help with their code. if you want someone to write this code for you, post your request in the Freelancing forum section. all you did to the code was to copy/paste the contents of the file you were previously including, at the point where you were including that file. that's not what someone suggested and in fact you were pointed to an example in your own code that shows how to dynamically display an image.
-
do you have a state-able goal for that code? most of that code doesn't do anything or is repeating something the code already did in a previous step. you are even throwing more code in because you are using a _fetch_array() statement and getting both numerical and associative indexes in your data.
-
the combined code you have posted does not produce the error you have implied. try again i didn't ask for the filename and line number from the error message because i needed the typing practice. you have missread or misinterpreted the information that you saw in front of you we cannot help you based on the information you have provided.