Jump to content

Barand

Moderators
  • Posts

    24,563
  • Joined

  • Last visited

  • Days Won

    822

Everything posted by Barand

  1. My main reason is that PDO is consistent. In mysqll, when you use the query() method it produces a result object; when you use prepare() it produces a statement object. It looks like mysqli was developed by two teams who never spoke to ane another as the methods within these two objects are completely different, so you have to process results differently. In PDO you always get a statement object so processing is always the same. The PDO methods are cleaner to use for prepared statements. PDO can be used with many different RDBMS, so if you want to change, the php remains constant (although you may have to adjust the SQL dialect in your queries). Did you read the link I gave you?
  2. Welcome. You'll be wanting to look at prepared statements then. Take the PDO route and not mysqli - it's much better.
  3. Provide a dump of the table structure and test data.
  4. Your browser should not be showing any php code if it is executed correctly. Are you just opening the file in the browser (like a text file)? You need to open it as a url, eg http://localhost/myfile.php
  5. You're best bet is to turn on display_errors in your php.ini file and also set the error reporting level to E_ALL. See what error messages you get.
  6. You need to define the required precision $totalgoal2 = number_format(min($num_rows['count'], 49)/49*100, 2); ^
  7. Specify the number of dec places in your call to number_format()
  8. That expression returns whichever is the smaller number - the count or 49. So if the count is, say, 40, then it returns 40. If it is anything over 49 it returns 49 (100%) as that is the smaller number.
  9. $totalgoal2 = number_format(min($num_rows['count'], 49)/49*100);
  10. Given that the query isn't syntactically correct and couldn't run, there is no way I'm attempting a solution without knowing the data and the structure of the data. So how about giving us some needed context.
  11. Output the results into an HTML table Put each result row in a new table <tr> row. Put each column into a table <td> cell. EG <?php // CONNECT TO DB HERE $sql = "SELECT animal_type , animal_breed , colour , owner_name , address , telephone , mobile , email , offence , offence_date , offence_location , case_status , case_ref , action_required , action_taken , microchipped , microchip_number , aggressive , dangerous , lost , date_lost , location_lost , stolen , date_stolen , location_stolen , found, date_found , location_found , other_information FROM `animals` -- WHERE 1 (utterly useless, just leave it out) "; //------------------------------------------------------------------------------ // I find this function useful for testing // queries when I haven't got MySql Workbench open function query2HTML($db, $sql) { $output = "<table border='1'>\n"; // Query the database $result = $db->query($sql); if ($result->num_rows == 0) return "No matching records"; // get the first row and display headings $row = $result->fetch_assoc(); $output .= "<tr><th>" . join('</th><th>', array_keys($row)) . "</th></tr>\n"; // display the data do { $output .= "<tr><td>" . join('</td><td>', $row) . "</td></tr>\n"; } while ($row = $result->fetch_assoc()); $output .= "</table>\n"; return $output; } ?> <!DOCTYPE html> <html lang='en'> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="generator" content="PhpED 19.5 (Build 19523, 64bit)"> <title>Example</title> <meta name="author" content="Barand"> <meta name="creation-date" content="04/09/2022"> <style type='text/css'> table { border-collapse: collapse; margin: 20px auto; width: 95%; } td, th { padding: 4px 8px; } th { background-color: #ccc; } </style> </head> <body> <?= query2html($conn, $sql) ?> </body> </html>
  12. He has. A query selects all records for which the WHERE expression evaluates to "true". As "1" is true for all records, they have specified that all records are selected.
  13. Example data TABLE: client TABLE: project +----+-----------+----------+ +----+---------------+-----------+------------+ | id | firstname | lastname | | id | project_name | client_id | start_date | +----+-----------+----------+ +----+---------------+-----------+------------+ | 1 | Scott | Chegg | | 1 | Project Alpha | 4 | 2022-12-01 | | 2 | Laura | Norder | | 2 | Proect Beta | 2 | 2023-01-15 | | 3 | Tom | DiCanari | | 3 | Project Gamma | 4 | 2023-03-01 | | 4 | S | Tonin | | 4 | Project Delta | 1 | 2023-03-20 | +----+-----------+----------+ +----+---------------+-----------+------------+ Query SELECT project_name , start_date , CONCAT(c.firstname, ' ', c.lastname) as client FROM project p JOIN client c ON p.client_id = c.id ORDER BY client, start_date; +---------------+------------+--------------+ | project_name | start_date | client | +---------------+------------+--------------+ | Proect Beta | 2023-01-15 | Laura Norder | | Project Alpha | 2022-12-01 | S Tonin | | Project Gamma | 2023-03-01 | S Tonin | | Project Delta | 2023-03-20 | Scott Chegg | +---------------+------------+--------------+ Now change the first name of client 4 and re-query UPDATE client SET firstname = 'Sarah' WHERE id = 4; SELECT project_name , start_date , CONCAT(c.firstname, ' ', c.lastname) as client FROM project p JOIN client c ON p.client_id = c.id ORDER BY client, start_date; +---------------+------------+--------------+ | project_name | start_date | client | +---------------+------------+--------------+ | Proect Beta | 2023-01-15 | Laura Norder | | Project Alpha | 2022-12-01 | Sarah Tonin | | Project Gamma | 2023-03-01 | Sarah Tonin | | Project Delta | 2023-03-20 | Scott Chegg | +---------------+------------+--------------+
  14. Data like names should be stored in one place only in a database. In this case, the only place the client name should be stored is in the "client" table. Other tables related to the client, such as "project" should contain the id of the id of the related client, When you query the project table you then JOIN to the client table using the client id. This way, a change in one place is all that is required. Databases should be correctly designed using a process of normalization - Google it.
  15. On your "previous page" you are populating $_SESSION['nik'] from $_POST['nik']. Are you sure 'nik' value was posted to that page? If not , $_SESSION['nik'] will be empty. Check the values in your $_SESSION. echo '<pre>' . print_r($_SESSION, true) . '</pre>';
  16. <?php require_once('kaynak/baglan.php'); session_start(); ?> <!DOCTYPE html> <html lang="tr"> <head> <title>Sayfanız - Hoşgeldiniz</title> <meta charset="utf-8"> <link rel="stylesheet" type="text/css" href="kaynak/style.css"> </head> <body> <div id="ana"> <h1><?php echo $_SESSION['nik']; ?> Üye giriş sayfasına hoşgeldiniz</h1><br> </div> </body> </html>
  17. echo $_SESSION['nik']; ... does it. Forget about the POSTed values - there aren't any being sent.
  18. If you put it into $_SESSION['nik'] in the previous page, it will still be there. That previous page isn;t posting any data.
  19. It apparently hasn't reached this page or you woudn't be getting the error. How are you sending it?
  20. Has data been posted to that page at the time of the error? Check for posted data before trying to process it if ($_SERVER['REQUEST_METHOD']=='POST') { // here you can process posted data }
  21. It looks like it expects that code to be in "index.php". The posted data will be sent to that file (itself) If you remove action="index.php" you can call your php file anything and, without the action, it will call itself.
  22. Remember that only check checkboxes are posted, so the kth checkbox isn't necessarily the kth other items I'd use the id as index for the post array, for example... <input type='text' name='prod_qty[$prod_id]' value='<?= $prod_qty ?>' hidden>
  23. Probably works because mysql is more relaxed than the standard dictates
  24. Your query failed, so $result contains false and not a result object. Put this code before your call to nysql_connect... mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT); ... it won't stop it failing but it will tell you why.
  25. You group by whatever you want to aggregate by. If you want total monthly sales, say, then you'd have something like SELECT YEAR(sales_date) as year , MONTH(sales_date) as month , SUM(sales_amount) as total FROM sales GROUP BY year, month If you wanted customer totals SELECT customer_id , SUM(sales_amount) as total FROM sales GROUP BY customer_id Neither customer_id nor sales_date is the primary key of the sales table.
×
×
  • 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.