Jump to content

A mysql function does not produce an exploitable value in a php script


Recommended Posts

$sqlresdays="SELECT DATEDIFF(CURDATE(),MAKEDATE(YEAR(CURDATE()),1)) AS date_difference";
            $resdays=mysqli_query($donnees,$sqlresdays);
            $daysinyr=mysqli_fetch_object($resdays);
            $divider = $daysinyr->date_difference;

When running the sql request raw, no problem, date_difference shows the right number but in the script nothing is echo'd. Thanks for clues on how to resolve this.

First please us the code icon (<>) in the top menu and specify PHP for your code.

If what you posted is your script then nothing is echoed because you are not echoing anything. Post all the relevant code.

2 minutes ago, gw1500se said:

First please us the code icon (<>) in the top menu and specify PHP for your code.

If what you posted is your script then nothing is echoed because you are not echoing anything. Post all the relevant code.

$sqlresdays="SELECT DATEDIFF(CURDATE(),MAKEDATE(YEAR(CURDATE()),1)) AS date_difference";
            $resdays=mysqli_query($donnees,$sqlresdays);
            $daysinyr=mysqli_fetch_object($resdays);
            $divider = $daysinyr->date_difference;
            echo $divider;
            $finalpop=intval($pop/$divider);

 

The above is the complete code. I actually try to divide the $pop (containing a big number) by the number of days in the actual calendar year.

$sqlresdays="SELECT DATEDIFF(CURDATE(),MAKEDATE(YEAR(CURDATE()),1)) AS date_difference";
$resdays=mysqli_query($donnees,$sqlresdays) or trigger)_error("Query failed: ".mysqli_error($donnees);
   if ($resdays->num_rows>0) {
      $daysinyr=mysqli_fetch_object($resdays);
      $divider = $daysinyr->date_difference;
      echo $divider;
      $finalpop=intval($pop/$divider);
   }
   else {
      echo "No resullts returned from query");
   }

 

Edited by gw1500se

Alternatively

$pop = 54321;

$res = $pdo->prepare("SELECT ROUND( ? / DAYOFYEAR(CURDATE()))");
$res->execute([$pop]);
echo $res->fetchColumn();             //--> 1025

or (no database required)...

echo round($pop / (date('z')+1));              //--> 1025

 

52 minutes ago, gw1500se said:

$sqlresdays="SELECT DATEDIFF(CURDATE(),MAKEDATE(YEAR(CURDATE()),1)) AS date_difference"; $resdays=mysqli_query($donnees,$sqlresdays) or trigger)_error("Query failed: ".mysqli_error($donnees); $daysinyr=mysqli_fetch_object($resdays); $divider = $daysinyr->date_difference; echo $divider; $finalpop=intval($pop/$divider);

 

Thank you very much Barand.
I run on PHP 5,6 in procedural style and I do not know whether I can mix your proposed PDO in my script.

$resdays=mysqli_query($donnees,$sqlresdays) or trigger)_error("Query failed: ".mysqli_error($donnees); appears to be faulty ...

Thank you for trying

 

The main point is the query. Use DAYOFYEAR to get the number of days from start of year, it's easier and doesn't produce a zero value. (and you can use that same query just as well with MySqli)

Yours will fail (div by 0) if you run it on the 1st Jan.

Also note the use of SQL is overkill when you can do what you want with a simple one liner in PHP.

Do you charge per line of code, or are you of the school of programming that thinks "Why use one line of code when you can do it with several?"

Anyway, here's a mysqli version

$pop = 54321;

$result = $donnees->query("SELECT DAYOFYEAR(CURDATE()) as dayno");
$row = $result->fetch_assoc();
echo round($pop / $row['dayno']);         //--> 1006

 

Thank you.
I applied your code and got INF as a result of 

echo round($pop / $row['dayno']);

? This is real weird. Sorry for the code writing.. I am just an autodidact amateur trying to assist an NGO in running their app . Further, I do not charge anything for this volunteer work.

Thanks.

42 minutes ago, jacquesvds said:

I applied your code and got INF as a result

Show your current code that gives that result.

Why do you insist on usng SQL for this?

Edited by Barand

Here is the code: 

$result = mysqli_query($donnees,"SELECT DAYOFYEAR(CURDATE()) as dayno");
            $row = mysqli_fetch_assoc($result);
            echo round($pop / $row['dayno']);

I use sql as I do not know any other way to program this. The whole app is written in PHP 5,8 and databases are mysql.

57 minutes ago, jacquesvds said:

I use sql as I do not know any other way to program this

I gave you a one line PHP solution in my first post

 

Your code works for me. It could be a conection problem. Check for mysql errors using mysqli_report()  (EG this is my mysqli connection code)...

mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);
$db = mysqli_connect(HOST,USERNAME,PASSWORD,$database);
$db->set_charset('utf8');

 

15 minutes ago, jacquesvds said:

I learned something.

It certainly wasn't how to read replies and take advice 🙂 , so again, in case you missed it, here's the 1 line solution with no SQL required

echo round($pop / (date('z')+1));

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.