Jump to content


  • Content Count

  • Joined

  • Last visited

  • Days Won


Barand last won the day on January 16

Barand had the most liked content!

Community Reputation

1,247 Excellent

1 Follower

About Barand

  • Rank
    Sen . ( ile || sei )

Profile Information

Recent Profile Visitors

56,242 profile views
  1. Barand

    PDO double entries

    A prepared query should contain placeholders for values, not the values. Line 12 is missing a ";" at the end; The word "indentation" seems to missing from your vocabulary. Preparing or running queries inside loops should always be avoided. I would use a single mutiple record insert $startnum = 1; $endnum = 10; $placeholders = []; $values = []; for($i = $startnum; $i <= $endnum; ++$i) { $url = "http://api.somesite.com&page_number=$i"; $response = json_decode(file_get_contents($url), true); $mykeys = 'page_id, ' . implode(', ', array_keys($response)); $placeholders[] = "(?,?,?)"; array_push($values, $i, $response['county'], $response['country']); } //endfor $res = $db->prepare("INSERT INTO TestDB ($mykeys) VALUES " . join(',', $placeholders)) ; $res->execute($values);
  2. Barand

    Get specific MySQL/MariaDB error info

    Oh yes it will try { $db->exec("INSERT INTO units(id, unit) VALUES (1, 'Test')"); // deliberate duplicate } catch (PDOException $e) { echo '<pre>', print_r($e->errorInfo, 1), '</pre>'; } Outputs Array ( [0] => 23000 [1] => 1062 [2] => Duplicate entry '1' for key 'PRIMARY' )
  3. Barand

    Get specific MySQL/MariaDB error info

    Perhaps there is something here that might help
  4. Barand

    Insert Array values into mysql database

    That isn't what it means. The PDO connection is and object. A result from a query is an object. A statement returned by preparing a query is an object. (This is also true for mysqli but, unlike mysqli, the PDO result and statement objects have identical methods. With mysqli you have two different sets of methods to learn). As for firing objects at the database, there are object databases out there but MySQL isn't one of them.
  5. Barand

    Insert Array values into mysql database

    Binding is optional with PDO or you can pass an array of values (as I did) To get the most out of, set the right options when connecting. This my connection code (The constants and function definition are in an include file) const HOST = 'localhost'; const USERNAME = '????'; const PASSWORD = '????'; const DBNAME = "????"; function pdoConnect() { $dsn = "mysql:dbname=".DBNAME."; host=".HOST."; charset=utf8"; $db = new pdo($dsn, USERNAME, PASSWORD, [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, ]); return $db; } $db = pdoConnect(); [EDIT] It also lets you used name placeholders which can be helpful if you have a large number in your query. So I could have used $res = $db->prepare("INSERT INTO ludo (county, country) VALUES (:county, :country)"); $res->execute($data);
  6. Barand

    Insert Array values into mysql database

    PDO makes it even easier. I would strongly advise you switch from mysqli. The PDO solution is $res = $db->prepare("INSERT INTO ludo (county, country) VALUES (?,?)"); $res->execute(array_values($data));
  7. Barand

    Insert Array values into mysql database

    You could always engage brain, leave out the "array_keys" bit and provide the column names/placeholders yourself EG $data = [ 'county' => 'Cheshire', 'country' => 'England']; $res = $db->prepare("INSERT INTO ludo (county, country) VALUES (?,?)"); $res->bind_param('ss', ...array_values($data)); $res->execute();
  8. Barand

    Returning PDO query by default as array or objects

    My default is PDO::FETCH_ASSOC I have oocasionally (a search found 2) overridden that with fetch(PDO::FETCH_NUM) EG list() requires a numeric index, array_merge() works differently with associative and numeric indexes and that "..." variadic thing needs a numeric index.
  9. Barand

    Insert Array values into mysql database

  10. Barand

    Insert Array values into mysql database

    Silly me!. When you stated that the keys were the same names as the columns I thought there was some relevance to that.
  11. Barand

    Insert Array values into mysql database

    Is this what you are looking for $db->query("DROP TABLE ludo"); $db->query("CREATE TABLE ludo ( id int auto_increment not null primary key, county varchar(20), country varchar(20) )"); $data = [ 'county' => 'Cheshire', 'country' => 'England']; $fields = array_keys($data); $placeholders = array_fill(0, count($data), '?'); $res = $db->prepare("INSERT INTO ludo (" . join(',', $fields) . ") VALUES (" . join(',', $placeholders) . ")"); $res->bind_param('ss', ...array_values($data)); $res->execute();
  12. Barand

    Insert Array values into mysql database

    I'll ask again. The solution would differ depending on the array's origin. (For example, if it's from a query fetch() or posted form data). So what are you trying to accomplish here? (If you won't answer my questions, why should I bother to answer yours?)
  13. Barand

    Insert Array values into mysql database

    Where does the array come from?
  14. Barand

    1 = 5 ??????

    Before you connect to mysqli, call mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT); mysqli will now automatically throw errors without you having to keep checking if it worked or not. It saves a lot of coding.
  15. Barand

    1 = 5 ??????

    Don't use "SELECT * ". You only want the client column so why select everything. Use "SELECT client FROM ...." If you use "or die()", how do you expect the next line to execute?

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.