-
Posts
24,566 -
Joined
-
Last visited
-
Days Won
822
Everything posted by Barand
-
Go Ahead. If it is a different problem then you should start a new topic.
-
I had just received the notification from PP. Thank you.
-
There are links to PP in all my posts if you insist.
-
should be $res = $conn->query("SELECT userid, fname, lname, sign_x, sign_y FROM winuser_demo");
-
When you use mysqli, call mysqli_report() before you connect. You will then be notified automatically of any errors encountered in your mysqli code. Your connection is "$conn" so you need to use that and not "$db" in the query call. mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT); $conn = mysqli_connect('localhost', '???', '???', 'pmapp'); $conn->query("SELECT ....
-
You are definitely on the right track. TABLE: plan +-----------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------+-------------+------+-----+---------+-------+ | plan_id | int(11) | NO | PRI | | | | plan_name | varchar(45) | YES | | | | +-----------+-------------+------+-----+---------+-------+ TABLE: plan_version +-------------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------------+---------+------+-----+---------+-------+ | plan_id | int(11) | NO | PRI | | | | version_no | int(11) | NO | PRI | | | | valid_from | date | YES | | | | | valid_until | date | YES | | | | +-------------+---------+------+-----+---------+-------+ TABLE: entitlement +------------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +------------+---------+------+-----+---------+-------+ | plan_id | int(11) | NO | PRI | | | | version_no | int(11) | NO | PRI | | | | feature_id | int(11) | NO | PRI | | | +------------+---------+------+-----+---------+-------+ TABLE: feature +------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +------------+--------------+------+-----+---------+-------+ | feature_id | int(11) | NO | PRI | | | | title | varchar(45) | YES | | | | | page | varchar(100) | YES | | | | +------------+--------------+------+-----+---------+-------+ Suppose the data is... TABLE: plan TABLE: entitlement +---------+-----------+ +---------+------------+------------+ | plan_id | plan_name | | plan_id | version_no | feature_id | +---------+-----------+ +---------+------------+------------+ | 1 | Silver | | 1 | 1 | 1 | | 2 | Gold | | 1 | 1 | 2 | | 3 | Platinum | | 1 | 1 | 3 | +---------+-----------+ | 1 | 2 | 1 | | 1 | 2 | 2 | TABLE: plan_version | 1 | 2 | 3 | +---------+------------+------------+-------------+ | 1 | 2 | 4 | | plan_id | version_no | valid_from | valid_until | | 2 | 1 | 1 | +---------+------------+------------+-------------+ | 2 | 1 | 2 | | 1 | 1 | 2019-06-01 | 2019-12-31 | | 2 | 1 | 3 | | 1 | 2 | 2020-01-01 | 9999-12-31 | | 2 | 1 | 4 | | 2 | 1 | 2019-06-01 | 2019-10-31 | | 2 | 1 | 5 | | 2 | 2 | 2019-11-01 | 2020-02-29 | | 2 | 1 | 6 | | 2 | 3 | 2020-03-01 | 9999-12-31 | | 2 | 2 | 1 | | 3 | 1 | 2019-12-01 | 9999-12-31 | | 2 | 2 | 2 | +---------+------------+------------+-------------+ | 2 | 2 | 3 | | 2 | 2 | 4 | TABLE: feature | 2 | 2 | 5 | +------------+-------+------------+ | 2 | 2 | 6 | | feature_id | title | page | | 2 | 2 | 7 | +------------+-------+------------+ | 2 | 3 | 1 | | 1 | AAA | Page1.php | | 2 | 3 | 2 | | 2 | BBB | Page2.php | | 2 | 3 | 3 | | 3 | CCC | Page3.php | | 2 | 3 | 4 | | 4 | DDD | Page4.php | | 2 | 3 | 5 | | 5 | EEE | Page5.php | | 2 | 3 | 6 | | 6 | FFF | Page6.php | | 2 | 3 | 7 | | 7 | GGG | Page7.php | | 2 | 3 | 8 | | 8 | HHH | Page8.php | | 3 | 1 | 1 | | 9 | JJJ | Page9.php | | 3 | 1 | 2 | | 10 | KKK | Page10.php | | 3 | 1 | 3 | +------------+-------+------------+ | 3 | 1 | 4 | | 3 | 1 | 5 | | 3 | 1 | 6 | | 3 | 1 | 7 | | 3 | 1 | 8 | | 3 | 1 | 9 | | 3 | 1 | 10 | +---------+------------+------------+ to summarize... SELECT p.plan_name , v.version_no , DATE_FORMAT(v.valid_from, '%m/%d/%Y') as `From` , DATE_FORMAT(v.valid_until, '%m/%d/%Y') as `Until` , GROUP_CONCAT(f.title ORDER BY title SEPARATOR ', ') as features FROM plan p JOIN plan_version v USING (plan_id) JOIN entitlement e USING (plan_id, version_no) JOIN feature f USING (feature_id) GROUP BY plan_id, version_no; +-----------+------------+------------+------------+--------------------------------------------------+ | plan_name | version_no | From | Until | features | +-----------+------------+------------+------------+--------------------------------------------------+ | Silver | 1 | 06/01/2019 | 12/31/2019 | AAA, BBB, CCC | | Silver | 2 | 01/01/2020 | 12/31/9999 | AAA, BBB, CCC, DDD | | Gold | 1 | 06/01/2019 | 10/31/2019 | AAA, BBB, CCC, DDD, EEE, FFF | | Gold | 2 | 11/01/2019 | 02/29/2020 | AAA, BBB, CCC, DDD, EEE, FFF, GGG | | Gold | 3 | 03/01/2020 | 12/31/9999 | AAA, BBB, CCC, DDD, EEE, FFF, GGG, HHH | | Platinum | 1 | 12/01/2019 | 12/31/9999 | AAA, BBB, CCC, DDD, EEE, FFF, GGG, HHH, JJJ, KKK | +-----------+------------+------------+------------+--------------------------------------------------+ to get the ones in effect on a particular date, add a where clause to the above query ... WHERE CURDATE() BETWEEN v.valid_from AND v.valid_until ... +-----------+------------+------------+------------+--------------------------------------------------+ | plan_name | version_no | From | Until | features | +-----------+------------+------------+------------+--------------------------------------------------+ | Silver | 2 | 01/01/2020 | 12/31/9999 | AAA, BBB, CCC, DDD | | Gold | 2 | 11/01/2019 | 02/29/2020 | AAA, BBB, CCC, DDD, EEE, FFF, GGG | | Platinum | 1 | 12/01/2019 | 12/31/9999 | AAA, BBB, CCC, DDD, EEE, FFF, GGG, HHH, JJJ, KKK | +-----------+------------+------------+------------+--------------------------------------------------+ ** ** ** Now to address your question about preventing date overlaps. Suppose that on March 1st you are adding a new feature 'LLL/page11.php' for Platinum plans and you want to introduce a new version. Add new feature LLL Select Platinum v1 record for editing On the edit form, change date_from to "2020-03-01" and add LLL to feature list. However, when you update the version table, leave the "from" date as it is and change the "until" date to the date before the new one (2020-02-29), save insert a new record for Platinum / v2 / 2020-03-01 / 9999-12-31 insert entitlement records for new version
-
The first parts of the code, which use ->exec(), can be thrown away once you have your data table. (I just used those to create the db table and test data) You will need to set up your own connection to SQLite and use that. Meanwhile here is a DB-less version that generates sign_x and sign_y data for random squiggles. <?php $tdata = ''; for ($i=1; $i<=10; $i++) { $tdata .= randomSignature($i) ; } function randomSignature($i) { $n1 = mt_rand(1, 15); $n2 = mt_rand(1, 15); $x = $y = []; for ($a = 1; $a < 360; $a++) { $x[] = $a; $y[] = round(35 + sin(deg2rad($n1 * $a)) * 15 - cos(deg2rad($n2 * $a)) * 15); } $sx = join(',',$x); $sy = join(',',$y); return "<tr><td>$i</td><td>$n1</td><td>$n2</td><td>" . drawImage($sx, $sy) . "</td>\n"; } function drawImage($xcoords, $ycoords) { $xa = explode(',', $xcoords); $ya = explode(',', $ycoords); $w = max($xa)+1; $h = max($ya)+1; $scaled_h = 50; $scaled_w = $scaled_h * $w / $h; $path = "M $xa[0] $ya[0] "; unset($xa[0], $ya[0]); foreach ($xa as $i => $x) { $y = $ya[$i]; $path .= "L $x $y "; } $im = "<svg width='$scaled_w' height='$scaled_h' viewBox='0 0 $w $h'> <path d='$path' stroke='#000' stroke-width='1' fill='none'/> </svg>"; return $im; } ?> <!DOCTYPE html> <html> <head> <style type="text/css"> table { border-collapse: collapse; width: 60%; } th { background-color: black; color: white; padding: 8px; text-align: left; } td { padding: 4px 8px;; } </style> </head> <body> <table border="1" style="width: 60%;"> <tr><th>ID</th> <th>N1</th> <th>N2</th> <th>Image</th> <tr> <?=$tdata?> </table> </body> </html>
-
What is the error message?
-
Use curly braces $currency_symbol = '£'; \\\ Thats £ print "Price: {$currency_symbol}5";
-
Needs a "country" table (country_id/country_name) and store the id of the country (not the name) in your table.
-
OK, I'm going to stick my neck out and assume I know what you are talking about. I am guessing that the columns sign_x and sign_y contain coordinates and that you want to put them into (x,y) pairs and join the dots. If I'm right, here's an example. If I'm wrong, well, it was fun but you need to explain a little more.. $db->exec("CREATE TABLE IF NOT EXISTS `winuser_demo` ( `userid` int(11) NOT NULL AUTO_INCREMENT, `fname` varchar(20) DEFAULT NULL, `lname` varchar(20) DEFAULT NULL, `sign_x` varchar(500) DEFAULT NULL, `sign_y` varchar(500) DEFAULT NULL, PRIMARY KEY (`userid`) ) "); $db->exec("INSERT IGNORE INTO winuser_demo (userid,fname,lname,sign_x,sign_y) VALUES (1, 'Tom', 'Di Canari', '0,0,100,100,0,50,100,0', '100,20,20,100,20,0,20,100'); "); $tdata = ''; $res = $db->query("SELECT userid , fname , lname , sign_x , sign_y FROM winuser_demo "); foreach ($res as $r) { $tdata .= "<tr><td>{$r['userid']}</td> <td>{$r['fname']}</td> <td>{$r['lname']}</td> <td>" . drawImage($r['sign_x'], $r['sign_y']) . "</td>\n"; } function drawImage($xcoords, $ycoords) { $xa = explode(',', $xcoords); $ya = explode(',', $ycoords); $w = max($xa)+10; $h = max($ya)+10; $path = "M $xa[0] $ya[0] "; unset($xa[0], $ya[0]); foreach ($xa as $i => $x) { $y = $ya[$i]; $path .= "L $x $y "; } $im = "<svg width='$w' height='$h' > <path d='$path' stroke='#000' fill='none'/> </svg>"; return $im; } ?> <!DOCTYPE html> <html> <head> <style type="text/css"> table { border-collapse: collapse; width: 60%; } th { background-color: black; color: white; padding: 8px; text-align: left; } td { padding: 4px 8px;; } </style> </head> <body> <table border="1" style="width: 60%;"> <tr><th>ID</th> <th>First name</th> <th>Last name</th> <th>Image</th> <tr> <?=$tdata?> </table> </body> </html>
-
In fact this guy had the same problem a couple of days ago
-
-
The person_id should be unique in person_tbl. I showed you in my earlier reply how the persons_web_tbl should be.
-
But for future reference, as benanamen said, don't waste time checking first. Ensure that person_id is either the primary key or is defined as a UNIQUE key. Then it is impossible to insert a duplicate and will throw an error/exception. Insert the new record and check for a duplicate key error. If there is one, output error message or "found", otherwise it worked and job done. Example CREATE TABLE `user_demo` ( `userid` int(11) NOT NULL AUTO_INCREMENT, `fname` varchar(20) DEFAULT NULL, `lname` varchar(20) DEFAULT NULL, `username` varchar(20) DEFAULT NULL, PRIMARY KEY (`userid`), UNIQUE KEY `username` (`username`) ) DATA: +--------+-------+----------+----------+ | userid | fname | lname | username | +--------+-------+----------+----------+ | 1 | Laura | Norder | norderl | | 2 | Tom | DiCanari | tomd | | 3 | Harry | Potter | harryp | +--------+-------+----------+----------+ Attempting to insert another user with same username gives a 1062 Duplicate key error mysql> INSERT INTO user_demo (fname, lname, username) -> VALUES ('Harry', 'Palmer', 'harryp'); ERROR 1062 (23000): Duplicate entry 'harryp' for key 'username' The processing would be try { insert new record } catch (exception e) { if error number = 1062 echo "Duplicate!" end if }
-
That is not the way to do it. DB tables are not spreadsheets
-
In that case your "persons_web_table" needs to allow multiple person_id" EG person_id | platform -----------+------------ 1 | Facebook 1 | Twitter 1 | Instgram 2 | Facebook 2 | Twitter 3 | Twitter
-
Your method creates a connection each time you run a query. That method creates a single connection for each object and uses it for all the queries in the object. Better still would be to create a single connection for the page and pass that one connection to each new object when it is created. At the top of each page, have require 'db_inc.php'; // <--- CONTAINS ----- # const HOST = "localhost"; # const USER = "root"; # const PWD = ""; # const DBNAME = "cms"; # # function pdoconnect() # { # $dsn = 'mysql:host=' . HOST . ';dbname=' . DBNAME; # $pdo = new PDO ($dsn, USER, PWD); # $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); # $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); # $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); # return $pdo; # } # $connection = pdoconnect(); // create connection then your code becomes <?php class dbh { protected $conn; public function __construct($connection) { $this->conn = $connection; } } class PostsData extends dbh{ public function getPosts() { $sql = "SELECT * FROM posts_tbl"; $stmt = $this->conn->prepare($sql); $stmt->execute(); $result = $stmt->fetchAll((PDO::FETCH_OBJ)); return $result; } public function addPost($filter_author, $filter_title, $filter_txt) { $sql = "INSERT INTO posts_tbl (post_author, post_title, post_txt) VALUES (?, ?, ?)"; $stmt = $this->conn->prepare($sql); $stmt->execute([$filter_author, $filter_title, $filter_txt]); } } $post = new PostsData($connection); // pass connection to new object $posts = $post->getPosts(); foreach ($posts as $post) { echo $post->post_title . '<br>'; } ?>
-
You are still connecting in each of your functions instead of just once. Try <?php class dbh{ private $host = "localhost"; private $user = "root"; private $pwd = ""; private $dbname = "cms"; protected $conn; // make available to subclass public function __construct() { $this->conn = $this->connect(); } private function connect() { $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname; $pdo = new PDO ($dsn, $this->user, $this->pwd); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); return $pdo; } } class PostsData extends dbh{ public function getPosts() { $sql = "SELECT * FROM posts_tbl"; $stmt = $this->conn->prepare($sql); $stmt->execute(); $result = $stmt->fetchAll((PDO::FETCH_OBJ)); return $result; } public function addPost($filter_author, $filter_title, $filter_txt) { $sql = "INSERT INTO posts_tbl (post_author, post_title, post_txt) VALUES (?, ?, ?)"; $stmt = $this->conn->prepare($sql); $stmt->execute([$filter_author, $filter_title, $filter_txt]); } } $post = new PostsData(); $posts = $post->getPosts(); foreach ($posts as $post) { echo $post->post_title . '<br>'; } ?>
-
How do I query a DB Table's field, selected by variable as column name?
Barand replied to simona6's topic in MySQL Help
Normalize your data (as @requinix has just said while I was still typing) +------------------+ +------------------+ +------------------+ +------------------+ | gigassigned | | user | | user_platform | | platform | +------------------+ +------------------+ +------------------+ +------------------+ | userid |>----------------| id |-----------------<| userid | +------| platformid | | gigid | | selfdescription | | platformid |>-----+ | name | | answer | | usertype | | url | +------------------+ | dateadded | | username | | rangelow | +------------------+ | firstname | | rangehigh | | surname | +------------------+ | status | | datecreated | | town | +------------------+ -
I agree with ginerjm - add "low" and "high" columns. (You can run an update query using the same functions I have used below to populate them) But, given what you have currently SELECT * FROM simona6; +----+-------+-------------+ | id | name | followers | +----+-------+-------------+ | 1 | Curly | 2500-5000 | | 2 | Larry | 5000-10000 | | 3 | Mo | 2500-5000 | | 4 | Peter | 5000-10000 | | 5 | Paul | 10000-15000 | | 6 | Mary | 15000-20000 | +----+-------+-------------+ SELECT name , substring_index(followers, '-', 1) as low , substring_index(followers, '-', -1) as high FROM simona6 WHERE 3000 BETWEEN substring_index(followers, '-', 1) AND substring_index(followers, '-', -1); +-------+------+------+ | name | low | high | +-------+------+------+ | Curly | 2500 | 5000 | | Mo | 2500 | 5000 | +-------+------+------+
-
Do not create a connection every time you want to execute a query. connecting probably takes longer than the query so you more than double the execution time you will soon run out of connections certain functions (such as lastInsert() ) are valid only in the current connection Create the connection (once per script unless more than one server is used), store in $this->connection, or similar, and use that each time for the function calls.
-
There are 5 parts to an ajax call generating the call (which you have shown us) The headers sent to the server the processing on the server the response returned the processing of the response (also shown) All of those need to be correct.
-
Have you checked the calls' headers and responses in the network tab of your browser's developer tools?
-
… or their correct salutation is Dr, or some other title (eg Prof.) that is not gender-related