TapeGun007 Posted August 1, 2009 Share Posted August 1, 2009 I can't find the solution to a simple problem on Google. <?php if (isset($_POST["fUsername"])) { $con = mysql_connect('localhost', 'xxxx', 'xxx!'); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("movedb", $con); $result = mysql_query("SELECT * FROM agents"); while($row = mysql_fetch_array($result)) { echo $row['Agent_Name']; echo "<br />"; } mysql_close($con); } ?> The error I get is: PHP Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\inetpub\wwwroot\_Meir\agent_login.php on line 15 Line 15 is the While statement. Quote Link to comment https://forums.phpfreaks.com/topic/168370-solved-problem-reading-from-database/ Share on other sites More sharing options...
trq Posted August 1, 2009 Share Posted August 1, 2009 Put simply, your query has failed and $result equals false. You should always check any result before attempting to use it. <?php if (isset($_POST["fUsername"])) { $con = mysql_connect('localhost', 'xxxx', 'xxx!'); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("movedb", $con); if ($result = mysql_query("SELECT * FROM agents")) { if (mysql_num_rows($result)) { while($row = mysql_fetch_array($result)) { echo $row['Agent_Name']; echo "<br />"; } mysql_close($con); } else { // no results found, handle error. } } else { // query failed, handle error. } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/168370-solved-problem-reading-from-database/#findComment-888171 Share on other sites More sharing options...
TapeGun007 Posted August 1, 2009 Author Share Posted August 1, 2009 Ah! I'll have to look at this because there should be data (or so I assumed). Is there a link somewhere that is like a reference guide to what the errors mean so I can troubleshoot easier? And...btw, thank you so much! Quote Link to comment https://forums.phpfreaks.com/topic/168370-solved-problem-reading-from-database/#findComment-888172 Share on other sites More sharing options...
TapeGun007 Posted August 1, 2009 Author Share Posted August 1, 2009 Ok.... wait a minute. There IS data in the table. Using the mySQL Query Browser, there are two agents. The table is: agents: Agent_ID Agent_Name Agent_Password Agent_Email There are two records in there currently. Granted your code above fixed the error, why does it not see there is data? Quote Link to comment https://forums.phpfreaks.com/topic/168370-solved-problem-reading-from-database/#findComment-888181 Share on other sites More sharing options...
bundyxc Posted August 1, 2009 Share Posted August 1, 2009 Modifying Thorpe's code a tiny bit so that you can see errors: <?php if (isset($_POST["fUsername"])) { $con = mysql_connect('localhost', 'xxxx', 'xxx!'); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("movedb", $con); if ($result = mysql_query("SELECT * FROM agents")) { if (mysql_num_rows($result)) { while($row = mysql_fetch_array($result)) { echo $row['Agent_Name']; echo "<br />"; } mysql_close($con); } else { echo 'no results found'; } } else { echo 'query failed'; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/168370-solved-problem-reading-from-database/#findComment-888218 Share on other sites More sharing options...
TapeGun007 Posted August 1, 2009 Author Share Posted August 1, 2009 Ok, I modified the code slightly as well saying "connection successful" when connected to the db. I am getting the "Query Failed" when I run your code. I presume then, that this snippet of code is able to realize that there are records available, but for some reason the query fails. Now that I have the problem narrowed down, how do I fix this? There must be something very simple I am not getting here. I am used to writing code in ASP. For example: <% Dim Connection, Recordset Dim sSQL, sConnString sSQL="SELECT * FROM ChoirMembers WHERE Active = True ORDER BY ChoirMembers.LastName, ChoirMembers.FirstName" sConnString="PROVIDER=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("\db\Choir.mdb") Set connection = Server.CreateObject("ADODB.Connection") Set recordset = Server.CreateObject("ADODB.Recordset") connection.Open sConnString recordset.Open sSQL,connection %> I could then write a table and insert the values read from the db into the table cells. This is really what I'm trying to do in php. I plan to eventually convert this other website from ASP to PHP. I just need to learn the differences. If I can simply access the database, and spit out the records, I believe I would be well on my way. I just can't seem to get over this little hump. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/168370-solved-problem-reading-from-database/#findComment-888439 Share on other sites More sharing options...
TapeGun007 Posted August 2, 2009 Author Share Posted August 2, 2009 I found a sample code on w3schools.com <?php $con = mysql_connect("localhost","xxx",xxx"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("movedb", $con); $result = mysql_query("SELECT * FROM agents"); echo "<table border='1'> <tr> <th>Firstname</th> <th>Email</th> </tr>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['Agent_Name'] . "</td>"; echo "<td>" . $row['Agent_Email'] . "</td>"; echo "</tr>"; } echo "</table>"; mysql_close($con); ?> This did exactly what I needed it to do. *shrug* Quote Link to comment https://forums.phpfreaks.com/topic/168370-solved-problem-reading-from-database/#findComment-888573 Share on other sites More sharing options...
bundyxc Posted August 2, 2009 Share Posted August 2, 2009 Now, time to figure out the difference in the two codes.. the first one should have worked, in theory.. Quote Link to comment https://forums.phpfreaks.com/topic/168370-solved-problem-reading-from-database/#findComment-888575 Share on other sites More sharing options...
TapeGun007 Posted August 2, 2009 Author Share Posted August 2, 2009 Yeah... interesting. Unfortunately, I'm under a heavy deadline and will have to peek at that later. I have this bookmarked. I appreciate your willingness to help me earlier! Many thanks to you. Quote Link to comment https://forums.phpfreaks.com/topic/168370-solved-problem-reading-from-database/#findComment-888758 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.