-
Posts
24,566 -
Joined
-
Last visited
-
Days Won
822
Everything posted by Barand
-
Is there a more efficient method to compress these queries?
Barand replied to imgrooot's topic in PHP Coding Help
see https://forums.phpfreaks.com/topic/303305-pdo-looping/?p=1543471&do=findComment&comment=1543471 -
Any ideas why code is inserting just one character of each value?
Barand replied to sonnieboy's topic in PHP Coding Help
Then why are they output within a foreach loop? -
The times when you might want similar to that is when you want to specify the array indexes For example if ($stmt->execute()) { while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { $products[ $row['user_id'] ] = $row; } } or to group results if ($stmt->execute()) { while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { $products[ $row['cat_id'] ][] = $row; } }
-
Any ideas why code is inserting just one character of each value?
Barand replied to sonnieboy's topic in PHP Coding Help
If, say, $_POST['sourcename'] is a string and not an array then $_POST['sourcename'][$i] with be the $ith character of the string and not the $ith element of an array. -
Is there a more efficient method to compress these queries?
Barand replied to imgrooot's topic in PHP Coding Help
One query, get all users and store in an indexed array. Not separate numbered variables. -
Public Function to retrieve data from 2 tables pdo
Barand replied to ScoobyDont's topic in PHP Coding Help
In what way is that "outside the function"? $db = DB(); // connection OUTSIDE the function $vehicle_data = UserDetails($db, $user_id); // pass the $db connection as a parameter plus you need to redefine the function to accept the extra parameter function UserDetails ($db, $user_id) { // function code } -
Public Function to retrieve data from 2 tables pdo
Barand replied to ScoobyDont's topic in PHP Coding Help
It was better as it was. Just add a WHERE clause to tell it which user. $query = $db->prepare("SELECT users.user_id , users.registration , users.email , vehicle.reg_id , vehicle.vehicle , vehicle.registration , vehicle.chassis FROM users JOIN vehicle ON vehicle.registration = users.registration WHERE users.user_id = :user_id"); $query->bindParam("user_id", $user_id, PDO::PARAM_STR); It looks like the line $db = DB(); is connecting to your database. Move that code out of the function and pass $db as a parameter to your function with $user_id. Connecting is a relatively slow process and you don't want to do every for every query. -
Public Function to retrieve data from 2 tables pdo
Barand replied to ScoobyDont's topic in PHP Coding Help
Add those columns' names to the list of columns in your SELECT clause. EDIT: You now have a bound parameter but there is nowhere to bind the parameter to. You need a WHERE clause in the query -
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.