Jump to content

Barand

Moderators
  • Posts

    24,319
  • Joined

  • Last visited

  • Days Won

    794

Everything posted by Barand

  1. And is the "apidata" table you are querying from the mysql shell in the same database that you are connecting to in your script?
  2. They are stings. print_r doesn't show the quotes. $arr = [ "apple" => "red", // array with string keys "orange" => "orange", "banana" => "yellow" ]; echo '<pre>', print_r($arr, 1), '</pre>'; /* OUTPUT FROM print_r() Array ( [apple] => red [orange] => orange [banana] => yellow ) */ echo '<pre>', var_dump($arr), '</pre>'; /* OUTPUT FROM var_dump() array(3) { ["apple"]=> string(3) "red" ["orange"]=> string(6) "orange" ["banana"]=> string(6) "yellow" } */
  3. To summarise - the record counts show 23 records are added. No errors are reported from attempts to add duplicate keys. You report that 46 records are in the table What is the query you are running that shows you 46 records?
  4. My pdo connection code looks like this. The ERRMODE option tells it to throw an exception error message when an error occurs $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 ]); If you use that you should get an error message from the 24th insert telling you where it is happening.
  5. View the rightmove page with your browser's developer tool/network tab open and watch the ajax calls
  6. Furlongs are nice, round unit of measure (1 furlong = 10 cricket pitches) I can't see Rightmove's code any better than you can.
  7. I guess you don't have any form of error reporting otherwise the second attempt to insert the records should now fail with duplicate key errors.
  8. How are you determining that it has been added twice?
  9. What about echo recordCount($db) . '<br>'; foreach ($newarr as $item => $val) { $res->execute( [ $item, $val ] ); } echo recordCount($db) . '<br>'; where recordCount() is function recordCount($db) { $res = $db->query("SELECT COUNT(*) FROM apidata"); return $res->fetchColumn(); }
  10. Why? TRUNCATE is faster than DELETE plus it resets the auto_increment counter.
  11. Sounds like something is causing the code to be run twice so you need to determine what that something is.
  12. Either they are in the $newarr twice or the code is being run twice (or already in the db before running once)
  13. The search value that the user enters will be available to search.php as $_GET['search']. If this has a value then you need to add a WHERE clause to your query to limit the results to just those rows that the user wants.
  14. I don't see why that would insert the data twice, unless you run the code twice. Why don't you just use a foreach loop? foreach ($newarr as $item => $val) { $res->execute( [ $item, $val ] ); }
  15. If ($lat, $lng) is the centre of the postcode area, then this will find the locations of properties within $d Km select lat, lng from properties where pow(lat-$lat, 2) + pow((lng-$lng)*cos(radians($lat)), 2) < pow($d/110.25, 2)
  16. Alternative $res = $db->prepare("INSERT INTO apidata (item, value) VALUES (?,?), (?,?), (?,?) "); $res->execute( [ 'apple', 'red, 'orange', 'orange', 'banana', 'yellow' ] ); Yes, iteration would be the best way to go if you want the separate inserts. I was just indicating you need three of them
  17. You currently have $res = $db->prepare("INSERT INTO apidata (item, value) VALUES (?,?,?)"); $res->execute("apple,orange,bannana", "red,orange,yellow"); You should have $res = $db->prepare("INSERT INTO apidata (item, value) VALUES (?,?)"); $res->execute("apple", "red"); $res->execute("orange", "orange"); $res->execute("banana", "yellow"); So you are going wrong everywhere
  18. Are you sure you are outputting the new array? $myarray = [ 'articles' => [ 'href' => 'http://admin.contenta.com/api/articles'], 'blocks' => [ 'href' => 'http://admin.contenta.com/api/blocks'], 'categories' => [ 'href' => 'http://admin.contenta.com/api/categories'] ]; foreach ($myarray as $k => $v) { $new[$k] = $v['href']; } echo '<pre>', print_r($new, 1), '</pre>'; /*** GIVES ME *** Array ( [articles] => http://admin.contenta.com/api/articles [blocks] => http://admin.contenta.com/api/blocks [categories] => http://admin.contenta.com/api/categories ) */
  19. Do you mean foreach ($myarray as $k => $v) { $new[$k] = $v['href']; }
  20. PDO With mysqli you get a result object when you use mysqli_query() or a statement object if you use mysqli_prepare. These two object types have different sets of methods. With PDO the result and statement objects are the same - so only one set of methods to remember. Also, with PDO, you are not limited to mysql databases.
  21. Just use the index hierarchy down to the item you want echo $array['data'][0]['attributes']['title']
  22. Don't store the multiple choices in the same record - each should be in a separated record. See this example
  23. The clue was in this statement If you bothered to use the PHP reference manual you would see that the execute command in mysqli does not work the same way as it does in PDO. With PDO you can pass the parameters for a prepared statemet as an array in the execute call (as I have). With mysqli you have to explicitly bind the parameters first then call execute(). The mysqli version of the code would be // // PROCESS POSTED DATA // if ($_SERVER['REQUEST_METHOD']=='POST') { $stmt = $db->prepare("INSERT INTO childmeal (child_id, meal_id, meal_date) VALUES ( ?, ?, CURDATE() )"); $stmt->bind_param('ii', $cid, $mid); foreach ($_POST['meal'] as $cid => $meals) { foreach ($meals as $mid) { $stmt->execute(); } } }
  24. It should be the first bit of code to be processed in the file. My code uses PDO, you use mysqli, so what is your code for that bit?
×
×
  • Create New...

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.