-
Posts
24,608 -
Joined
-
Last visited
-
Days Won
831
Everything posted by Barand
-
Public Function to retrieve data from 2 tables pdo
Barand replied to ScoobyDont's topic in PHP Coding Help
Then that is what you should use for the join with your current tables. SELECT .... FROM user u JOIN vehicle v ON u.registration = v.registration However, a more flexible model would be ... +-------------+ | user | +-------------+ | user_id |--------+ | email | | +--------------+ | etc | | | vehicle | +-------------+ | +--------------+ | | vehicle_id | +--------<| user_id | | registration | | vehicle | | chassis_no | +--------------+ .. which easily allows you to add a second (and third...) car for a user just by adding another row with the user's user_id. In this model the join would be on the user_id columns. -
Public Function to retrieve data from 2 tables pdo
Barand replied to ScoobyDont's topic in PHP Coding Help
Your column names are ambiguous and confusing. Can you show us your table structures and some sample data? -
$data = []; is equivalent to $data = array();
-
A sort function should return -ve, zero or +ve values depending on the comparison result. Yours will return a boolean result. This would achieve that return filemtime($x) - filemtime($y);
-
You may find it better to store the date and contents in the array (with filename as the key). In which case a simple asort() will not work, you will need a custom sort function. For example $files['file1.txt'] = [ 'date' => '2017-01-03', 'content' => 'aaa' ]; $files['file2.txt'] = [ 'date' => '2017-01-01', 'content' => 'bbb' ]; $files['file3.txt'] = [ 'date' => '2017-01-02', 'content' => 'ccc' ]; $files['file4.txt'] = [ 'date' => '2017-01-04', 'content' => 'ddd' ]; uasort($files, function ($a, $b) { return strcmp ($a['date'], $b['date']); }); foreach ($files as $filename => $fdata) { // process the output }
-
How can you say that when you have no knowledge of the application? Let's say I want to list names in three columns and want the numbers in each column to be as near equal as possible. For example I want A E I B F J C G K D H and not A F K B G C H D I E J I would be interested in your method of doing this without knowing how many there are.
-
Migrated working app to locally hosted and have mysql_connect errors
Barand replied to allinarush's topic in PHP Coding Help
There is a clue in that error message - wrong parameters. So look up the function mysqli_select_db in the PHP manual and see what the parameters should be -
http://uk1.php.net/manual/en/class.pdo.php
-
Benanamen's point was that you need to stop this happening future, so you don't have to keep repeating this exercise.
-
You need session_start() at beginning of every script that needs to reference $_SESSION.
-
alter table add column ... like this other column over here?
Barand replied to fatkatie's topic in MySQL Help
You can create a table which has exactly the same structure as another CREATE TABLE table2 LIKE table1; I know of nothing similar down at the ADD COLUMN level. -
Sorry, careless with my column aliases Try SELECT DATE(payments.date) as dateportion , users.username , COUNT(*) as tot from payments INNER JOIN users ON payments.user_id = users.id WHERE payments.membership_id IN (6,7) GROUP BY payments.user_id, dateportion HAVING tot > 1
-
Work with just the date parts of the fields using DATE(payments.date) SELECT DATE(date) as date , userid , COUNT(*) as tot FROM payments GROUP BY user_id, date HAVING tot > 1
-
try $results = array( array("SALE_ID"=>"1","SALE_DATE"=>"2017-01-01"), array("SALE_ID"=>"2","SALE_DATE"=>"2017-02-15"), array("SALE_ID"=>"3","SALE_DATE"=>"2017-03-25"), array("SALE_ID"=>"4","SALE_DATE"=>"2017-04-10"), array("SALE_ID"=>"5","SALE_DATE"=>"2017-01-08"), array("SALE_ID"=>"6","SALE_DATE"=>"2017-02-23"), array("SALE_ID"=>"7","SALE_DATE"=>"2017-03-15"), array("SALE_ID"=>"8","SALE_DATE"=>"2017-04-09") ); $results_by_week = []; foreach ($results as $sale) { $w = week_number($sale['SALE_DATE']); $results_by_week[$w][] = (object)$sale; } ksort($results_by_week);
-
Are you sure that is the same form - the hidden fields you originally posted are text fields in that form?
-
edit row if hashtag field already exists with the varible
Barand replied to dropfaith's topic in PHP Coding Help
reverse the array first (array_reverse()) -
edit row if hashtag field already exists with the varible
Barand replied to dropfaith's topic in PHP Coding Help
try removing the (..)s from around the insert statement. -
You only show part of the html for the form. What is the whole form code?
-
Have you tried doing a var_dump($_POST) to check that the custname isn't null?
-
Your query has five columns with five parameters - so why nine placeholders for those parameters?
-
edit row if hashtag field already exists with the varible
Barand replied to dropfaith's topic in PHP Coding Help
Put unique key on hashtag column. CREATE TABLE `hashtag` ( `hashtag_id` int(11) NOT NULL AUTO_INCREMENT, `hashtag` varchar(45) DEFAULT NULL, `link` varchar(45) DEFAULT NULL, PRIMARY KEY (`hashtag_id`), UNIQUE KEY `idx_hashtag_hashtag` (`hashtag`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; mysql> insert into hashtag (hashtag, link) VALUES -> ('abc', 'xyz'), -> ('bcd', 'stu'); Query OK, 2 rows affected (0.05 sec) mysql> select * from hashtag; +------------+---------+------+ | hashtag_id | hashtag | link | +------------+---------+------+ | 1 | abc | xyz | | 2 | bcd | stu | +------------+---------+------+ 2 rows in set (0.00 sec) Instead of querying to see if it exists then doing a second query to update, do it in one query using "INSERT .. ON DUPLICATE KEY" mysql> INSERT INTO hashtag (hashtag, link) VALUES ('abc', 'def') -> ON DUPLICATE KEY UPDATE link = values(link); Query OK, 2 rows affected (0.06 sec) mysql> select * from hashtag; +------------+---------+------+ | hashtag_id | hashtag | link | +------------+---------+------+ | 1 | abc | def | | 2 | bcd | stu | +------------+---------+------+ 2 rows in set (0.00 sec) -
Categories and subcategories array and dynamic dropdown list
Barand replied to z4z07's topic in PHP Coding Help
When the first category menu changes, send an AJAX request with the selected category id. On the server, on receipt of the request, retrieve from the database those categories whose parent is the received id. Send back the options required to populate the second category menu. Repeat the process with the second menu to retrieve the options for the third. -
You need a WHERE clause in the cart update query so you can specify the correct cart and product within that cart UPDATE ...SET ... WHERE ...
-
Using your current method (but with arrays and PDO) it becomes $databases = [ 'database1', 'database2', 'database3', 'database4', 'database5' ]; $baseSQL = "SELECT FirstName, LastName, Email, Mobile, PromoCode FROM <DB>.tablename WHERE (Email LIKE ?) OR (Mobile LIKE ?) OR (PromoCode LIKE ?) "; $queries = []; $params = []; $srch = isset($_GET['search']) ? '%' . $_GET['search'] . '%' : '%'; foreach ($databases as $dbname) { $queries[] = str_replace('<DB>', $dbname, $baseSQL ); array_push($params, $srch, $srch, $srch); } $sql = join("\nUNION\n", $queries); // build set of UNION queries /********** DEBUG ONLY _ DISPLAY QUERY **************/ echo '<pre>', $sql, '</pre>'; /****************************************************/ $stmt = $db->prepare($sql); $stmt->execute($params);
-
Why are you messing around with string concatenation and then worrying about removing trailing commas or UNION keywords. Use arrays with join(). You will also find it a lot simpler with PDO. Its parameter binding is far more streamlined.