Jump to content

Barand

Moderators
  • Posts

    24,563
  • Joined

  • Last visited

  • Days Won

    822

Everything posted by Barand

  1. Have you considered sorting the records and just returning the first record?
  2. Why have you started a new topic. Now I'll have to search the forum for your earlier one to get your original table structure to give a revised solution. One day, maybe.
  3. I've done a graphical version for you to play with (attached) which creates an image. Place on a page with an ordinary HTML image tag <img src='pigeon.php' width='500' height='500' alt='pigeon'/> pigeon.php
  4. If that is your password above, I would change it immediately now you have published it
  5. Sorry, missed those $xml = simplexml_load_string($str); $acopy = array(); foreach ($xml->match as $match) { foreach ($match->linkset->link as $link) { $acopy[(string)$match->matchname][] = array( (string)$link['channelname'], (string)$link['lang'], (string)$link['kbps'], (string)$link ); } } echo '<pre>',print_r($acopy, true),'</pre>';
  6. Where you have the line $dayofyear = date('z'); you could also try checking the value of "date('Y-m-d H:i:s')" to see if the datetime is what you would expect it to be or if it out by several hours. If it is out, try adjusting the timezone. Your second point I cannot understand as the link value is created on the server and will be sent to all browsers ???
  7. See "Ternary operator" on this page http://php.net/manual/en/language.operators.comparison.php Basically echo (condition) ? 'A' : 'B'; is shorthand for if (condition == true) { echo 'A'; } else { echo 'B'; } In your code it is useless as it echos the same value regardless of the test.
  8. attribute values need to be in quotes, otherwise it will only include the first word <input type="text" name="First" value="<?=$FirstLine?>">
  9. from manual for fgets() Nothing about stopping at space characters.
  10. try removing the final comma
  11. I hardcoded Channel, Lang and Kbps as they were not in the XML that you posted
  12. What does mysqli_error($db) return after the select query? It could be you do have permissions to create temporary tables, although you should have with "root". run the query "SHOW PRIVILEGES" to see
  13. That query is failing because there is no man_number column in the temp table (as mysqli_error would have told you) Either change that query to use "key" or change "key" to "man_number" when you create the temp table
  14. Temporary tables only last as long as the mysql connection, nothing to do with the php session. Since a connection is closed at the end of the script then the temp table will disappear then also. This will create a temp table with your values $db = new mysqli(HOST,USERNAME,PASSWORD,DATABASE); $people=array(1234 => 1, 1235 => 4, 1237 => 3, 1239 => 5); $sql = "CREATE TEMPORARY TABLE temp ( `key` INT, `value` INT )"; $db->query($sql); $rec = array(); foreach ($people as $k=>$v) { $rec[] = "($k, $v)"; } $sql = "INSERT INTO temp VALUES " . join(',', $rec); $db->query($sql);
  15. Same table in all tests
  16. A while loop will execute as long as the condition in the ()s evaluates to true. So while(true) would execute forever without the test to execute the break. You could also achieve the same results with $count = 0; while ( $count < 10 ) { // start of while block $count++; # increment value by 1 echo "I've counted to: $count <br />"; } // end of while block where it gets to "I've counted to ten", goes to the start of the loop again, checks if count is still less than 10 it now is not true (count == 10), so it exits
  17. My 180,000 record table has no index on the "score" column so that could slow it
  18. If he already has more than 1 matching record then checking for ==1 will let him add an infinite number more duplicates
  19. On a table with 180,000+ rows both the dependent subquery and left join versions timed out. The version with the SQL variables took < 0.9 seconds.
  20. remove the single quotes from around the column name 'username'. Without the quotes it will be treated as a column name, with the quotes it is a string value
  21. The data +--------+-------+-----------+-------+ | idtest | name | subject | score | +--------+-------+-----------+-------+ | 1 | Peter | English | NULL | | 2 | Peter | Maths | 84 | | 3 | Peter | Chemistry | 66 | | 4 | Mary | English | 90 | | 5 | Mary | Maths | 80 | | 6 | Mary | Chemistry | 50 | | 7 | Paul | English | 60 | | 8 | Paul | Maths | 0 | | 9 | Paul | Chemistry | 100 | +--------+-------+-----------+-------+ 1. Find the total score for each student mysql> SELECT name, SUM(score) as total -> FROM testdata -> GROUP BY name; +-------+-------+ | name | total | +-------+-------+ | Mary | 220 | | Paul | 160 | | Peter | 150 | +-------+-------+ 2. Find the average score for each subject mysql> SELECT subject, SUM(score) as total, COUNT(*) as num, -> COUNT(score) as numscores, AVG(score) as average -> FROM testdata -> GROUP BY subject; +-----------+-------+-----+-----------+---------+ | subject | total | num | numscores | average | +-----------+-------+-----+-----------+---------+ | Chemistry | 216 | 3 | 3 | 72.0000 | | English | 150 | 3 | 2 | 75.0000 | <-- null was ignored | Maths | 164 | 3 | 3 | 54.6667 | +-----------+-------+-----+-----------+---------+ Note there was a NULL score for English which was ignored, (so the answer is NO they are not included in group aggregations) 3. Find the lowest non-zero score mysql> SELECT MIN(score) as lowest -> FROM testdata -> WHERE score > 0; +--------+ | lowest | +--------+ | 50 | +--------+
  22. You could if you want a highly inefficient solution. That solution uses a dependent subquery where every row has to run another query on the table.
  23. We enclose the output from each record in a <div>,,</div> which is defined as float:left. This means each div will be placed next to the previous one instead of underneath (default behaviour). To ensure that we start a new row every other record (when our counter $i has a remainder of zero when divided by 2) we output a <div> to clear the floating behaviour then start again on the next row of two
  24. The first part of your code should be $username = mysql_real_escape_string($_POST['username']); $points = (int)$_POST['points']; //This is 2 $actualpoints_query = "SELECT points FROM users WHERE 'username'='$username'"; $actualpoints_result = mysql_query($actualpoints_query); $row = mysql_fetch_assoc($actualpoints_result); // fetch the returned row $actualpoints = $row['points']; //This should be 1 $newpoints = $points + $actualpoints; //This should be 3 but it is 6! However that was unnecessary - all you need is an update query $username = mysql_real_escape_string($_POST['username']); // protect against SQL injection $points = (int)$_POST['points']; //This is 2 mysql_query("UPDATE users SET points = points + $points WHERE 'username'='$username' ");
  25. here's an example using floating divs $db = new mysqli(HOST, USERNAME, PASSWORD, DATABASE); $sql = "SELECT name, score FROM score"; $res = $db->query($sql); $i = 0; while ($row = $res->fetch_assoc()) { echo <<<TXT <div style='float:left; width:200px; height:50px; padding:10px'> {$row['name']} <br> <span style='font-size:20pt; font-weight: 600'>{$row['score']}</span> </div> TXT; if (++$i%2==0) echo "<div style='clear:both'></div>"; } Result attached
×
×
  • 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.