-3

I have 5 records in my table still the if loop does not run & throws a

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\Crud\select.php on line 20

What should be the possible changes I make to get rid of the error?

$link=mysql_connect("localhost","root","");

$output='';
$sql="SELECT *FROM detail ORDER BY id DESC";
$result=mysql_query($sql);
$output .='

<div align="center">
<table border=5 width=500>
<tr>
<th width="40%">ID</th>
<th width="40%"> First Name</th>
<th width="40%"> Last Name</th>
</tr>';



if(mysql_num_rows($result)>0)

{
    while($row=mysql_fetch_array($result))
    {
    $output .='
    <tr>
    <td>'.$row["id"].'</td>
    <td class="name" data-id1"'.$row["id"].'" contenteditedtable>'.$row["name"].'</td>
    <td class="lname" data-id2"'.$row["id"].'" contenteditedtable>'.$row["lname"].'</td>
    <td>button type="button" name="delete_btn" data-id3="'.$row["id"].'" id="delete">Delete</button></td>
    </tr>';
    }

    $output .='
    <tr>
    <td></td>
    <td id="name" contenteditedtable></td>
    <td id="lname" contenteditedtable></td>

    <td><button type="button" name="add">Add</button></td>
    </tr>';

    }
    $output .='</table>
    </div>';
    echo $output;
Jonathan
  • 2,778
  • 13
  • 23
Celin Matthews
  • 61
  • 1
  • 3
  • 11
  • 1
    there are no affected rows in a select statement. – Jonathan Feb 18 '17 at 17:28
  • 1
    Possible duplicate of [Reference - What does this error mean in PHP?](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – miken32 Feb 19 '17 at 01:34

2 Answers2

2

Your actual problem is your SQL query, it's failing.

$sql="SELECT *FROM detail ORDER BY id DESC";

You need a space between the * and FROM

$sql="SELECT * FROM detail ORDER BY id DESC";

You could have other problems but that's what's happening right now. Since your SQL query is failing, it's returning a boolean which in this case is false.

Also as mentioned in my comment above, mysql_affected_rows is not used for select statements, here is an excerpt from the documentation.

Get the number of affected rows by the last INSERT, UPDATE, REPLACE or DELETE query

The function you actually want to use is mysql_num_rows.

Retrieves the number of rows from a result set. This command is only valid for statements like SELECT or SHOW that return an actual result set.

Jonathan
  • 2,778
  • 13
  • 23
  • i am sorry there was a mistake in the question. Its mysql_num_rows causing the error and not mysql_affected_rows. I have made the changes. Kindly check – Celin Matthews Feb 18 '17 at 17:36
  • Read the first half of my response. – Jonathan Feb 18 '17 at 17:36
  • yes i tried space between * and from ...it doesn't work – Celin Matthews Feb 18 '17 at 17:37
  • If you're still getting an error then you have even more problems with your statement (i.e. no database selected, table detail doesn't exist, id column doesn't exist on detail). get the output of `mysql_error()` after running `$result=mysql_query($sql);` and it'll give you more detailed information as to why it's failing. – Jonathan Feb 18 '17 at 17:40
0

The database connectivity was missing here hence created problems

mysql_select_db(database_name,connection);

The above code solves the problem

Celin Matthews
  • 61
  • 1
  • 3
  • 11