-
Posts
24,338 -
Joined
-
Last visited
-
Days Won
795
Posts posted by Barand
-
-
One query, get all users and store in an indexed array. Not separate numbered variables.
-
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 }
-
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.
-
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
-
I put registration in both as I thought this may be used to help create a relationship between the 2
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.
-
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 many are returned if any is irrelevant.
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.
-
\r\n ErrStr: mysqli_select_db() expects exactly 2 parameters, 1 given,
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
-
-
Benanamen - I am looking for a way to identify those who have already signed up, not preventing future ones. But thank you for the reply.
Benanamen's point was that you need to stop this happening future, so you don't have to keep repeating this exercise.
-
Should I have the session start on this script?
You need session_start() at beginning of every script that needs to reference $_SESSION.
-
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);
- 1
-
Are you sure that is the same form - the hidden fields you originally posted are text fields in that form?
-
-
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?
Any ideas why code is inserting just one character of each value?
in PHP Coding Help
Posted
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.