-
Posts
24,606 -
Joined
-
Last visited
-
Days Won
831
Everything posted by Barand
-
Apart from unnecessary field retrieval and loops, what is the nature of your problem?
-
If you take the MySQL route then use PDO to interfaace with DB. Create a connection file which can be included in your scripts. Mine is <?php #------------------------------------------------------------------------------------------+ # db_connect.php | #------------------------------------------------------------------------------------------+ const HOST = 'localhost'; const USERNAME = '????'; const PASSWORD = '????'; const DATABASE = '????'; // default db function pdoConnect($dbname=DATABASE) { $db = new PDO("mysql:host=".HOST.";dbname=$dbname;charset=utf8",USERNAME,PASSWORD); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); return $db; } Then the code you need will look similar to this <?php require 'db_connect.php'; $pdo = pdoConnect(); $res = $pdo->query("SELECT history , description FROM boats "); $output = ''; foreach ($res as $row) { $output .= "<span>History</span><br> {$row['history']}<br> <span>{$row['description']}</span><br> <br> "; } ?> <html> <body> <?= $output ?> </body> </html>
-
decode and use print_r(). Then follow the keys. echo $data->quoteSummary->result[0]->price->regularMarketPrice->raw;
-
why-My-Data-was-not-uploaded-in-the-Mysql-database(Connection.php)
Barand replied to AKASH2001's topic in PHP Coding Help
An interesting concept though. An input form pointed at a DB connection, at which point the DBAI interface takes over, processes the data, and updates the required tables for you. (MySQL v9.0 perhaps?) -
why-My-Data-was-not-uploaded-in-the-Mysql-database(Connection.php)
Barand replied to AKASH2001's topic in PHP Coding Help
What we don't see are check to see if data was posted to the page data validation an insert query to write the posted data to the database (When posting code in these forums, use the code button "<>" in the toolbar.) -
There is a difference between not knowing how to do something and knowing how to do it (in this case, find a section of a known site) but being too idle to do it yourself.
-
Why not? You are asking us to do everything else for you. You sit back and ask us to do all your legwork.
-
I'm sure he'd rather you read it to him like a bedtime story.
-
Mini php search engine, order with the search term
Barand replied to oz11's topic in PHP Coding Help
SELECT ... ORDER BY LOCATE('google', title) = 0 EG mysql> select * from test; +----------+ | title | +----------+ | Google | | Fox | | CNN | | BBC | | Google 2 | +----------+ 5 rows in set (0.00 sec) mysql> select * from test order by locate('google', title) = 0, title; +----------+ | title | +----------+ | Google | | Google 2 | | BBC | | CNN | | Fox | +----------+ 5 rows in set (0.02 sec) -
Try $futureDate = new DateTime('dec 25'); $now = new DateTime('today'); if ($now > $futureDate) { $futureDate->modify('+1 year'); } echo $now->diff($futureDate)->days;
-
Have you changed the browser?
-
Put them in an array, $var = [ "hello", "good bye", "back soon"]; for ($x=0; $x<3; $x++) { echo $var[$x] . '<br>'; } or $var0 = "hello"; $var1 = "good bye"; $var2 = "back soon"; test($var0, $var1, $var2); function test(...$vars) { for ($x=0; $x<3; $x++) { echo $vars[$x] . '<br>'; } }
-
You need a datetime object when using diff(), you are using you start time string. Re-read my example $event_date = new DateTime($row['evt_start']); $now = new DateTime(); $time_left = $event_date->diff($now)->format('%m months %d days %h hours %i minutes'); What were you expecting with lines like these? ... ( https://www.php.net/manual/en/function.date.php ) $month=date('m',$difference); $days=date('d',$difference);
-
Not sure which block of code we should be looking at, but you do seem to be making heavy weather of calculating date differences $event_date = new DateTime('2022-05-15 15:00:00'); $now = new DateTime(); echo $event_date->diff($now)->format('%m months %d days %h hours %i minutes'); // 3 months 24 days 22 hours 34 minutes
-
The same way that you are doing now. Did you read the code that you posted? foreach ($res as $row) { echo "<li>$row->cat_name ($row->total)</li>\n"; ^^^^^^^^^^^^^^ ^^^^^^^^^^^ cat_name total }
-
fetchAll() is a PDO method. It won't work if you're using mysqli objects. An alternative without it would be $res = $db->query("SELECT c1.cat_name , count(i.id) as total FROM ".DB_PREFIX."channels c1 JOIN ".DB_PREFIX."channels c2 ON c2.child_of = c1.cat_id JOIN ".DB_PREFIX."images i ON i.category = c2.cat_id GROUP BY c1.cat_name UNION SELECT c1.cat_name , count(i.id) as total FROM ".DB_PREFIX."channels c1 JOIN ".DB_PREFIX."images i ON i.category = c1.cat_id WHERE c1.child_of = 0 GROUP BY c1.cat_name "); echo "<ul>\n"; foreach ($res as $row) { echo "<li>{$row['cat_name']} ({$row['total']})</li>\n"; } echo "</ul>\n"; ?>
-
PDO is more streamlined mysqli has result objects and statement objects depending on whether you used query() or prepare(). The methods for handling the results of these two objects are completely different. This mean two confusing sets of methods to learn. On the other hand, PDO produces objects with exactly the same methods to process the results of query() or prepare(), and usage of prepare() is itself much simpler. Some mysqli methods are only available depending on your implementation of MySql. PDO works with multiple varieties of DBMS (mysql, postgres etc). The SQL dialects may be different but your php code remains the same if you change. Having used the old mysql_* for years I originally changed to mysqli (how different could it be?) After a long while I decided I had to learn PDO in order to support others who were using it. I really wished I'd switched to it originally instead of mysqli.
-
Yes, you told us before that it won't centre. Stop repeating the same ambiguous terms and tell us what you want - A or B, or something else?
-
Ah! The delights of mysqli. https://dev.mysql.com/doc/refman/8.0/en/commands-out-of-sync.html Do yourself favour and change to PDO.
-
$pdo in my code is a PDO object connecting to the database server. Example from PHP manual /* Connect to a MySQL database using driver invocation */ $dsn = 'mysql:dbname=testdb;host=127.0.0.1'; $user = 'dbuser'; $password = 'dbpass'; $pdo = new PDO($dsn, $user, $password);
-
Define Do you mean the content of the card is not centrally aligned, or the card itself is not in the centre of an area?
-
I'd do something like this (although my output is simplified as I have no idea what your methods are producing)... $res = $pdo->query("SELECT c1.cat_name , count(i.id) as total FROM prefix_channels c1 JOIN prefix_channels c2 ON c2.child_of = c1.cat_id JOIN prefix_images i ON i.category = c2.cat_id GROUP BY c1.cat_name UNION SELECT c1.cat_name , count(i.id) as total FROM prefix_channels c1 JOIN prefix_images i ON i.category = c1.cat_id WHERE c1.child_of = 0 GROUP BY c1.cat_name "); $result = $res->fetchAll(PDO::FETCH_OBJ); echo "<ul>\n"; foreach ($result as $row) { echo "<li>$row->cat_name ($row->total)</li>\n"; } echo "</ul>\n"; giving