Home | Community | Message Board


This site includes paid links. Please support our sponsors.


Welcome to the Shroomery Message Board! You are experiencing a small sample of what the site has to offer. Please login or register to post messages and view our exclusive members-only content. You'll gain access to additional forums, file attachments, board customizations, encrypted private messages, and much more!

Shop: PhytoExtractum Kratom Powder for Sale   Bridgetown Botanicals CBD Concentrates   Kraken Kratom Red Vein Kratom   Unfolding Nature Unfolding Nature: Being in the Implicate Order

Jump to first unread post Pages: 1
OfflineGypsy Boy
Redeemer
Male


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 :sad:


--------------------





Extras: Filter Print Post Top
OfflineYthanA
ᕕ( ᐛ )ᕗ
Male User Gallery

Registered: 08/08/97
Posts: 18,774
Loc: NY/MA/VT Borderlands Flag
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.


Extras: Filter Print Post Top
OfflineGypsy Boy
Redeemer
Male


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...


--------------------





Extras: Filter Print Post Top
OfflineGypsy Boy
Redeemer
Male


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)


Extras: Filter Print Post Top
Offlinelord_nikon6983
Seeker
Male

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


Extras: Filter Print Post Top
OfflineGypsy Boy
Redeemer
Male


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


Extras: Filter Print Post Top
OfflineGypsy Boy
Redeemer
Male


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


--------------------





Extras: Filter Print Post Top
OfflineGypsy Boy
Redeemer
Male


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




--------------------





Extras: Filter Print Post Top
OfflineGypsy Boy
Redeemer
Male


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 :crazy2::lol::strokebeard:


--------------------





Edited by Gypsy Boy (09/25/20 11:44 AM)


Extras: Filter Print Post Top
OfflineOggy
Stranger Danger
 User Gallery

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)


Extras: Filter Print Post Top
Jump to top Pages: 1

Shop: PhytoExtractum Kratom Powder for Sale   Bridgetown Botanicals CBD Concentrates   Kraken Kratom Red Vein Kratom   Unfolding Nature Unfolding Nature: Being in the Implicate Order


Similar ThreadsPosterViewsRepliesLast post
* sql getting started strangladesh 830 15 11/29/06 12:04 PM
by tak
* PHP Problem st0nedphucker 1,305 7 03/07/07 03:38 AM
by Seuss
* ATTN: PHP/MySQL Gurus
( 1 2 all )
elbisivni 2,075 20 09/04/07 06:04 PM
by elbisivni
* PHP simple login elbisivni 1,372 4 06/04/07 04:40 AM
by Seuss
* webmasters HELP ( php ) ChromeCrow 713 3 09/08/03 07:47 AM
by Seuss
* php easter egg Mycomancer 5,143 7 05/02/06 03:15 AM
by Le_Canard
* PHP vs. Cold Fusion Evolving 1,126 7 03/15/03 06:08 AM
by Lana
* PHP people HELLA_TIGHT 706 8 06/02/07 11:20 AM
by Ythan

Extra information
You cannot start new topics / You cannot reply to topics
HTML is disabled / BBCode is enabled
Moderator: trendal, automan, Northerner
326 topic views. 0 members, 0 guests and 2 web crawlers are browsing this forum.
[ Show Images Only | Sort by Score | Print Topic ]
Search this thread:

Copyright 1997-2024 Mind Media. Some rights reserved.

Generated in 0.024 seconds spending 0.006 seconds on 14 queries.