mysql

MySQL pivot row into dynamic number of columns

痴心易碎 提交于 2021-02-17 06:34:07
问题 Lets say I have three different MySQL tables: Table products : id | name 1 Product A 2 Product B Table partners : id | name 1 Partner A 2 Partner B Table sales : partners_id | products_id 1 2 2 5 1 5 1 3 1 4 1 5 2 2 2 4 2 3 1 1 I would like to get a table with partners in the rows and products as columns. So far I was able to get an output like this: name | name | COUNT( * ) Partner A Product A 1 Partner A Product B 1 Partner A Product C 1 Partner A Product D 1 Partner A Product E 2 Partner B

Populating Php Dropdown list from mysql database

假如想象 提交于 2021-02-17 06:10:17
问题 am trying to populate a dropdown list from mysql database table here is the code <div class="form-group"> Select Make <?php include '../db_config/dbcon.php'; $sql = "SELECT * FROM vehicle_details"; $result = mysql_query($sql); echo "<select name='vehicle_make'>"; while ($row = mysql_fetch_array($result)) { echo "<option value='" . $row['vehicle_make'] . "'>" . $row['vehicle_make'] . "</option>"; } echo "</select>"; ?> </div> This is what is displayed by the code Where am i going wrong?? 回答1:

Populating Php Dropdown list from mysql database

冷暖自知 提交于 2021-02-17 06:08:39
问题 am trying to populate a dropdown list from mysql database table here is the code <div class="form-group"> Select Make <?php include '../db_config/dbcon.php'; $sql = "SELECT * FROM vehicle_details"; $result = mysql_query($sql); echo "<select name='vehicle_make'>"; while ($row = mysql_fetch_array($result)) { echo "<option value='" . $row['vehicle_make'] . "'>" . $row['vehicle_make'] . "</option>"; } echo "</select>"; ?> </div> This is what is displayed by the code Where am i going wrong?? 回答1:

PDO and binding multiple value sets during insert - recently

丶灬走出姿态 提交于 2021-02-17 06:07:27
问题 Using PDO in PHP, when having to insert multiple rows into a table at once, I've used sql that looks something like this: INSERT INTO some_names (firstName, lastName) VALUES ('Joe', 'Smith'),('Fred','Sampson'),('Lisa','Pearce'); As you can see I'm inserting three rows with one statement. The reason I do this is that I believe it is more efficient than executing three distinct statements to insert the rows. So my question is this: how do I do this in PHP if I want to be able to bind my values

PDO and binding multiple value sets during insert - recently

我是研究僧i 提交于 2021-02-17 06:05:44
问题 Using PDO in PHP, when having to insert multiple rows into a table at once, I've used sql that looks something like this: INSERT INTO some_names (firstName, lastName) VALUES ('Joe', 'Smith'),('Fred','Sampson'),('Lisa','Pearce'); As you can see I'm inserting three rows with one statement. The reason I do this is that I believe it is more efficient than executing three distinct statements to insert the rows. So my question is this: how do I do this in PHP if I want to be able to bind my values

SQL ERROR in Syntax when using WITH [duplicate]

大城市里の小女人 提交于 2021-02-17 05:56:20
问题 This question already has answers here : How do you use the “WITH” clause in MySQL? (7 answers) Closed 4 years ago . I want to use a SQL Query with the WITH clause an I get a Syntax Error. I´m using MySQL Version 5.6.28 Here a simple Code example WITH alias_test AS (SELECT id, title FROM `tips_locations`) SELECT id, title FROM alias_test Here the error I get in my SQL Tool You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax

php submit only giving last value from array that populates form

拟墨画扇 提交于 2021-02-17 05:54:45
问题 Struggling with a one page form that i want to first populate a form from a mysql query, then enable a user to update some of the values in each of several table rows from text input fields in the form. The code's intent is to update the field by referencing the row ID. But for some reason I'm only able to update the last row in the form (the last row from the array). I've included some troubleshooting code to see what the ID variable is and it always comes up as the last iteration of the

how to get sum of a column with codeigniter query [duplicate]

馋奶兔 提交于 2021-02-17 05:54:08
问题 This question already has an answer here : codeigniter - how to add up values in table column (1 answer) Closed 1 year ago . I am trying to get sum of a column values in mysql table with the codeigniter query as $this->db ->query("Select SUM(tb1.amount) from table1 tb1 inner join table2 tb2 on tb2.Id=tb1.Id Where tb2.Status='1'") ->result() it give me error as array to string conversion I need just a number such count(amount) returns number of rows with num_row() 回答1: use as => $data['total']

mysqli_insert_id is not working on php OOP method

怎甘沉沦 提交于 2021-02-17 05:53:16
问题 I am testing the function of getting last auto_increment_ID by using mysqli_insert_id . I feel quite confuse when I find out the if i use 2 different methods, the results are different. Method 1 <?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; // Create connection $conn = mysqli_connect($servername, $username, $password, $dbname); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } $sql = "INSERT into item

MySQL on duplicate key… get existing ID?

泪湿孤枕 提交于 2021-02-17 05:44:07
问题 I'm using ON DUPLICATE KEY UPDATE to handle duplicate inserts on a table, in order that they are discarded. In my case it's a simple table storing tags: id (int, PK, AI, unsigned, not null) tag (varchar 25, not null, unique) This is working fine, but I need to retrieve the ID - either the insert ID, on successful insert, or the existing ID, if it's a duplicate. I'm getting insert ID = 0 where ON DUPLICATE KEY UPDATE fires, which I guess is expected behaviour since no insert took place. Is