0

I created a custom MySQL function that generates a 15 character random string. When I test the function in phpmyadmin it generates the code fine. However when I run it from PHP it returns an error - "Notice: Undefined index: dex in file.php on line ###".

    $sql1 = "select GenerateRand() as dex;";
    $result1 = mysqli_query($con,$sql1);
    $row1 = mysqli_fetch_row($result1);
    echo $sql1 . "| |" . $row1['dex'];

I have tried the following

  • using the different mysqli_fetch options but none return a different result.
  • checking the query result for errors;
  • mysqli_errno($con) returns 0
  • mysqli_error($con) returns ""
  • mysqli_num_rows($result1) returns 1
  • mysqli_num_fields($result1) returns 1

SOLVED

After adding these lines to my code

    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

I realized that I had not granted my database user the execute PRIVILEGES.

It was still not working so after trying the var_dump again I realized that I needed to be using mysqli_fetch_array if I wanted to use $row['dex'] to retrieve the code.

Thank You all for your ideas.

Thank you for the help everyone!

Jeff
  • 25
  • 5
  • 1
    what does `var_dump($row1);` return? –  Jun 28 '19 at 00:18
  • changing my echo command to echo $sql1 . "| |" . var_dump($row1); the result is "NULL select GenerateRand() as dex;| |" – Jeff Jun 28 '19 at 00:24
  • 2
    Why aren't you checking the return status of your query for errors? –  Jun 28 '19 at 00:53
  • 1
    See [How to get MySQLi error information in different environments? / mysqli_fetch_assoc() expects parameter 1 to be mysqli_result](https://stackoverflow.com/questions/22662488/how-to-get-mysqli-error-information-in-different-environments-mysqli-fetch-as) – Phil Jun 28 '19 at 03:10
  • 1
    Do you need a ';' after dex in the query ? – SScotti Jun 28 '19 at 03:30

0 Answers0