DannyM Posted February 7, 2008 Share Posted February 7, 2008 Hello everyone. I've been recieving this strange warning message in my scripts recently - Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\wamp\www\EasySwipeTimeSystems\addEmployee.php on line 23 - and I have no clue why they are popping up. It is connecting to the database just fine, and entering everything into the tables. However, it still wants to complain. Any ideas why? Or should I just suppress it with the @ sign? I've bolded the line of code where the error pops up <?php if($_COOKIE["User"] != "0001" || !$_COOKIE["User"]) { header("Location: Login.php"); } if(isset($_POST["back"])) { header("Location: userCP.php"); } //connect to the database $con=mysql_connect("********","****","*********"); //Select the database mysql_select_db("easyswipe_timecardtest",$con); //Check to see what they clicked if(isset($_POST["submit"])) { $sql="SELECT * FROM employees"; mysql_query($sql,$con); [b]while($row = mysql_fetch_array($sql))[/b] { if($row['ID'] == $_POST["ID"]) { echo "<b>ID already taken!</b>"; } else { //Insert into everything $sql=mysql_query("INSERT INTO employees(Name, ID) VALUES('$_POST[Name]', '$_POST[iD]')"); $sql=mysql_query("INSERT INTO Jan(Name, ID) VALUES('$_POST[Name]', '$_POST[iD]')"); $sql=mysql_query("INSERT INTO Feb(Name, ID) VALUES('$_POST[Name]', '$_POST[iD]')"); $sql=mysql_query("INSERT INTO Mar(Name, ID) VALUES('$_POST[Name]', '$_POST[iD]')"); $sql=mysql_query("INSERT INTO Apr(Name, ID) VALUES('$_POST[Name]', '$_POST[iD]')"); $sql=mysql_query("INSERT INTO May(Name, ID) VALUES('$_POST[Name]', '$_POST[iD]')"); $sql=mysql_query("INSERT INTO Jun(Name, ID) VALUES('$_POST[Name]', '$_POST[iD]')"); $sql=mysql_query("INSERT INTO Jul(Name, ID) VALUES('$_POST[Name]', '$_POST[iD]')"); $sql=mysql_query("INSERT INTO Aug(Name, ID) VALUES('$_POST[Name]', '$_POST[iD]')"); $sql=mysql_query("INSERT INTO Sep(Name, ID) VALUES('$_POST[Name]', '$_POST[iD]')"); $sql=mysql_query("INSERT INTO M1(Name, ID) VALUES('$_POST[Name]', '$_POST[iD]')"); $sql=mysql_query("INSERT INTO Nov(Name, ID) VALUES('$_POST[Name]', '$_POST[iD]')"); $sql=mysql_query("INSERT INTO M2(Name, ID) VALUES('$_POST[Name]', '$_POST[iD]')"); } } } //Center everything echo'<center> <img src="logo.png"/>'; echo "<form action='addEmployee.php' method='POST'>"; echo '<b>Employee Name</b> - <input type="text" name="Name"/>'; echo '<br/>'; echo '<b>Employee Phone</b> - <input type = "text" name = "phone"/>'; echo '<br/>'; echo '<b>Employee Address</b> - <input type = "text" name = "Address"/>'; echo '<br/>'; echo '<b>Employee ID</B> - <input type="text" name="ID"/>'; echo '<br/>'; echo '<input type="submit" name="submit" value="Add Employee"/>'; echo '<input type="submit" name="back" value="Return"/>'; echo '</form>'; ?> Quote Link to comment Share on other sites More sharing options...
Stooney Posted February 7, 2008 Share Posted February 7, 2008 The cause of that error is because the mysql query didnt return any results therefore not making $sql a valid mysql resource. try echoing mysql_error() after the query and see what it says. Quote Link to comment Share on other sites More sharing options...
DannyM Posted February 7, 2008 Author Share Posted February 7, 2008 mysql_error() provides no information when I echo it. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted February 7, 2008 Share Posted February 7, 2008 Your variables are a little off...needs to be: $sql="SELECT * FROM employees"; $result = mysql_query($sql,$con) or die(mysql_error()); while($row = mysql_fetch_array($result)) { Quote Link to comment Share on other sites More sharing options...
haku Posted February 7, 2008 Share Posted February 7, 2008 rhodesa beat me to it! You didn't assign the result of the query to anything, then you tried to fetch an array from $qry, which is a string, not a result of query. Quote Link to comment Share on other sites More sharing options...
DannyM Posted February 7, 2008 Author Share Posted February 7, 2008 Your variables are a little off...needs to be: $sql="SELECT * FROM employees"; $result = mysql_query($sql,$con) or die(mysql_error()); while($row = mysql_fetch_array($result)) { Ahh, I see. Nobody had informed me that the variable within mysql_fetch_array() had to be $result, I thought it was just an example that was used. I'll make note of this! Thanks everyone! Case solved. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.