Jump to content

Barand

Moderators
  • Posts

    24,563
  • Joined

  • Last visited

  • Days Won

    822

Everything posted by Barand

  1. Imagine you had another table that contained product_code | latest_id for every product code. You could then join the product_section table to that matching on the code and the id to get the latest. Using a table subquery gives you that table. SELECT customer_favourites.cust_id , customer_favourites.prod_id , product.code , product.product_name , product.hidden , product_section.section_id , product_section.relative_order , product_section.catpage FROM customer _favourites INNER JOIN product ON customer_favourites.prod_id = product.id INNER JOIN product_section ON product_section.product_code = product.code INNER JOIN ( SELECT product_code , MAX(id) as id FROM product_section GROUP BY product_code ) as latest ON product_section.product_code = latest.product_code AND product_section.id = latest.id WHERE `cust_id` = '17' AND `hidden` = '0' ORDER BY `section_id` ASC, `relative_order` ASC, `catpage` ASC LIMIT 0,30
  2. Not much point in using any array functions on array $a as the only item referenced is $a[1] However I constructed an array with values [0..10] and used array_walk() with an anonymous function and benchmarked it against your code. Your current code was marginally faster so I'd stick with that. <?php function doThis(&$a,$b,$c) {return $a+$b+$c;} $a=array(10,20,30); $inc = $a[1] + 50; $t1 = microtime(1); for ($i=0; $i<10000; $i++) { $y = range(0,10); array_walk($y,function(&$v,$k,$b){$v += $b;},$inc); } $t2 = microtime(1); for ($i=0; $i<10000; $i++) { $z = []; for ($x=0; $x<=10; $x++) { $z[]=doThis($x,$a[1],50); } } $t3 = microtime(1); printf ("<pre>%-10s %0.4f\n%-10s %0.4f\n</pre>", 'anonymous',$t2-$t1, 'doThis()', $t3-$t2); ?> RESULT anonymous 0.1160 doThis() 0.1030
  3. Change these lines echo "</form>"; echo "<a href = 'fasttrackscw.php?id=".$ID."'>"; echo "<input type='submit' name='btn1'>"; echo "</a>"; to echo "<input type='hidden' name='id' value='$ID'>"; echo "<input type='submit' name='btn'>"; echo "</form>"; so the id and txtscore are both POSTed in the form data when the submit button ("btn" by the way) is clicked
  4. Your query has an error. Check what mysql_error() returns.
  5. Why do you loop through each field in each returned row? Lose the foreach() while ($rows = $query_result->fetch_assoc()) { foreach ($rows as $k => $v) { $v = $rows['user_id']; $u = $rows['listing_title']; $v = urlencode($v); // since this is a text name, make it url encoded $result_tb .= "<a href='searchResultslive.php?user_id=$v'>$u</a><br>"; } }
  6. Then you will have to select the name as well as the id in your query
  7. The content of the output produced by your code bears no relation to the content of what you claim is the input array. So I am not going to struggle with trying to work out what you are trying to do and how the data should be manipulated.
  8. Sorry but your code loop is far too advanced me. I have had no experience of code that processes an array and produces output from an alternate reality.
  9. The code to produce an image needs to be in a file of its own eg "myimg.php". Place on the page with an img tag, passing the player id <img src="myimg.php?id=123" /> Use $_GET['id'] in myimg.php to query the database for the player details.
  10. Why guess when you can look it up? http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html
  11. "where 1" is a boolean expression equivalent to "where 1 = true". Since 1 is always true then every record meets that criteria and is selected. Without a where clause every record is selected anyway so it is superfluous. It is used (by Dreamweaver, I think) to set up a dummy WHERE clause then append to it in the code with more conditions. eg $where = "WHERE 1 "; if (something) { $where .= "AND some_other_condition "; } if (somethingelse) { $where .= "AND yet_another_condition "; }
  12. With a "stock" table, only the id of the stock record would be duplicated. That's how relational databases work - the records are related by the keys and foreign keys. What is the highest priced stock? Which stock prices moved most in a particular time period? How would you query those with 7700 tables?
  13. If, over the years, no-one had ever redesigned the wheel we would all be driving round in cars with granite discs stuck on the end of the axles.
  14. Yes, but you put them inside parentheses. Look at the examples in manual for the ON DUPLICATE KEY UPDATE clauses - not a "(" or ")" to be seen
  15. You cannot put a table subquery in the order by clause, only single value expressions. From the MySQL manual Bad idea to use those single digit names anyway, even if they they are quoted. Is this what you want? SELECT COUNT( points ) AS rides , SUM( a.points ) AS pts , SUM(IF(a.points=3,1,0)) AS wins , SUM(IF(a.points=2,1,0)) AS p2 , SUM(IF(a.points=1,1,0)) AS p1 , SUM(IF(a.points=0,1,0)) AS p0 , rider_name , b.points as cardpoints FROM tbl_heat a JOIN tbl_heat b USING (heat, card_id, rider_name) WHERE a.card_id = 38 GROUP BY a.rider_name ORDER BY pts DESC, wins desc, p2 desc, p1 desc, p0 desc, cardpoints desc LIMIT 8
  16. That's 7700 tables isn't it. What you should have is -> CREATE TABLE `stock_price` ( -> `stock` VARCHAR(6) DEFAULT NULL, -> `date` date DEFAULT NULL, -> `open` decimal(16,4) DEFAULT NULL, -> `high` decimal(16,4) DEFAULT NULL, -> `low` decimal(16,4) DEFAULT NULL, -> `close` decimal(16,4) DEFAULT NULL, -> `volume` double(15,0) DEFAULT NULL, -> Primary key(stock, date) -> ) ENGINE=MyISAM DEFAULT CHARSET=latin1 where the added stock column contains the "AAPL" etc. One of the rules when constructing a db is "no derived data" so all those averages, percentages etc are not required. You calculate those in your queries when required. edit: if you have a separate "stock" table with details of the quoted stock (exchange, industry type etc) then the stock column above should be the id of the "stock" record.
  17. Parentheses!
  18. From that same page in the manual:
  19. You use "... ON DUPLICATE KEY UPDATE .." http://dev.mysql.com/doc/refman/5.6/en/insert-on-duplicate.html
  20. And... why not :
  21. No, no, no. Don't store delimited lists (especially lists of keys) in a database table. The table should be mid INT, qid INT, PRIMARY KEY (mid,qid) You then use LEFT join between the two table to find those not answered. SELECT question FROM quiz q LEFT JOIN othertable o ON q.qid = o.qid AND o.mid = ? WHERE o.qid IS NULL
  22. You do not need to connect twice to the server, it wastes time.Most of the time used in the execution of the script will be the database connect time. You can access different databases by prefixing the the table name with the database name (even in the same query) eg login.users and skola.upis. You could also accomplish your task with a single insert query INSERT INTO skola.upis(`col1`, `col2`, `col3`, `col4`) SELECT prskola, '$value2', '$value3', '$value4' FROM login.users WHERE user_name = '$rrr' However, rather than inserting raw user-provided data directly into a query you should use a prepared query. So you would have $conn = new mysqli("localhost", "root", "111", "login"); if ($conn->connect_error) { die('Connect Error (' . $conn->connect_errno . ') ' . $conn->connect_error); } $rrr = $_POST['user']; $value2 = $_POST['data1']; $value3 = $_POST['data2']; $value4 = $_POST['data3']; $sql = "INSERT INTO skola.upis(`col1`, `col2`, `col3`, `col4`) SELECT prskola, ?, ?, ? FROM login.users WHERE user_name = ?"; $stmt = $conn->prepare($sql); $stmt->bind_param('ssss', $value2, $value3, $value4, $rrr); $stmt->execute(); if ($stmt->affected_rows==0) { echo "Could not get data"; }
  23. The query would look like this. $sql = "SELECT s.sector_id s.sector_name c.person_id FROM sector s LEFT JOIN choice c ON s.sector_id = c.sector_id AND c.person_id = $person"; Where person_id has a value (ie not null) then that person has chosen the sector
  24. I assume that the new table is an HTML table that you want to display because you certainly shouldn't have a SQL table in that format <?php include('db_inc.php'); $db = new mysqli(HOST,USERNAME,PASSWORD,'test'); $sql = "SELECT make , model FROM car ORDER BY make, model"; $res = $db->query($sql); $data = array(); while (list($make,$model) = $res->fetch_row()) { $data[$make][] = $model; } ?> <table border='1' cellpadding='3'> <tr> <th> <?= join('</th><th>', array_keys($data)) ?> </th> </tr> <tr valign='top'> <?php foreach ($data as $models) { echo '<td>' . join('<br>', $models) . '</td>'; } ?> </tr> </table> Results would look like
×
×
  • 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.