Page 1 of 3 123 LastLast
Results 1 to 10 of 22

Thread: How to update mysql_result(mysql_query("SELECT COUNT(*) FROM `table`for php 7.4

  1. #1
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    637
    Thanks
    280
    Thanked 15 Times in 15 Posts

    Default How to update mysql_result(mysql_query("SELECT COUNT(*) FROM `table`for php 7.4

    I am updating an old site written in php 5.4 to php 7.4 and I am having trouble with this line...

    Code:
    $upcoming = mysql_result(mysql_query("SELECT COUNT(*) FROM `event` WHERE `approved`=1 AND (`startdate` >= '".$today."' OR `oge` > 0) "),0);
    I tried this:

    Code:
    if ($result4 = mysqli_query($connection, "SELECT COUNT(*) FROM `event` WHERE `approved`=1 AND (`startdate` >= '".$today."' OR `oge` > 0) "))  { $upcoming = mysqli_num_rows($result4); }
    but it gave the count as 1 instead of 422.

    Not sure what I am doing wrong. Please can one of you php pros shed some light? Thanks.

  2. #2
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,348
    Thanks
    99
    Thanked 112 Times in 110 Posts

    Default

    I guess I would need to see what the data is that you are working with, however I do see one error.

    Try changing:

    Code:
    $result4 = mysqli_query
    to:

    Code:
    $result4 == mysqli_query
    To choose the lesser of two evils is still to choose evil. My personal site

  3. The Following User Says Thank You to james438 For This Useful Post:

    kuau (02-24-2021)

  4. #3
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    637
    Thanks
    280
    Thanked 15 Times in 15 Posts

    Default

    Dear James: Thank you. Yes, that was definitely a mistake but, when I fixed it, then I got no result. The COUNT is for the number of upcoming events on an event calendar. You can see what I am trying to fix under the small calendar in the top center of this page: https://www.calendarmaui.com/ Probably I don't even need an IF clause. I'm simply trying to count the number of events in a table that are not expired. There must be something wrong with the COUNT phrase. Has that been deprecated in 7.4? Thanks for helping me.

  5. #4
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,348
    Thanks
    99
    Thanked 112 Times in 110 Posts

    Default

    Count has not been deprecated, but I noticed after updating my website to PHP 7.4 that some MySQL functions operated somewhat differently.

    For now I would get rid of the IF statement until we can figure out the solution and then add it back if you still need it.

    What is: SELECT COUNT(*)? Shouldn't it be SELECT * instead?

    I would write your query something like this:

    Code:
    $get_list="SELECT * FROM event WHERE approved=1 AND (startdate >= $today OR oge > 0)";
    $result4 = mysqli_query($connection, $get_list);
    $upcoming = mysqli_num_rows($result4);
    echo "number of results found: $upcoming";
    To choose the lesser of two evils is still to choose evil. My personal site

  6. The Following User Says Thank You to james438 For This Useful Post:

    kuau (02-24-2021)

  7. #5
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    637
    Thanks
    280
    Thanked 15 Times in 15 Posts

    Default

    Dear James: I just came here to tell you I figured it out by finding an example online here https://www.w3schools.com/Php/func_mysqli_num_rows.asp and then saw your solution which is basically the same as what I changed it to... in the example, they did not use ==, perhaps because it is just checking if there was a valid result...

    Code:
     $sql = "SELECT * FROM `event` WHERE `approved`=1 AND (`startdate` >= '".$today."' OR `oge` > 0)" ;
       if ($result=mysqli_query($connection,$sql)){ $upcoming = mysqli_num_rows($result); mysqli_free_result($result); } mysqli_close($connection);
    The original code was kind of a shortcut way of coding. It makes more sense to me the way you did it. Anyway, thanks very much. It seems the COUNT function may have been replaced with mysqli_num_rows. Anyway, it works perfectly now. Thanks again! aa

  8. #6
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,348
    Thanks
    99
    Thanked 112 Times in 110 Posts

    Default

    Just a quick note on IF statements with php.

    Code:
    <?php
    $test='0';
    if ($test=='1') $test="u";
    echo"$test";
    ?>
    The above uses "==" and produces "0".

    Code:
    <?php
    $test='0';
    if ($test='1') $test="u";
    echo"$test";
    ?>
    This second code snippet one "=" and produces "u". This is because by using one = you are setting the value of $test to '1'. I could use anything and it would be true. I could use $test='346' or $test='James' and it would be true.

    In the first example, by using two == you create a conditional statement which is either true or false.
    To choose the lesser of two evils is still to choose evil. My personal site

  9. The Following User Says Thank You to james438 For This Useful Post:

    kuau (02-26-2021)

  10. #7
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    637
    Thanks
    280
    Thanked 15 Times in 15 Posts

    Default

    Thanks for explaining. So the second case is more like if setting $test to 1 is true, then $test="u"? If the second example is not a conditional statement, why is the 'if' used? It seems kind of pointless if you can just say $test="u".

  11. #8
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    637
    Thanks
    280
    Thanked 15 Times in 15 Posts

    Default

    Thanks, Zinavo, but that code does not use mysqli. I am trying to upgrade to code that will work in 7.4 mysql_query has been deprecated. Sounds as if you may need to update your code too.

  12. #9
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,348
    Thanks
    99
    Thanked 112 Times in 110 Posts

    Default

    Quote Originally Posted by kuau View Post
    Thanks for explaining. So the second case is more like if setting $test to 1 is true, then $test="u"? If the second example is not a conditional statement, why is the 'if' used? It seems kind of pointless if you can just say $test="u".
    Correct. In the second example where only one = is used if ($test='1') $test="u"; it is a conditional statement, but it is always true. Since it is always true, it is not useful as an if statement.

    I wanted to be sure you were aware of that, otherwise it might cause trouble for you later.
    To choose the lesser of two evils is still to choose evil. My personal site

  13. #10
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    637
    Thanks
    280
    Thanked 15 Times in 15 Posts

    Default

    Dear James: Thank you so much! You made me realize that my code was actually causing the sql command to run twice. I thought it was checking to see if the command was successful but in fact I was re-executing the command. This resulted in every event being added twice to the database and every new location too. OMG. This had been going on for years and made a huge mess. And it all had to do with that fake conditional with the single =. Now I have to check all my code because I seem to have used that all over the place. Thanks for teaching me what it means.

Similar Threads

  1. Replies: 7
    Last Post: 02-07-2013, 03:25 PM
  2. Replies: 6
    Last Post: 04-06-2009, 11:27 AM
  3. I want to sort the table "sql:select results query" to appear in sequence of the arra
    By leonidassavvides in forum MySQL and other databases
    Replies: 0
    Last Post: 12-26-2008, 11:09 PM
  4. "select * " vs "select col1, col2 "
    By james438 in forum MySQL and other databases
    Replies: 5
    Last Post: 04-03-2007, 08:40 AM
  5. Select From Table Where Fieldvalue != ""
    By centenial in forum MySQL and other databases
    Replies: 1
    Last Post: 07-03-2006, 06:31 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •