Jump to content

[SOLVED] Problem Reading from Database


TapeGun007

Recommended Posts

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. 

Link to comment
https://forums.phpfreaks.com/topic/168370-solved-problem-reading-from-database/
Share on other sites

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.
  }
}
?>

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!

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?

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';
  }
}
?>

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.

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*

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.