Jump to content

[SOLVED] mysql_fetch_array(): supplied argument is not a valid MySQL result resource


Recommended Posts

Error i Get:

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home3/sex1800/public_html/demos/search/search.php on line 13

 

Heres the code

 

<?php
$hostname_php_result_conn = "localhost";
$database_php_result_conn = "sex1800_phpresult";
$username_php_result_conn = "sex1800_demo";
$password_php_result_conn = "demotable";   
$php_result_conn = mysql_connect($hostname_php_result_conn, $username_php_result_conn, $password_php_result_conn);
mysql_select_db($database_php_result_conn,$php_result_conn);

if(isset($_POST['submit'])) {
   $roll_no = $_POST['roll_no'];
   
   $sql = mysql_query("SELECT * FROM result_table WHERE roll_no = $roll_no");
$row1 = mysql_fetch_array($sql);
$name = $row1['name'];
$marks = $row1['marks'];
   
   if($row1 == 0) {
      echo 'Error, ID does not exist';
   } else {

echo 'Name = '.$name.'<br />
Marks = '.$marks;

   }   
} else {
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>

<style type="text/css">
#header { width:100%;
background:lightblue;
margin:0px;
padding:0px;
}

#leftbar { float:left;
width:40%;
height:30px;
background:lightblue;
text-align:right;
}

#rightbar { float:left;
width:60%;
height:30px;
background:lightblue;
text-align:center;
}

#bar { width:100%;
background:lightblue;
}

#center {float:center;
width:60%;
height:10px;
background:lightblue;
align:center;
}

a { font-family:Arial;
font-size:16px;
background-color:aqua;
color:blue;
font-weight:bold;
vertical-align:text-bottom;
}

a:hover { font-family:Lucida Calligraphy;
font-size:ahoversize1;
background-color:yellow;
color:darkblue;
}
</style>

</head>
<center>
<body>
<br><Br><Br>
<h1> Search Roll Number </h1>

<Br><br>
<div id="header">
<div id="center">
<table>
<form action="search.php" method="post">
<tr>
<td>Roll Number:</td><td><input name="roll_no" type="text" id="roll_no" /></td>
</tr>
<tr>
<td align="center"><input type="submit" name="submit" value="search" /></td>
</tr>
</form>
</table>
</div>
<div id="leftbar">
</div>
<div id="rightbar">
</div>
</div>
<div id="bar">
</div>
<div id="header">
<div id="leftbar">
</div>
<div id="rightbar">
</div>
</div>


</body>
</html>
<?php
}
?>

$sql = mysql_query("SELECT * FROM result_table WHERE roll_no = $roll_no") or die (mysql_error());

 

May shed some light on the issue. You could also do the same for the db selection and connection if the query looks okay.

Now it Just Says "No database selected"

 

----------

Heres The Code:

 

Added Your Line and error_reporting(E_ALL);

 

<?php
error_reporting(E_ALL);

$hostname_php_result_conn = "localhost";
$database_php_result_conn = "sex1800_phpresult";
$username_php_result_conn = "sex1800_demo";
$password_php_result_conn = "demotable";   
$php_result_conn = mysql_connect($hostname_php_result_conn, $username_php_result_conn, $password_php_result_conn);
mysql_select_db($database_php_result_conn,$php_result_conn);

if(isset($_POST['submit'])) {
   $roll_no = $_POST['roll_no'];
   
   $sql = mysql_query("SELECT * FROM result_table WHERE roll_no = $roll_no") or die (mysql_error());
$row1 = mysql_fetch_array($sql);
$name = $row1['name'];
$marks = $row1['marks'];
   
   if($row1 == 0) {
      echo 'Error, ID does not exist';
   } else {

echo 'Name = '.$name.'<br />
Marks = '.$marks;

   }   
} else {
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>

<style type="text/css">
#header { width:100%;
background:lightblue;
margin:0px;
padding:0px;
}

#leftbar { float:left;
width:40%;
height:30px;
background:lightblue;
text-align:right;
}

#rightbar { float:left;
width:60%;
height:30px;
background:lightblue;
text-align:center;
}

#bar { width:100%;
background:lightblue;
}

#center {float:center;
width:60%;
height:10px;
background:lightblue;
align:center;
}

