0

I'm new to PHP and just doing a test site for practice. I've run across the following error:

Notice: Use of undefined constant sql - assumed 'sql' in F:\wamp\www\css\Index.php on line 33
Call Stack
#   Time    Memory  Function    Location
1   0.0003  671872  {main}( )   ..\Index.php:0
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in F:\wamp\www\css\Index.php on line 35
Call Stack
#   Time    Memory  Function    Location
1   0.0003  671872  {main}( )   ..\Index.php:0
2   1.0141  680104  mysql_fetch_array ( )   ..\Index.php:35

Here's a snip of the portion that it finds erroneous:

30              <?php
31              
32                  $sql="SELECT * FROM products ORDER BY name ASC";
33                  $query=mysql_query(sql);
34                  
35                  while ($row=mysql_fetch_array($query)) {
36              
37              ?>
38                  <tr>
39                      <td><?php echo $row['SKU'] ?></td>
40                      <td><?php echo $row['Product'] ?></td>
41                      <td><a href="#">Select Product</a></td>
42                  </tr>
43              <?php
44                  }
45              ?>

I've included the line numbers with the snip. The table that I'm trying to pull from is indeed "products," so I don't think that's the issue here. I've been scratching my head for the past hour or so trying to figure out where the issue is, but I can't figure it out. I guess I should take that as a warm welcome from Father PHP as an initiation rite of passage, huh?

user3450245
  • 25
  • 2
  • 5
  • The line mentioned by the error message is `$query=mysql_query(sql);`. And that's where you ought to look. – mario Mar 22 '14 at 17:16
  • possible duplicate of [mysql\_fetch\_array() expects parameter 1 to be resource, boolean given in select](http://stackoverflow.com/questions/2973202/mysql-fetch-array-expects-parameter-1-to-be-resource-boolean-given-in-select) – John Conde Apr 08 '14 at 03:07

2 Answers2

2

Variables must begin with $

This is wrong-

$query=mysql_query(sql);
                   ^^^ This must be $sql

This should work -

$query=mysql_query($sql);
                   ^Added $
Kamehameha
  • 5,423
  • 1
  • 23
  • 28
  • Ah, I figured it'd be something as simple as that. Thanks for the quick response! – user3450245 Mar 22 '14 at 17:22
  • @user3450245 Great! Glad to help! If the issue is resolved, kindly consider [accepting](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) one of the answers. – Kamehameha Mar 22 '14 at 17:24
  • Am I unable to accept them all? You guys all helped me so quickly. I don have the rep to upvote yet... – user3450245 Mar 22 '14 at 17:31
1

You forgot the $ on line 33:

$query=mysql_query(sql); // Note that you have here "sql", not "$sql".

Instead, you should use:

$query=mysql_query($sql);
Mureinik
  • 297,002
  • 52
  • 306
  • 350