0

I am selecting values from a row, and inserting it into a table.

Receiving the error: mysql_fetch_array() expects parameter 1 to be resource, boolean given on line 42.

35 <?php
36 // Viewing input values from its respected row
37 $query = "SELECT * FROM servers";
38 $result = mysql_query($query);
39
40 echo "<table"; 
41
42 while($row = mysql_fetch_array($result)){
43 echo "<tr><td>" . $row['serverip'] . "</td><td>" ;
44
45 echo "</table>"; 
46
47 mysqli_close();
48
49 }
50 ?>

I am told that this error is caused because $result is not a resource.. however I have configured $result in line 38. Any ideas as to whats causing the error?

  • Did you try checking for errors after the `mysql_query`? – Rowland Shaw Jan 28 '14 at 19:53
  • 2
    youi connect to db some where? –  Jan 28 '14 at 19:53
  • echo mysql error description and code after line # 38 like, echo mysql_errno($link) . ": " . mysql_error($link); and see what error you got – Hassan Jan 28 '14 at 19:57
  • `mysql_query` returns a resource on success, and (as is your case) `false` on errors such as invalid query, no table, no permission to access the table or no DB connection. That said, do not use `mysql_*` functions in new code. They're deprecated and support for them is going be removed from PHP in the future. – lafor Jan 28 '14 at 20:00
  • Try mysql_query($query) or die (mysql_error()); Btw, you should close your table tag, and why is that td at the end of your echo statement (line 43) ? – JiFus Jan 28 '14 at 20:03
  • 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 Jan 29 '14 at 02:45

2 Answers2

0

Lots of issues at play here.

First off do not use MySQL it is deprecated use MySQLi isntead

Second rewrite your code as detailed here: http://us2.php.net/manual/en/mysqli.query.php

You need to define the DB connection in a variable

$link = mysqli_connect("localhost", "my_user", "my_password", "my_db");

Next store your query

$query = "SELECT * FROM servers";

echo your HTML

 echo "<table>"; //added the closing ">" as you left it out. 

Next Create an If statement to run the query against the DB in.

 if ($result = mysqli_query($link, $query)) {

Then add your while loop

     while($row = mysqli_fetch_array($result)) {

Add your table rows

          echo "<tr><td>" . $row['serverip'] . "</td></tr>" ; //changed </td> to </tr>

Close the While loop

     }

Free your result set

     mysqli_free_result($result);

close if statement

 }

Add error catching else statement

 else {

      printf("Errormessage: %s\n", mysqli_error($link));

 }

close DB connection

 mysqli_close($link);

echo your closing HTML

 echo "</table>";
wmfrancia
  • 1,176
  • 2
  • 10
  • 25
-1

please try following code:

$row=mysql_fetch_array($result,MYSQL_ASSOC)
Laxdeep
  • 9
  • 1