Jump to content

btherl

Staff Alumni
  • Posts

    3,893
  • Joined

  • Last visited

Everything posted by btherl

  1. If the select returns multiple rows, then you must choose which row you want. I recommend printing out $db for verification. It may not be what you expect.
  2. If by "optimize" you mean fewer lines of code or neater appearance, then that may be true, depending on your point of view. You can also write an "if" in one line, though I don't think it looks as nice as the ternary operator. I would not expect it to be any faster to execute.
  3. I've simplified some bits .. I also renamed some variables to more suitable names, and improved the error checking on the mysql query. As for improving the code in general, I think you'll need to tell us what it does The other change I made is to protect against sql injection by allowing only digits in $year and $month. And the other thing is the syntax of building strings. I think that this way is clearer. The {} is to protect a "complex" variable, like an array with an index, when it's inside a double quoted string. <? include('../qazwsxedc/config.php'); mysql_connect($server,$username,$password); mysql_select_db($database) or die ("Unable to connect to the database"); $year=$_POST['year']; $month=$_POST['month']; # Sanitize to protect against SQL injection $year = preg_replace('|[^[:digit:]]|', '', $year); $month = preg_replace('|[^[:digit:]]|', '', $month); $min="$year.$month.00"; $max="$year.$month.32"; $sql = "SELECT event_date FROM calendar_events WHERE event_date>'$min' and event_date<'$max' and status='confirmed'and event_type='gig' "; $result = mysql_query($sql) or die("Error in $sql\n" . mysql_error()); $x=mysql_num_rows($result); while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $dates = explode("-",$row["event_date"]); $name.="{$dates[2]}.{$dates[1]}.{$dates[0]} "; } echo "resultstr=".$name; ?>
  4. include() and require_once() are not really appropriate here. In any case, all you will be able to access is the output of the remote script, not the functions within the script. What you can do however, is have the remote script take the necessary arguments, and return the output your script needs. So the script itself is like a single function (or many functions, with an argument to select which function). The simple method here is to use functions like file("https://remotesite.com/script.php?arg=foo"), providing your php supports https wrappers. The more complex but more configurable method is using Curl (about which much information can be found through google, and in the forum here).
  5. Ok, I've sorted it out. Here's the fast trie algorithm (pseudocode only .. if you like it, I might be convinced to write real code) 1. Each trie node is labelled with a single letter 2. The root node is the labelled with nothing, and represents the start of a word 3. Each node contains a list of words containing all letters up to that point 4. There are up to 26 children of each node (assuming 26 letters). 5. Nodes lower in the tree will have fewer children, because all children must have letters greater in the ordering than their parent. 6. Any path from the root to a leaf passes letters in sorted order. Example. Word list: the ten cat Trie: root a e c h n t t t | | | cat the ten To find matching words, you start at the top. Follow all children which match any letter in your letter list. If your letter list was "aent", you would follow the left path for only one step and then fail there. But you could follow the right-most path all the way to the bottom and find the word "ten". If your word list contained "he" and your letter list also contained "e" and "h", you would find "he" after following the "e" and "h" nodes. This algorithm (providing the trie is pre-generated) will run much faster than the linear search method.
  6. I think I misunderstood the question.. there is a flaw in Orio's suggestion though. The word "arm" will not match, because it does not contain the letters f and l. Instead you could use a regexp match in place of the strpos. preg_match('|^a?f?l?m?r?u?$|', sort_letters($word)) That expression will allow missing letters in the middle. I'm sure there must be a more efficient algorithm though .. and I'm sure some sort of modified trie will work here.
  7. That's a tough one. Is your word list likely to change? Is the list of characters likely to change? If one is going to be mostly static, then we can choose an algorithm that takes advantage of that. Assuming the word list of mostly static (that is, changes are infrequent) we can re-arrange it into a structure that aids the search. A trie of some sort may be good. Perhaps a reverse trie, where the nodes tell you what letter isn't in a word. "All letters in the list must be present" is equivalent to "No letter in the list may not be present", so reversing the logic like that may open up better algorithms.
  8. obsidian hinted at this, but don't put that code out for public use! It will allow your users to execute any command they want on your webserver. You can avoid this by sanitizing the input, for example: $safe_eq = preg_replace('|[^[:digit:]()+*/-]|', '', $eq); That will allow only digits and the following symbols: ( ) + * / - I think that ought to be safe.. at least I can't imagine anything you could do with that other than causing the eval to fail due to syntax error.
  9. Instead of $update = mysql_query("UPDATE competitions SET competitionname = '$var10', favorite = '$var11', underdog = '$var13', pointspread = '$var12', day = '$var15', month = '$var14', year = '$var16' WHERE competitionid = '$var17'") or die(mysql_error()); do this: $sql = "UPDATE competitions SET competitionname = '$var10', favorite = '$var11', underdog = '$var13', pointspread = '$var12', day = '$var15', month = '$var14', year = '$var16' WHERE competitionid = '$var17'"; $update = mysql_query($sql) or die("Error in $sql\n" . mysql_error()); The second version will print the exact query you tried to execute, so you can easily see what went wrong. Make the same change to both queries, and you should get enough information to fix it!
  10. Can you post the code for the form? If you are using post method in the form, then you must check $_POST instead of $_GET (or $_REQUEST, which includes both methods)
  11. Is it possible that the query returns no matches? Add this: echo "Query returns ", mysql_num_rows($eventdata), " rows";
  12. Isn't recommended by who, and why?
  13. What happens when you run your code? Edit: <> means "not equal", same meaning as !=, so it's ok to use.
  14. What would you be multiplying them together for? I understand why you would want to take the average. As for how to do it, base_convert() will convert a hex string into an integer, after which you can use normal maths on it. Or are you looking for a method that will operate on actual hex strings themselves? Edit: And for averaging the colours, you would need to split the colour into the red green and blue components before doing the maths (at least that makes it easier).
  15. For base conversion, it's best to use base_convert() The reason is that "0x" is part of php's syntax, and is not considered when turning strings into integers. You could use eval() to force it to interpret it as php syntax, but that's overkill.
  16. btherl

    join help

    Is buddy_id and user_id unique? If so you can drop the group by. You can also drop the where condition, it doesn't do anything. So: SELECT `buddy_id`,`buddy_order`, `user_name`, `user_lastvisit` FROM buddy JOIN users ON buddy.buddy_id = users.user_id ORDER BY `buddy_order`, `buddy_id` If you need to specify which table to select a column from, you can do it like this: SELECT `buddy`.`buddy_id` ...
  17. btherl

    join help

    I don't think that will help. SQL optimizers are very good, and can do that kind of optimization just by examining the select list, and seeing that you don't need all those extra rows. I'll suggest this thread be moved to the mysql forum so the experts can take a look. I may be wrong.
  18. btherl

    join help

    Try: SELECT buddy_id FROM buddy LEFT JOIN users ON buddy.buddy_id = users.user_id ORDER BY `order`, `id` I don't know if that order by will work unless you put those columns in the select list though. Wolphie, the default ordering is ascending. There's no need to specify.
  19. If you set the limit clause based on a variable set by the user, then that's enough. The foreach can display all results returned from mysql. And yes, I would make it an unchangeable default first. Doing things 1 at a time makes life easier. I wouldn't hardcode it everywhere though, I would set it as a variable at the start and use that variable throughout the code. Pagination is always messy .. the approach I've used is to break it down into seperate tasks 1. Generate "First" link 2. Generate "Prev" link 3. Generate links before current page 4. Generate links after current page 5. Generate "Next" link 6. Generate "Last" link All of those tasks are simple in themselves. And if you do them all, then you have all the links you need for full featured pagination. I would also include a "jump to page" feature.
  20. If there is a single tab, then you can use a solution like this The above method is for making a popup on exit of a site, but the same principle can be used to log the user out on exit. It's not 100% reliable, and will definitely fail if the user opens multiple tabs on your site. I know this isn't an answer to your question, but if you look at all the major social networking sites, none of them log the user out on closing of a tab. It's just not practical. Instead they use user-configurable timeouts (or fixed timeouts), as well as the standard of having cookies expire when the browser closes.
  21. 6 left joins is not so bad as long as the left side of each join has a small number of results, AND the right side of the join uses an index. Say you have 3 results on the left, and a matching index on the right. Then that is only 3 index scans into the right-hand table to execute the join. If those conditions aren't met though, then you may be in for a long query If you're finding that performance is not acceptable, then there are many generic approaches, including APC (alternative php cache) and memcached (memory cache daemon), as well as other forms of caching. Then there is specific optimization, which will depend on your queries and data. I tend to use caching when results get into the thousands, as it takes too long to generate them for each request. Multiple levels of caching can be very effective when you find the right level at which to cache. Such as caching a topic's data, caching an entire forum's data, caching a post's data. Each cache is a different level, and some will be more effective than others.
  22. This function can give you the caller's file and line number: debug_backtrace()
  23. Try adding a timeout to ftp_connect(), and see what happens. It sounds as if your host may not allow ftp connections to the outside. If that's the case, they may still allow it via a proxy server.
  24. Can you give some more detail? Are you accepting EUC-JP data from a form and submitting it to a postgresql database?
  25. That means that the postgresql extension for php is not installed (or is not configured for use with php). Are you using windows or unix? Each has a slightly different way to enable the extension. The file php.ini controls which extensions are loaded.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.