HI All,

I am currently following a tutorial on mysql and php and I am getting this error on displaying results ready for an update in a form.

Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in /home/site/update.php on line 11

<?
$id=$_GET['id'];
$username="web183-sql";
$password="sqlpassword";
$database="web183-sql";
mysql_connect(localhost,$username,$password);

$query=" SELECT * FROM contacts WHERE id='$id'";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

$i=0;
while ($i < $num) {

$first=mysql_result($result,$i,"first");
$last=mysql_result($result,$i,"last");
$phone=mysql_result($result,$i,"phone");
$mobile=mysql_result($result,$i,"mobile");
$fax=mysql_result($result,$i,"fax");
$email=mysql_result($result,$i,"email");
$web=mysql_result($result,$i,"web");
?>

<form action="updated.php" method="post">
<input type="hidden" name="ud_id" value="<? echo $id; ?>">
First Name: <input type="text" name="ud_first" value="<? echo $first; ?>"><br>
Last Name: <input type="text" name="ud_last" value="<? echo $last; ?>"><br>
Phone Number: <input type="text" name="ud_phone" value="<? echo $phone; ?>"><br>
Mobile Number: <input type="text" name="ud_mobile" value="<? echo $mobile; ?>"><br>
Fax Number: <input type="text" name="ud_fax" value="<? echo $fax; ?>"><br>
E-mail Address: <input type="text" name="ud_email" value="<? echo $email; ?>"><br>
Web Address: <input type="text" name="ud_web" value="<? echo $web; ?>"><br>
<input type="Submit" value="Update">
</form>

<?
++$i;
}
?>

Any ideas.

Thanks
Sam

Dani AI

Generated

Quick expert note for : that warning means the value passed to the row-count function was not a valid MySQL result resource. Common immediate causes (already hinted at by replies) are: the query failed, the DB wasn't selected or the connection failed, the result was read after the connection was closed, or the input id was empty/invalid. Good pointers in the thread: on the row-count issue, on capturing DB errors, on verifying the id value, and on not closing the connection too early.

Suggested debugging checklist:

  • Verify the script actually connected and the database was selected; check and log the connection error message if present.
  • Confirm the id parameter is present and well-formed (inspect or log it). Cast to an integer when it must be numeric to avoid injection and unexpected SQL.
  • After running the query, test the query result for failure and surface the DB error string to see why it failed.
  • Do not close the DB connection before extracting rows from the result resource.
  • Run the exact SQL the script produces directly in the database client to confirm syntax and returned rows.

A modern, safer approach is to switch to mysqli or PDO with prepared statements. Example (mysqli minimal pattern):

<?php
$mysqli = new mysqli('localhost','user','pass','database');
if ($mysqli->connect_errno) { error_log($mysqli->connect_error); /* handle */ }
$id = (int)($_GET['id'] ?? 0);
$stmt = $mysqli->prepare('SELECT first,last,phone,email FROM contacts WHERE id = ?');
$stmt->bind_param('i',$id);
$stmt->execute();
$row = $stmt->get_result()->fetch_assoc(); // check for null
$stmt->close();
$mysqli->close();
?>

Note: mysql_* functions were removed from recent PHP releases; migrating to mysqli or PDO avoids compatibility issues and provides prepared statements to prevent SQL injection.

Recommended Answers

All 6 Replies

Maybe try this... Its What I've used. $num = mysql_num_rows($result); it might help.

this most likely means your query failed.

change:

$query=" SELECT * FROM contacts WHERE id='$id'";
$result=mysql_query($query);

to

$query="SELECT * FROM contacts WHERE id='$id'";
$result=mysql_query($query) or die( 'Error: ' . mysql_error() );

you had an extra space in the query which might make it fail, but I am not sure.

Maybe try this... Its What I've used. $num = mysql_num_rows($result); it might help.

i think the above is the solution....

Use this..

$num = mysql_num_rows($result);

hi,
the above all solutions looks right...

still u didnt get this try to do this...

echo $id;

i think id value is null....

2 things...

First in your sql query...
There is a space in your sql query..
" Select ......"
change it to :
$sql = "Select ....... ";

Secondly... u are using mysql_result just above the form thing,,

But before using the mysql_result thing, you have already closed the mysql connection.. i.e. mysql_close();

Remove it and move it to last of the page, after the <form>

<?
$id=$_GET['id'];
$username="web183-sql";
$password="sqlpassword";
$database="web183-sql";
mysql_connect(localhost,$username,$password);

$query=" SELECT * FROM contacts WHERE id='$id'";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

$i=0;
while ($i < $num) {

$first=mysql_result($result,$i,"first");
$last=mysql_result($result,$i,"last");
$phone=mysql_result($result,$i,"phone");
$mobile=mysql_result($result,$i,"mobile");
$fax=mysql_result($result,$i,"fax");
$email=mysql_result($result,$i,"email");
$web=mysql_result($result,$i,"web");
?>

<form action="updated.php" method="post">
<input type="hidden" name="ud_id" value="<? echo $id; ?>">
First Name: <input type="text" name="ud_first" value="<? echo $first; ?>"><br>
Last Name: <input type="text" name="ud_last" value="<? echo $last; ?>"><br>
Phone Number: <input type="text" name="ud_phone" value="<? echo $phone; ?>"><br>
Mobile Number: <input type="text" name="ud_mobile" value="<? echo $mobile; ?>"><br>
Fax Number: <input type="text" name="ud_fax" value="<? echo $fax; ?>"><br>
E-mail Address: <input type="text" name="ud_email" value="<? echo $email; ?>"><br>
Web Address: <input type="text" name="ud_web" value="<? echo $web; ?>"><br>
<input type="Submit" value="Update">
</form>

<?
++$i;
}
?><?
$id=$_GET['id'];
$username="web183-sql";
$password="sqlpassword";
$database="web183-sql";
mysql_connect(localhost,$username,$password);

$query=" SELECT * FROM contacts WHERE id='$id'";
$result=mysql_query($query);

$num=mysql_numrows($result);


$i=0;
while ($i < $num) {

$first=mysql_result($result,$i,"first");
$last=mysql_result($result,$i,"last");
$phone=mysql_result($result,$i,"phone");
$mobile=mysql_result($result,$i,"mobile");
$fax=mysql_result($result,$i,"fax");
$email=mysql_result($result,$i,"email");
$web=mysql_result($result,$i,"web");
?>

<form action="updated.php" method="post">
<input type="hidden" name="ud_id" value="<? echo $id; ?>">
First Name: <input type="text" name="ud_first" value="<? echo $first; ?>"><br>
Last Name: <input type="text" name="ud_last" value="<? echo $last; ?>"><br>
Phone Number: <input type="text" name="ud_phone" value="<? echo $phone; ?>"><br>
Mobile Number: <input type="text" name="ud_mobile" value="<? echo $mobile; ?>"><br>
Fax Number: <input type="text" name="ud_fax" value="<? echo $fax; ?>"><br>
E-mail Address: <input type="text" name="ud_email" value="<? echo $email; ?>"><br>
Web Address: <input type="text" name="ud_web" value="<? echo $web; ?>"><br>
<input type="Submit" value="Update">
</form>

<?
++$i;
}

mysql_close();
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.