a { font-family:Arial;
font-size:16px;
background-color:aqua;
color:blue;
font-weight:bold;
vertical-align:text-bottom;
}

a:hover { font-family:Lucida Calligraphy;
font-size:ahoversize1;
background-color:yellow;
color:darkblue;
}
</style>

</head>
<center>
<body>
<br><Br><Br>
<h1> Search Roll Number </h1>

<Br><br>
<div id="header">
<div id="center">
<table>
<form action="search.php" method="post">
<tr>
<td>Roll Number:</td><td><input name="roll_no" type="text" id="roll_no" /></td>
</tr>
<tr>
<td align="center"><input type="submit" name="submit" value="search" /></td>
</tr>
</form>
</table>
</div>
<div id="leftbar">
</div>
<div id="rightbar">
</div>
</div>
<div id="bar">
</div>
<div id="header">
<div id="leftbar">
</div>
<div id="rightbar">
</div>
</div>


</body>
</html>
<?php
}
?>

I have changed the code i added some javascript thats all

 

it disallows users to type text only numbers in form field

 

heres the new code but i still get the same error

 

<?php
error_reporting(E_ALL);

$hostname_php_result_conn = "localhost";
$database_php_result_conn = "sex1800_phpresult";
$username_php_result_conn = "sex1800_demo";
$password_php_result_conn = "demotable";   
$php_result_conn = mysql_connect($hostname_php_result_conn, $username_php_result_conn, $password_php_result_conn);
mysql_select_db($database_php_result_conn,$php_result_conn);

if(isset($_POST['submit'])) {
   $roll_no = $_POST['roll_no'];
   
   $sql = mysql_query("SELECT * FROM result_table WHERE roll_no = $roll_no") or die (mysql_error());
$row1 = mysql_fetch_array($sql);
$name = $row1['name'];
$marks = $row1['marks'];
   
   if($row1 == 0) {
      echo 'Error, ID does not exist';
   } else {

echo 'Name = '.$name.'<br />
Marks = '.$marks;

   }   
} else {
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>

<style type="text/css">
#header { width:100%;
background:lightblue;
margin:0px;
padding:0px;
}

#leftbar { float:left;
width:40%;
height:30px;
background:lightblue;
text-align:right;
}

#rightbar { float:left;
width:60%;
height:30px;
background:lightblue;
text-align:center;
}

#bar { width:100%;
background:lightblue;
}

#center {float:center;
width:60%;
height:10px;
background:lightblue;
align:center;
}

a { font-family:Arial;
font-size:16px;
background-color:aqua;
color:blue;
font-weight:bold;
vertical-align:text-bottom;
}

a:hover { font-family:Lucida Calligraphy;
font-size:ahoversize1;
background-color:yellow;
color:darkblue;
}
</style>

<script type="text/javascript">

function numbersonly(myfield, e, dec) {
  var key;
  var keychar;

  if (window.event)
    key = window.event.keyCode;
  else if (e)
    key = e.which;
  else
    return true;
  keychar = String.fromCharCode(key);

  // control keys
  if ((key==null) || (key==0) || (key== || (key==9) || (key==13) || (key==27) )
    return true;

  // numbers
  else if ((("0123456789").indexOf(keychar) > -1))
    return true;

  // decimal point jump
  else if (dec && (keychar == ".")) {
    myfield.form.elements[dec].focus();
    return false;
  } else
    return false;
}

</script>


</head>
<center>
<body>
<br><Br><Br>
<h1> Search Roll Number </h1>

<Br><br>
<div id="header">
<div id="center">
<table>
<form action="search.php" method="post">
<tr>
<td>Roll Number:</td><td><input name="roll_no" type="text" id="roll_no" onkeypress="return numbersonly(this, event)" /></td>
</tr>
<tr>
<td align="center"><input type="submit" name="submit" value="search" /></td>
</tr>
</form>
</table>
</div>
<div id="leftbar">
</div>
<div id="rightbar">
</div>
</div>
<div id="bar">
</div>
<div id="header">
<div id="leftbar">
</div>
<div id="rightbar">
</div>
</div>


</body>
</html>
<?php
}
?>

Also, make sure an ID was input, otherwise instead of a nice error message they'll get this mess:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.