-
Posts
24,563 -
Joined
-
Last visited
-
Days Won
822
Everything posted by Barand
-
A prepared query is the best way to accomplish that. MYSQLI $inputname = "O'Brien"; $res = $mysqli->prepare("SELECT whatever from table1 where lastname = ? "; $res->bind->param("s", $inputname); $res->execute(); PDO $inputname = "O'Brien";; $res = $pdo->prepare("SELECT whatever from table1 where lastname = ? "; $res->execute( [$inputname] );
-
This example ensures every student has a unique matriculation number CREATE TABLE `student` ( `id` int(11) NOT NULL AUTO_INCREMENT, `firstName` varchar(50) NOT NULL, `lastName` varchar(50) NOT NULL, `matricNo` varchar(50) NOT NULL, `DOB` date NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `idx_student_matricNo` (`matricNo`) ) ;
-
Describe "not working". That on its own tells us nothing.
-
There's an example here, courtesy of @Psycho
-
Don't check if a user exisits by first running a query. Instead, you should put a UNIQUE constraint on the columns and trap duplicate key errors when they occur on inserting a record. Why a mix of mysqli and PDO. Just stick with PDO. Don't create a new connection for each query as you do in you signup_push function - that will really slow you down. Create the connection once and pass it in the function arguments. INSERT, UPDATE and DELETE queries do not create result sets. You are inserting then using fetchAll() to count non-existant results. PDO::row_count() will tell you how many were affected by the query.
-
Why duplicate all the code when the only thing that needs to change is the name of the database in the query?
-
get all keys that match value from multidimensional array
Barand replied to dodgeitorelse3's topic in PHP Coding Help
Use array keys instead of array_search $results = array_keys(array_column($data, 'oname'), 'Border.aao'); -
Example of form and writing its form data to two tables in different databases <?php $con = <your db connection code> ; if ($_SERVER['REQUEST_METHOD']=='POST') { $stmt1 = $con->prepare("INSERT INTO database1.mytable(col1, col2, col3) VALUES (?,?,?)"); // insert into table i database1 $stmt2 = $con->prepare("INSERT INTO database2.mytable(col1, col2, col3) VALUES (?,?,?)"); // insert into table i database2 $stmt1->execute([ $_POST['val1'],$_POST['val2'],$_POST['val3'] ]); $stmt2->execute([ $_POST['val1'],$_POST['val2'],$_POST['val3'] ]); } ?> <form method='post'> Value 1 <input name='val1'> Value 2 <input name='val2'> Value 3 <input name='val3'> <input type='submit>' </form>
-
OK. But, again, why do you need two pages? Are both databases on the same server? (Even if they aren't it just means you need two connections)
-
Why submit to 2 pages?
-
Show database entries by todays date plus 7 days
Barand replied to EarthDay's topic in PHP Coding Help
My query didn't involve any external variables as the date+7 is calculated in the query. I therefore used query() instead of prepare() - there is no execute() required with query(). Incidentally, your code just compares against now, now 7 days from now, -
Show database entries by todays date plus 7 days
Barand replied to EarthDay's topic in PHP Coding Help
Try $stmt = $pdo->query("SELECT * FROM care_plan_review WHERE reminder_date > CURDATE() + INTERVAL 7 DAY"); -
what is the most secure random number generator function to use?
Barand replied to alexandre's topic in PHP Coding Help
Don't use a number. Each character in a number can be 1 of 10 choices. If you use a string of mixed uppercase, lowercase, numbers and puctuation each character can be 1 of 90 (approx) characters. -
Fatal error: Uncaught ArgumentCountError: 3 arguments are required, 2 given in /var/www/html/cocoa/index.php:13 Stack trace: #0 /var/www/html/cocoa/index.php(13): printf('%s %s\n', '<a href='cocoa_...') #1 {main} thrown in /var/www/html/cocoa/index.php o
Barand replied to bertrc's topic in PHP Coding Help
Probably the wierdest (and worst) way of using printf that I have ever seen. -
How to Add Element to end of currently Iterating array
Barand replied to sen5241b's topic in PHP Coding Help
The first reply in this thread does it without array iterator - read it beyond the first sentence and look at the output. -
Fatal error: Uncaught ArgumentCountError: 3 arguments are required, 2 given in /var/www/html/cocoa/index.php:13 Stack trace: #0 /var/www/html/cocoa/index.php(13): printf('%s %s\n', '<a href='cocoa_...') #1 {main} thrown in /var/www/html/cocoa/index.php o
Barand replied to bertrc's topic in PHP Coding Help
https://www.php.net/print_f You don't need to select the same column twice. The %s in the print_f format argument are placeholders for the string variables ($row['type_chocolate']) mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); $mysqli = mysqli_connect("localhost", "root", "P-11fl32fg14", "cocoa"); $query = "SELECT type_chocolate FROM type_chocolate"; $result = mysqli_query($mysqli, $query); while ($row = mysqli_fetch_assoc($result)) { printf("<a href='cacau_type_chocolate.php?type_chocolate=%s'>%s</a>", $row['type_chocolate'], $row['type_chocolate']); } -
It does exactly the same thing. Apart from making the PHP processor work a little harder, what does it achieve?
-
-
@Zane I am pretty sure that these days #go-to-here at the end of the URL will send the user to the element on the page with id='go-to-here'
-
We'll probably never know. I did ask for his table structures but that got ignored. Good luck.
-
As @ginerjm stated, the AND is wrong - it should be a comma. Also the use of "... FROM users, messages" will result in a cartesian join. If you have a 100 users and each have 1 message, the query will return 10,000 rows (each user joined to each message). You need to specify the join criteria. What are the structures of your 2 tables? ie the output from these two queries.... DESCRIBE users; DESCRIBE messages;
-
Store dates and times in your DB as date, time or datetime types format yyyy-mm-dd-hh-ii-ss (that's what they are there for). You can then use the dozens of inbuilt datetime functions, sort and compare them. There is, however, a str_to_date() function in mysql to convert a string to a datetime type. Yo can use the date_format() function to convert a datetime field to your required output format (eg d/m/Y). Example My original table select * from deftest; +---------------------------------+ | test | +---------------------------------+ | Mon, 21 Nov 2022 10:05:13 +0200 | +---------------------------------+ Using the functions... select test as original , str_to_date(test, '%a, %d %b %Y %H:%i:%s') as asDatetime , date_format(str_to_date(test, '%a, %d %b %Y %H:%i:%s'), '%d/%m/%Y') as output from deftest; +---------------------------------+---------------------+------------+ | original | asDatetime | output | +---------------------------------+---------------------+------------+ | Mon, 21 Nov 2022 10:05:13 +0200 | 2022-11-21 10:05:13 | 21/11/2022 | +---------------------------------+---------------------+------------+
-
The error message would have told which line the error was on. Perhaps you could tell us?