Jump to content

Barand

Moderators
  • Posts

    24,563
  • Joined

  • Last visited

  • Days Won

    822

Everything posted by Barand

  1. fetchAll() is a PDO method. It won't work if you're using mysqli objects. An alternative without it would be $res = $db->query("SELECT c1.cat_name , count(i.id) as total FROM ".DB_PREFIX."channels c1 JOIN ".DB_PREFIX."channels c2 ON c2.child_of = c1.cat_id JOIN ".DB_PREFIX."images i ON i.category = c2.cat_id GROUP BY c1.cat_name UNION SELECT c1.cat_name , count(i.id) as total FROM ".DB_PREFIX."channels c1 JOIN ".DB_PREFIX."images i ON i.category = c1.cat_id WHERE c1.child_of = 0 GROUP BY c1.cat_name "); echo "<ul>\n"; foreach ($res as $row) { echo "<li>{$row['cat_name']} ({$row['total']})</li>\n"; } echo "</ul>\n"; ?>
  2. PDO is more streamlined mysqli has result objects and statement objects depending on whether you used query() or prepare(). The methods for handling the results of these two objects are completely different. This mean two confusing sets of methods to learn. On the other hand, PDO produces objects with exactly the same methods to process the results of query() or prepare(), and usage of prepare() is itself much simpler. Some mysqli methods are only available depending on your implementation of MySql. PDO works with multiple varieties of DBMS (mysql, postgres etc). The SQL dialects may be different but your php code remains the same if you change. Having used the old mysql_* for years I originally changed to mysqli (how different could it be?) After a long while I decided I had to learn PDO in order to support others who were using it. I really wished I'd switched to it originally instead of mysqli.
  3. Yes, you told us before that it won't centre. Stop repeating the same ambiguous terms and tell us what you want - A or B, or something else?
  4. Ah! The delights of mysqli. https://dev.mysql.com/doc/refman/8.0/en/commands-out-of-sync.html Do yourself favour and change to PDO.
  5. $pdo in my code is a PDO object connecting to the database server. Example from PHP manual /* Connect to a MySQL database using driver invocation */ $dsn = 'mysql:dbname=testdb;host=127.0.0.1'; $user = 'dbuser'; $password = 'dbpass'; $pdo = new PDO($dsn, $user, $password);
  6. Define Do you mean the content of the card is not centrally aligned, or the card itself is not in the centre of an area?
  7. I'd do something like this (although my output is simplified as I have no idea what your methods are producing)... $res = $pdo->query("SELECT c1.cat_name , count(i.id) as total FROM prefix_channels c1 JOIN prefix_channels c2 ON c2.child_of = c1.cat_id JOIN prefix_images i ON i.category = c2.cat_id GROUP BY c1.cat_name UNION SELECT c1.cat_name , count(i.id) as total FROM prefix_channels c1 JOIN prefix_images i ON i.category = c1.cat_id WHERE c1.child_of = 0 GROUP BY c1.cat_name "); $result = $res->fetchAll(PDO::FETCH_OBJ); echo "<ul>\n"; foreach ($result as $row) { echo "<li>$row->cat_name ($row->total)</li>\n"; } echo "</ul>\n"; giving
  8. You can simplify things by always ensuring categories have at least one subcategory, and always linking images to the subcategories then you have so that mysql> SELECT c1.cat_name -> , count(i.id) as total -> FROM prefix_channels c1 -> JOIN prefix_channels c2 ON c2.child_of = c1.cat_id -> JOIN prefix_images i ON i.category = c2.cat_id -> GROUP BY c1.cat_name; +-------------------+-------+ | cat_name | total | +-------------------+-------+ | parent_category_1 | 4 | | parent_category_2 | 2 | | parent_category_3 | 1 | +-------------------+-------+
  9. Run my query with your class then output as now.
  10. Indirect owners of images UNION Direct owners of images eg SELECT c1.cat_name , count(i.id) as total FROM prefix_channels c1 JOIN prefix_channels c2 ON c2.child_of = c1.cat_id JOIN prefix_images i ON i.category = c2.cat_id GROUP BY c1.cat_name UNION SELECT c1.cat_name , count(i.id) as total FROM prefix_channels c1 JOIN prefix_images i ON i.category = c1.cat_id WHERE c1.child_of = 0 GROUP BY c1.cat_name; +-------------------+-------+ | cat_name | total | +-------------------+-------+ | parent_category_1 | 4 | | parent_category_2 | 2 | | parent_category_3 | 1 | +-------------------+-------+
  11. According to my IDE and php.net...
  12. Have you thought about subtracting the smaller number from the larger one?
  13. It's you Scots who insist that the correct spelling of whisky is without the "e"
  14. I'd have expected a true Scotch whisky drinker to be able to spell it correctly. And please read the forum rules, #5 and #7 in particular.
  15. Why weren't they numeric to start with?
  16. You seem to missing the bit where $answer is calculated. There should be an error message telling you that $anwer is not defined.
  17. mysqli_query uses an SQL query (string) for its second argument. You are passing $stmt which is a prepared statement object. There is nothing in that query that needs preparing so just use mysqli_query with the query string.
  18. The "where 1" line is totally unnecessary (and looks like something produced by Dreamweaver). Ignore it. The GROUP BY line should come before the ORDER BY line.
  19. You don't store totals of their donations in the first place. All you need are the individual donation amounts and sum them when you need the total.
  20. It's a waste time trying update records with derived data that can be easily got with a query when required. Don't store derived data.
  21. $stmt = $pdo->prepare("SELECT <whatever columns you need> FROM business WHERE city = ? OR zipcode = ? "); $stmt->execute([ $search, $search ]); $results = $stmt->fetchAll(); foreach ($results as $row) { // output result row }
  22. Use backticks and not single quotes for identifiers, or omit them - they are only necessary if the identifier contains special charaters or is a reserved word.. Quotes are for string literals.
  23. https://www.php.net/manual/en/tutorial.forms.php
  24. The global bit of code was working, but you were trying to use the ones that weren't defined. You needed to make the rest of the code dependent on the form being 'form1' or not.
  25. Per haps something like this... <?php $a = 123; $x = "hello"; $z = "there"; $formSelector = 'form2'; function choose($val1, $val2='') { //Test for result echo $val2; if ($val1 == 123){ echo "bye bye"; } echo "test result"; } //Call function if ($formSelector == 'form1') { choose($a); } else { choose($x, $z); } ?>
×
×
  • 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.