Jump to content

aschk

Staff Alumni
  • Posts

    1,245
  • Joined

  • Last visited

    Never

Everything posted by aschk

  1. Corrections FTW (for the win)! <?php // SQL query with CORRECT syntax $sql = "SELECT * FROM TestTable"; $q = mysql_query($sql); while ($f = mysql_fetch_array($q)) { // Correction array key as STRING if ($f['Condition']) { // The above condition will ONLY pass if $f['Condition'] expression > 0 // So you need to know how PHP treats strings/ints/arrays/constants as test cases. echo 'Condition Passed'; } } ?>
  2. A tip for spreading code readability. Try this instead: <?php // Query with replacement token(s). $query = "SELECT * FROM event_db WHERE eddate LIKE '%s%%' ORDER BY edate ASC"; // Query with parameters. $query = sprintf($query, $keyword); // Query execution. $sql=mysql_query($query, $con); ?>
  3. urlencode() 'ing a serialized string and sending it over a GET request for teh facepalm.
  4. The OP has only asked for an answer to his question, which has been given. As to how she/he intends to utilise this information we don't know yet. I'm sure it will become apparent soon
  5. Well you have 2 options. 1) Use PHP to decide which rows are alternate and supply a class="odd" to the <tr> element 2) Use CSS pseudo-selectors for "nth-child: background: black" or something along those lines (only works with CSS2+ compliant browsers)
  6. I discovered something recently, which might give you what you're after. Stick the following at the top of your code: <? ob_implicit_flush(true); ob_end_flush(); // ... rest of code ?> I have however noticed a rather disturbing lag in the closing of the PHP/Apache/TCP connection, so I would approach this with caution.
  7. I have another suggestion. You can set an array of input values, like so: <form method="POST" action="FootyForm_P.php"> <input type="text" name="names[]" /> <input type="text" name="names[]" /> <input type="text" name="names[]" /> <input type="text" name="names[]" /> </form> Then you can check for duplicates as follows: <?php if(isset($_POST['names'])){ $names = $_POST['names']; if(count($names) !== count(array_unique($names))){ echo "Duplicate name found"; } } ?> It would probably be more informative however to highlight the boxes with the duplicate names. I shall leave that as an exercise for you to do.
  8. Here's a PHP5 valid version of your code with PHPDoc comments: <?php /** * Admin Class * @author br3nn4n */ class admin { /** * Name of the page. * @var string */ private $_page; /** * String containing the page content. * @var string */ private $_content; /** * Constructor * @param string $page */ function __construct($page) { $this->setPageName($page) ->initDB(); } /** * Initialise the database connection. */ protected function initDB() { mysql_connect('......stripped....'); mysql_select_db("wri"); mysql_query("SET NAMES 'utf8'"); return $this; } /** * Set the internal page name. * @param string $page * @return admin - Allows chaining of methods. */ public function setPageName($page){ $this->_page = $page; return $this; } /** * Execute SQL query and return 1st row as numeric indexed array. * @param string $query * @return array */ public function query($query) { $q = mysql_query($query) or die("Error, please try later"); $q = mysql_fetch_array($q); return $q; } /** * Get the title based on page name. * @return string */ public function title() { // some crazy querying shit here... $title = ucfirst($this->getPageName()); return $title; } /** * Retrieve the name of the page. * @return string */ protected function getPageName(){ return $this->_page; } /** * Calling page() loads the content from the DB internally. * @return admin - Allows method chaining. */ public function function page() { $page = $this->query("SELECT * FROM pages WHERE page='{$this->getPageName()}' AND administrative='1'"); // This is a guess but i'm loading the 1st part of the return query array as the content. $this->_content = $page[0]; return $this; } /** * Get the "page" content. * @return string */ public function content() { if(!$this->_content){ $this->page(); } return $this->_content; } } /** * @example */ $admin = new admin(); print $admin->content(); ?>
  9. I'm taking a guess here (because i've not checked MySQL docs) but if you make your email column a unique index (as you should be doing if it's unique anyway) then the INSERT ... ON DUPLICATE KEY UPDATE... should work for you. However, because you don't actually want to UPDATE anything on a DUPLICATE then you're better off using IGNORE e.g. INSERT IGNORE INTO <tablename>('col', ....) VALUES(1,...); However, because an INSERT has a higher overhead than a SELECT you would probably be better off doing the SELECT 1st (as you were). Going back to the original point SELECT query, I see no need for the LIMIT 1 because if you're saying that your email field is unique (because you don't accept dupes) then you will only ever get 1 result. Thus: <?php /** * Email class for performing acts on email addresses. */ class Email { private $_email; private $_isDuplicate; public function __construct($email, $db = null){ $this->_email = $email; if(null === $db) { $db = mysql_connect('localhost','username','password'); mysql_select_db('database'); } $this->_db = $db; } private function getDB(){ return $this->_db; } public function getEmail(){ return $this->_email; } public function isDuplicate(){ if(null === $this->_isDuplicate){ $this->_isDuplicate = false; $result = mysql_fetch_row(mysql_query( sprintf( "SELECT COUNT(*) FROM customer WHERE email='%s'", mysql_real_escape_string($this->getEmail()) ) , $this->getDB() )); if($result[0]){ $this->_isDuplicate = true; } } return $this->_isDuplicate; } } /** * @example */ foreach($emails as $email){ $test = new Email($email, $db); if(!$test->isDuplicate()){ // INSERT new record. } } ?> The above code is pretty extravagant for what you're looking for, but you get the idea I hope.
  10. You can't as crayon said, PHP is server side, it opens a connection, sends some html, closes a connection and it's done. The only way to do something after that is to have the client side (via client-side code i.e. javascript) perform a routine after a period of time. Telling PHP to sleep won't work because you've already sent your HTTP headers to the client, even if you maintain an open connection, so you can't issue an HTTP redirect. Thus, it brings me back to the fact that you have to client-side redirect.
  11. Use an include to include video.php in your other templates. And for editing the file, just open the file using php file functions (fopen, fread, fwrite, fclose, etc), instead of loading a database field into your textarea. It's just the same really. Wordpress does it
  12. That apostrophe isn't part of the normal character set. it's most probably being pasted from a word document as something like a backtick (`). Thus your problem is that your database isn't storing your data as UTF-8. So I suggest your first port of call is to alter the table schema to UTF-8 first.
  13. Ok, so what error are you getting (echo mysql_error()) and what does $qry['title'] equal?
  14. What you can do instead if INSERT .. ON DUPLICATE KEY UPDATE... e.g. INSERT INTO table(visit) VALUES(0) ON DUPLICATE KEY UPDATE SET visit = visit+1
  15. I'm not sure how MySQL treats natural joins these days but I recommend reformatting your SQL to look a bit more like this: SELECT * FROM table1 t1 JOIN table2 t2 ON t1.userid = t2.userid WHERE t1.name = 'jon' AND t2.gender = 'male'
  16. You have a typo in your 2nd piece of code '$qry[title}' should be '$qry[title]'
  17. The dash isn't actually a normal dash, you'll find it's a nasty MSWord elongated dash... yuck! One is hoping you're storing all your database in your database as UTF-8, and you will also need to make sure your page encoding on output is UTF-8 also.
  18. /facepalm Did you even try? <?php preg_match("/([0-9]{4}).([0-9]{1,2}).([0-9]{1,2})/", $data[$nn], $regs) ?>
  19. Perhaps you'll find that allow_url_fopen is disabled in your server PHP environment, and if you're getting an undefined function error I expect the version of PHP you're running in production is lower than what you're developing in.
  20. Use include instead of echo/print, I believe this should resolve your problem.
  21. I believe you can set certain php.ini values using .htaccess or your virtual host config (depending on hosting limitations). You can also try doing the following in your code: <?php ini_set("session.gc_maxlifetime", 7200); ?> Assuming their session times out, when they submit their post would you require them to log in again? If that's the case then i'm guessing you redirect them to a login page. What you need to do is create a new session for them using their old session cookie value, save their post data (in session), and process their login and then post in the same step.
  22. Does your prodution server support output buffering? Does it support <??> short tags. What mail program/server is it using for mail()? Also bear in mind that you're line delimiters might be different per environment (which is most probably your issue). To be safe use "PHP_EOL" constant instead of relying on your PHP script + environment to contain the correct line delimiters. And last but not least, what does your error logs say?
  23. I don't quite see how you're getting a relevance between "country" and "ip_address", and why you would want both in visits table. Because 1 IP will ALWAYS be from the same country. What you want is a count of visits by IP. Then you want to correlate that IP to a country. There's no point storing the IP address and the country together in the same table, because you'll just be duplicating the country information every time that IP address occurs. I think you need to define what tables you have better, and also what you want out of them. To get a count for an IP address: SELECT ip_address, COUNT(*) FROM visits GROUP BY ip_address
  24. In a phrase, "You can't". Because the characters were trunced to 2 bytes (i'm assuming previous collation was latin_1 based), you have lost the end 2 bytes of the utf-8 character that you attempted to insert. Therefore you don't know what character the original was.
×
×
  • 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.