0

I have to create 6 queries and print them to an HTML document. 5 out of the 6 variable work perfectly and display the results, I use the same syntax for all 6 variables, but the 4th variable is not working despite checking all the syntax and not having any errors in my IDE. I am getting these two errors in the browser:

Notice: Undefined variable: containsNumbers in C:\xampp\htdocs\scripts\Project3\template.html.php on line 38

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\scripts\Project3\template.html.php on line 38

I've already gone through all the code to make sure all spelling is good and that the syntax matches specifically. I feel like the problem I'm having is with my for each loop.

Here is the php snippet

//Try catch statment with query to check if the first name contains numbers.
try{
    $sql = "SELECT * FROM employees WHERE first_name REGEXP '[0-9]' 'results'";
    $result = $pdo->query($sql);
} catch (PDOException $e){
    $output = "Error selecting id" . $e->getMessage();
    include 'output.html.php';
    exit();
}

// For each loop used to process results one at a time
foreach ($result as $row){
    //Stores each results of the query in an array called containsNum
    $containsNumbers[] = $row['results'];
}

And here is the HTML snippet

<h1>Here are the results of any names with numbers in them.</h1>
<div class="wrapper">
    <div id="section--4" class="section">
        <?php foreach ($containsNumbers as $employee): ?>
            <div><?php echo htmlspecialchars($employee, ENT_QUOTES, 'UTF-8');?></div>
        <?php endforeach; ?>
    </div>
</div>

I expect the result to print any names that have numbers in them, but I am just getting errors.

EDIT****: to add some context this is the question I am trying to answer: 4. Write a query to check if the first_name fields of the employees table contains numbers.

Community
  • 1
  • 1
Ben Smallwood
  • 43
  • 1
  • 7
  • 2
    If the query doesn't return any rows for whatever reason, that variable will be undefined. – Don't Panic May 02 '19 at 20:32
  • @Don'tPanic oh, so like if all the names don't have numbers in them? – Ben Smallwood May 02 '19 at 20:34
  • Yes, or if the query fails, although if your PDO connection is set up to convert mysql errors to PHP exceptions it won't make it to that point – Don't Panic May 02 '19 at 20:36
  • You may want to check `if (!empty($containsNumbers)) { ... }` – Jimmy Adaro May 02 '19 at 20:37
  • Is your connection set to trigger exceptions on SQL errors? – Dharman May 02 '19 at 20:38
  • 2
    What is the 'results' string in your SQL? REGEXP only takes one argument after, right? – Don't Panic May 02 '19 at 20:39
  • @Dharman yes my connection is set to trigger exceptions on SQL errors. – Ben Smallwood May 02 '19 at 20:41
  • @Don'tPanic I don't fully understand what you mean. You mean I can't have 'results' because it can only take one argument which is set to '[0-9]'? – Ben Smallwood May 02 '19 at 20:42
  • is your html snippet is at the end of the same file? – Alex May 02 '19 at 20:44
  • @Alex no it is in a different file – Ben Smallwood May 02 '19 at 20:46
  • just to debug could you do `die(print_r($result));` just before `foreach ($result as $row){` please? – Alex May 02 '19 at 20:55
  • @Alex This is what I got: PDOStatement Object ( [queryString] => SELECT * FROM employees WHERE first_name REGEXP '[0-9]' 'results' ) 1 – Ben Smallwood May 02 '19 at 20:56
  • @BenSmallwood yes, that's what I meant. – Don't Panic May 02 '19 at 20:57
  • @Don'tPanic I just tried Alex's result below that only has one argument and it still was giving me the same errors – Ben Smallwood May 02 '19 at 21:02
  • 1
    Ben that's probably because the query still didn't return any rows. If you run your SQL directly in Mysql (e.g. using workbench or PhpMyAdmin or whatever you use), do you get any results? – ADyson May 02 '19 at 21:14
  • 2
    If that returns rows, then the PHP is at fault. You mentioned that the HTML is in a different file to the PHP, so I'm wondering about a scope issue. Does the file with the HTML have an "include" or "require" statement which calls the script containing the database code? Does the $containsNumbers array have global scope, or is it limited to within a function? You haven't shown the context for the database code, or shown where you declared $containsNumbers prior to populating it. – ADyson May 02 '19 at 21:17

2 Answers2

3

You should initialize the variable to an empty array before the loop. Otherwise, if the query doesn't return any rows, the variable will be undefined.

$containsNumbers = array();
// For each loop used to process results one at a time
foreach ($result as $row){
    //Stores each results of the query in an array called containsNum
    $containsNumbers[] = $row['results'];
}

Your query also has a syntax error. The string 'results' at the end doesn't belong there. This should have triggered an exception. Are you sure you enabled PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION in your PDO options?

Barmar
  • 741,623
  • 53
  • 500
  • 612
1

First, it is not clear what you are trying to get when

"SELECT * FROM employees WHERE first_name REGEXP '[0-9]' 'results'"

To me this query itself should fail. Reading your code down there I suspect you want to alias the column first_name as results.

Secondly, according to php documentation query returns PdoStatement object. That means you need to fetch result from it in kind : fetch, fetchAll, etc.

Here is my final guess this is how your code should look like:

try{
    $sql = "SELECT first_name as results FROM employees WHERE first_name REGEXP '[0-9]'";
    $result = $pdo->query($sql);
} catch (PDOException $e){
    $output = "Error selecting id" . $e->getMessage();
    include 'output.html.php';
    exit();
}

$containsNumbers = $result->fetchAll(PDO::FETCH_ASSOC);
Alex
  • 16,739
  • 1
  • 28
  • 51