|
Gypsy Boy
Redeemer



Registered: 03/17/17
Posts: 4,501
Loc: Deep in the discoteka
Last seen: 2 months, 24 days
|
Please help me with my PHP SQL code!
#26952921 - 09/24/20 02:54 PM (3 years, 4 months ago) |
|
|
Ive been busting my ass all evening trying to get this working.
Basically, my website you enter standard date thro a field:
<input name="realdate" type="date" >
I store this date in my database, no problems.
The rest of my website prints out "events" based on date: events that yet not come to pass are colorful (random colors) while events that are past by date (old) shoould appear grey.
This is the code that mean to do it:
Quote:
$sql= (" SELECT * FROM posts ORDER BY post_id DESC LIMIT 500 "); $result = mysqli_query($dbc, $sql);
while ($resultarr = mysqli_fetch_assoc($result)){ $background_colors = array('#FDCACA', '#FDBCE5', '#F0BCFF', '#C2BCF7', '#B2E9F9', '#BDFCD6', '#C7FCBD', '#FFF1C8', '#FFDCC1', '#FDCEC4'); $count = count($background_colors) - 1; $i = rand(0, $count); $rand_background = $background_colors[$i]; $r1 = $resultarr["message"]; $r2 = $resultarr["town"]; $r3 = $resultarr["dateofparty"]; $ql = ( " SELECT CURDATE() " ); $currentdate = mysqli_query($dbc, $ql); If ($r3 > $currentdate) $rand_background = '#6A645E'; echo "<textarea name=\"party\" cols=\"70\" rows=\"12\" maxlength=\"750\" wrap=\"soft\" class=\"party\" style=\"background-color: $rand_background \">";
But it doesnt work.
This bit here:
" If ($r3 > $currentdate) $rand_background = '#6A645E'; "
All my posts are coloured. If i change
($r3 > $currentdate)
to
($r3 < $currentdate)
All my items are grey. Whers only old items should be grey.
I spent good few hours fiddling with PHP date functions, but
SQL SELECT CURDATE()
Is probably the right choice , do it thro database
But it aint working right , please somebody help
--------------------
|
Ythan
ᕕ( ᐛ )ᕗ


Registered: 08/08/97
Posts: 18,774
Loc: NY/MA/VT Borderlands
Last seen: 37 minutes, 34 seconds
|
Re: Please help me with my PHP SQL code! [Re: Gypsy Boy]
#26953377 - 09/24/20 07:32 PM (3 years, 4 months ago) |
|
|
Lol, so after more than a decade I just learned something new about PHP: you can compare dates in YYYY-MM-DD format using the equality operators. I didn't realize that, but it should work the way you're doing it.
You want to inspect the value of $r3 and $currentdate to figure out what's going on. For a complex or confusing script you'd use a debugger like Xdebug but the quick and dirty solution is just to put this right before the comparison:
Code:
echo "r3: $r3 | currentdate: $currentdate<br>";
That will give you an idea of what's going on. The way you describe the problem, my guess is that $r3 is empty or not in YYYY-MM-DD format.
|
Gypsy Boy
Redeemer



Registered: 03/17/17
Posts: 4,501
Loc: Deep in the discoteka
Last seen: 2 months, 24 days
|
Re: Please help me with my PHP SQL code! [Re: Ythan]
#26954148 - 09/25/20 09:38 AM (3 years, 4 months ago) |
|
|
echo "r3: $r3 | currentdate: $currentdate<br>";
Does not work. Its $currentdate. It gives me error:
Quote:
Recoverable fatal error: Object of class mysqli_result could not be converted to string in C:\wamp64\www\date\browsehp.php on line 62
But echo "r3: $r3<br>";
Gives a valid date.
What the hell is going on...
--------------------
|
Gypsy Boy
Redeemer



Registered: 03/17/17
Posts: 4,501
Loc: Deep in the discoteka
Last seen: 2 months, 24 days
|
Re: Please help me with my PHP SQL code! [Re: Gypsy Boy]
#26954162 - 09/25/20 09:47 AM (3 years, 4 months ago) |
|
|
Even simple shit like this:
Quote:
DEFINE ('DB_USER', 'root'); DEFINE ('DB_PASSWORD', ''); DEFINE ('DB_HOST', 'localhost'); DEFINE ('DB_NAME', 'bbs');
$dbc = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die ('Couldnt connect' . mysqli_connect_error() ); mysqli_set_charset($dbc, 'utf8');
$sql = ( " SELECT CURDATE() " ); $currentdate = mysqli_query($dbc, $sql); echo "<br>"; echo "currentdate: $currentdate<br>"; echo "<br>";
Does no work, same error. Im guesssing MySQLs date format isnt compatable with PHPs string $
Something to do with loops maybe?
Quote:
Object of class Mysqli_result could not be converted to string What is Object of class mysqli_result Could Not be Converted to String Error? The mysqli_query() function returns object resource to the result variable. It does not return a string. So, this result cannot be converted into a string and hence the error occurs.
Hmmmmm
Shouldnt
Quote:
$ql = ( " SELECT CURDATE() " ); $currentdate = mysqli_query($dbc, $ql);
be outside the while loop?
--------------------
Edited by Gypsy Boy (09/25/20 10:18 AM)
|
lord_nikon6983
Seeker


Registered: 08/10/20
Posts: 26
Loc: Da Burgh
Last seen: 1 year, 7 months
|
Re: Please help me with my PHP SQL code! [Re: Gypsy Boy]
#26954203 - 09/25/20 10:07 AM (3 years, 4 months ago) |
|
|
I would highly recommend posting this on stack overflow as well. I am a DBA working my first real job and Ive gotten so much help from there.
-------------------- Not all those who wander are lost
|
Gypsy Boy
Redeemer



Registered: 03/17/17
Posts: 4,501
Loc: Deep in the discoteka
Last seen: 2 months, 24 days
|
Re: Please help me with my PHP SQL code! [Re: lord_nikon6983]
#26954225 - 09/25/20 10:19 AM (3 years, 4 months ago) |
|
|
Ok thanx for the tip
|
Gypsy Boy
Redeemer



Registered: 03/17/17
Posts: 4,501
Loc: Deep in the discoteka
Last seen: 2 months, 24 days
|
Re: Please help me with my PHP SQL code! [Re: Gypsy Boy]
#26954246 - 09/25/20 10:35 AM (3 years, 4 months ago) |
|
|
Also
Instead of creating DATE type column on mysql i just created default column or something
So i fixed that, still doesnt work
--------------------
|
Gypsy Boy
Redeemer



Registered: 03/17/17
Posts: 4,501
Loc: Deep in the discoteka
Last seen: 2 months, 24 days
|
Re: Please help me with my PHP SQL code! [Re: Gypsy Boy]
#26954283 - 09/25/20 11:04 AM (3 years, 4 months ago) |
|
|
Tried doing it the PHP way lol:
Quote:
$date = new DateTime(); If ($r3 > $date) $rand_background = '#6A645E'; echo "<br>"; echo "r3: $r3 | $date<br>";
Quote:
Recoverable fatal error: Object of class DateTime could not be converted to string
--------------------
|
Gypsy Boy
Redeemer



Registered: 03/17/17
Posts: 4,501
Loc: Deep in the discoteka
Last seen: 2 months, 24 days
|
Re: Please help me with my PHP SQL code! [Re: Gypsy Boy]
#26954341 - 09/25/20 11:35 AM (3 years, 4 months ago) |
|
|
ANNNNNNNNNNNND it works!!
If ($r3 < date("Y-m-d")) $rand_background = '#6A645E';
Only 3 lines, who would have thought  
--------------------
Edited by Gypsy Boy (09/25/20 11:44 AM)
|
Oggy
Stranger Danger


Registered: 12/05/14
Posts: 1,276
Loc: Planet Remulak
Last seen: 6 months, 27 days
|
Re: Please help me with my PHP SQL code! [Re: Gypsy Boy]
#26984904 - 10/14/20 11:08 AM (3 years, 3 months ago) |
|
|
I know this is old but I need to tell you that what you are doing with your SQL queries is insecure and will result in bad times™
You need to use prepared statements to insure you don't have nerds dropping tables on you or worse. https://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php PDO's are also a great start if you're stuck with php for this. https://www.php.net/manual/en/book.pdo.php
--------------------
Edited by Oggy (10/14/20 11:09 AM)
|
|