Jump to content

[SOLVED] Warning: mysql_fetch_array():


sanderphp

Recommended Posts

I'm getting a Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource error.

 

I had this working but I was trying to limit the results shown to the user logged in. Can anyone tell what I have wrong?

 

function show_data(){


  // Title of page
  print "<center><h3>Stats</h3></center>";
  
  // this pulls in connection specific information to connect to the database
  include("dataConn.php");

  $rider_id = $_SESSION['login_id'];
  
  //connect to database
  mysql_select_db($database_dataConn, $dataConn);
  
  //create sql
  $sql_user_data="SELECT * FROM info WHERE rider_id = '$rider_id'";

  // this statement actually executes the sql
  $result = mysql_query($sql_user_data);
  
  
  
   // add a final row to the end of the table that has a link to add a record
  print("<tr><td align='right'><a href='".$_SERVER['PHP_SELF']."?pg=info&pgflow=addrecord'>New Record</a><td></tr>");
  //Create table
  print("<table border='1' align='center'><tr>");
  
    //create column headers based on feilds from table 
print("<th>id");
print("<th>date");
print("<th>total_distance");
print("<th>total_time");
print("<th>ave_speed");
print("<th>rider_id");

// add the delete column header
  print("<th>Delete</tr>");
//add edit column header
  print("<th>Edit"); 
  //now we iterate through the results of the SQL Query
  while($row = mysql_fetch_array($result)) {
  //create a new row in the table
  print("<tr>");
  print("<td>$row[id] </td>");
  print("<td>$row[date] </td>");
  print("<td>$row[total_distance] </td>");
  print("<td>$row[total_time] </td>");
  print("<td>$row[ave_speed] </td>");
  print("<td>$row[rider_id] </td>");

  // add a delete icon for the row
  print("<td align='center'><a href='".$_SERVER['PHP_SELF']."?pg=info&pgflow=deleterecord&id=$row[id]' ONCLICK=\"return confirmLink()\"><img src='images/delete_icon.png' width='16' height='16' border='0' alt='delete'></a></td>"); 

  // add an edit icon for the row
  print("<td align='center'><a href='".$_SERVER['PHP_SELF']."?pg=info&pgflow=editrecord&id=$row[id]'><img src='images/edit_icon.png' width='16' height='16' border='0' alt='edit'></a></td>"); 

  print("</tr>");
  } #end while( = mysql_fetch_array())
  
   
  // finally we close the table
  print("</table>");


}

Link to comment
Share on other sites

Change:

$result = mysql_query($sql_user_data);

To:

$result = mysql_query($sql_user_data) or die("ERROR!".mysql_error($dataConn));

 

That will display the error.  I think $dataConn is the connection to MySQL.  If not, change that variable.

Link to comment
Share on other sites

Here is the whole page.

 

 

<?
session_start();
# include ("secure.php");
include("dataConn.php");
// include("config.php");
// include("opendb.php");


$pgflow = $_GET[pgflow];

#global $application;
if ( $application != "yes" ) { 
print("
<html>
<head>
<title>Cycling Stats</title>
<link rel='stylesheet' href='styles/style.css' type='text/css' media='all' />
<script language=\"javascript\" type=\"text/javascript\" src=\"js/datetimepicker.js\"></script>
<script language=\"javascript\" type=\"text/javascript\">
<!--
function confirmLink() {
return confirm(\"Are you sure you want to Delete this Record?\")
}
//-->
</script>
</head>
<body>
");
}	

# Conditional statements to determine program flow
switch ($pgflow) {

case 'addrecord':
	new_record();
	break;
case 'insertrecord':
	insert_record();
	break;
case 'deleterecord':
	delete_record();
	break;   
case 'editrecord':
	edit_record();
	break;
case 'showchanges':  
	show_changes();
	break;
case 'verified':
	update_database();
	break;
default:
	show_data();
}




#-------------------------------------------------------------------------------------------------------------------------------
function delete_record(){
//get the variable datakey


//database info
include("dataConn.php");
// include("config.php");
// include("opendb.php");


   $id = $_GET[id];
   //print out variables so we can see if they made it
   #print ("$id <br>");
   
   $date = $_GET[date];
   //print out variables so we can see if they made it
   #print ("$date <br>");
   
   $total_distance = $_GET[total_distance];
   //print out variables so we can see if they made it
   #print ("$total_distance <br>");
   
   $total_time = $_GET[total_time];
   //print out variables so we can see if they made it
   #print ("$total_time <br>");
   
   $ave_speed = $_GET[ave_speed];
   //print out variables so we can see if they made it
   #print ("$ave_speed <br>");
   
   $rider_id = $_GET[rider_id];
   //print out variables so we can see if they made it
   #print ("$rider_id <br>");
   
//connect to database
mysql_select_db($database_dataConn, $dataConn);

// create sql

$sql_delete_record ="DELETE FROM `info` WHERE `id` ='$id' LIMIT 1 ; ";
#print ("$sql_delete_record");

// delete the old record if it exists
$result = mysql_query($sql_delete_record); 

// close database 
mysql_close();

//show records in table
show_data();

}
   
#-------------------------------------------------------------------------------------------------------------------------------
function new_record(){
   
//title of page and open table
print ("<br><br><center><h3> Add New Record in Table: info</h3></center>
       <form action='".$_SERVER['PHP_SELF']."?pg=info&pgflow=insertrecord' method='POST' name='log_entry'>
       <TABLE align='center' border='1'>");
print("<tr><td>id</td><td><input name='id' type='text' id='id'><img src='images/primarykey_icon.png' width='16' height='16' border='0' alt='Primary Key Feild'><img src='images/autoincrement_icon.png' width='16' height='16' border='0' alt='Auto Incrementing'></td></tr>
"); print("<tr><td>date</td><td><input name='date' type='text' id='Date'><a href=\"javascript:NewCal('date','ddmmmyyyy')\"><img src=\"images/calendar_icon.png\" width=\"16\" height=\"16\" border=\"0\" alt=\"Pick a date\"></a>
</td></tr>
"); print("<tr><td>total_distance</td><td><input name='total_distance' type='text' id='total_distance'></td></tr>
"); print("<tr><td>total_time</td><td><input name='total_time' type='text' id='total_time'></td></tr>
"); print("<tr><td>ave_speed</td><td><input name='ave_speed' type='text' id='ave_speed'></td></tr>
"); print("<tr><td>rider_id</td><td><input name='rider_id' type='text' id='rider_id'></td></tr>
");    
//add submit and reset buttons
print("
       <tr>
         <td><input type='submit' name='submit' value='submit'></td>
         <td><input type='reset' name='reset' value='reset'></td>
       </tr>
       </table>
       </form>
   ");


}
   
   
#-------------------------------------------------------------------------------------------------------------------------------
function insert_record(){

include("dataConn.php");
// include("config.php");
// include("opendb.php");
// Variables can be declared here
   
   $id = $_POST[id];
   //print out variables so we can see if they made it
   #print ("$id <br>");
   
   $date = $_POST[date];
   //print out variables so we can see if they made it
   #print ("$date <br>");
   
   $total_distance = $_POST[total_distance];
   //print out variables so we can see if they made it
   #print ("$total_distance <br>");
   
   $total_time = $_POST[total_time];
   //print out variables so we can see if they made it
   #print ("$total_time <br>");
   
   $ave_speed = $_POST[ave_speed];
   //print out variables so we can see if they made it
   #print ("$ave_speed <br>");
   
   $rider_id = $_POST[rider_id];
   //print out variables so we can see if they made it
   #print ("$rider_id <br>");
   


//connect to database
mysql_select_db($database_dataConn, $dataConn);

//create sql
$sql_insert = "INSERT INTO info (id, date, total_distance, total_time, ave_speed, rider_id) VALUES ('$id', '$date', '$total_distance', '$total_time', '$ave_speed', '$rider_id')";

#print("$sql_insert");

// this statement actually executes the sql
$result = mysql_query($sql_insert); # make this vaiable match the last one

// close database 
mysql_close();

//show records in table
show_data();

}
      
#-------------------------------------------------------------------------------------------------------------------------------
function edit_record(){

  
    // this pulls in connection specific information to connect to the database
include("dataConn.php");
// include("config.php");
// include("opendb.php");  
  
   //get vars
  
   $id = $_GET[id];
   //print out variables so we can see if they made it
   #print ("$id <br>");
   
   $date = $_GET[date];
   //print out variables so we can see if they made it
   #print ("$date <br>");
   
   $total_distance = $_GET[total_distance];
   //print out variables so we can see if they made it
   #print ("$total_distance <br>");
   
   $total_time = $_GET[total_time];
   //print out variables so we can see if they made it
   #print ("$total_time <br>");
   
   $ave_speed = $_GET[ave_speed];
   //print out variables so we can see if they made it
   #print ("$ave_speed <br>");
   
   $rider_id = $_GET[rider_id];
   //print out variables so we can see if they made it
   #print ("$rider_id <br>");
   
  //connect to database
  mysql_select_db($database_dataConn, $dataConn);
  
  //create sql
  $sql_table_data="SELECT * FROM info WHERE `id` ='$id' LIMIT 1 ;";

  #print("$sql_table_data");
  // this statement actually executes the sql
  $result = mysql_query($sql_table_data);

  //access record info and build form
  while($row = mysql_fetch_array($result)) {

//title of page and open table
print ("<br><br><center><h3> Edit Log entry </h3></center>
       <form action='".$_SERVER['PHP_SELF']."?pg=info&pgflow=showchanges' method='POST' name='log_entry'>
       <TABLE border='1'>");


  print("<tr><td>id</td><td><input name='id' type='text' id='id' value='$row[id]'><img src='images/primarykey_icon.png' width='16' height='16' border='0' alt='Primary Key Feild'>
<img src='images/autoincrement_icon.png' width='16' height='16' border='0' alt='Auto Incrementing'>
</td></tr>
"); 

  	  print("<input name='orig_id' type='hidden' id='orig_id' value='$row[id]'>
"); 


  
  print("<tr><td>date</td><td><input name='date' type='text' id='date' value='$row[date]'><a href=\"javascript:NewCal('date','ddmmmyyyy')\"><img src=\"images/calendar_icon.png\" width=\"16\" height=\"16\" border=\"0\" alt=\"Pick a date\"></a>
</td></tr>
"); 

  	  print("<input name='orig_date' type='hidden' id='orig_date' value='$row[date]'>
"); 


  
  print("<tr><td>total_distance</td><td><input name='total_distance' type='text' id='total_distance' value='$row[total_distance]'></td></tr>
"); 

  	  print("<input name='orig_total_distance' type='hidden' id='orig_total_distance' value='$row[total_distance]'>
"); 


  
  print("<tr><td>total_time</td><td><input name='total_time' type='text' id='total_time' value='$row[total_time]'></td></tr>
"); 

  	  print("<input name='orig_total_time' type='hidden' id='orig_total_time' value='$row[total_time]'>
"); 


  
  print("<tr><td>ave_speed</td><td><input name='ave_speed' type='text' id='ave_speed' value='$row[ave_speed]'></td></tr>
"); 

  	  print("<input name='orig_ave_speed' type='hidden' id='orig_ave_speed' value='$row[ave_speed]'>
"); 


  
  print("<tr><td>rider_id</td><td><input name='rider_id' type='text' id='rider_id' value='$row[rider_id]'></td></tr>
"); 

  	  print("<input name='orig_rider_id' type='hidden' id='orig_rider_id' value='$row[rider_id]'>
"); 


     
//add submit and reset buttons
print("
       <tr>
         <td><input type='submit' name='submit' value='submit'></td>
         <td><input type='reset' name='reset' value='reset'></td>
       </tr>
       </table>
       </form>
   ");
		}

}
   
   
#-------------------------------------------------------------------------------------------------------------------------------
function show_changes(){

// Variables can be declared here

   $id = $_POST[id];
    $orig_id = $_POST[orig_id];  
   //print out variables so we can see if they made it
   #print ("$id <br>");
   #print ("$orig_id <br>");
   
   $date = $_POST[date];
    $orig_date = $_POST[orig_date];  
   //print out variables so we can see if they made it
   #print ("$date <br>");
   #print ("$orig_date <br>");
   
   $total_distance = $_POST[total_distance];
    $orig_total_distance = $_POST[orig_total_distance];  
   //print out variables so we can see if they made it
   #print ("$total_distance <br>");
   #print ("$orig_total_distance <br>");
   
   $total_time = $_POST[total_time];
    $orig_total_time = $_POST[orig_total_time];  
   //print out variables so we can see if they made it
   #print ("$total_time <br>");
   #print ("$orig_total_time <br>");
   
   $ave_speed = $_POST[ave_speed];
    $orig_ave_speed = $_POST[orig_ave_speed];  
   //print out variables so we can see if they made it
   #print ("$ave_speed <br>");
   #print ("$orig_ave_speed <br>");
   
   $rider_id = $_POST[rider_id];
    $orig_rider_id = $_POST[orig_rider_id];  
   //print out variables so we can see if they made it
   #print ("$rider_id <br>");
   #print ("$orig_rider_id <br>");
   
  
  // this pulls in connection specific information to connect to the database
include("dataConn.php");
  // include("config.php");
  // include("opendb.php");
  
  //connect to database
  mysql_select_db($database_dataConn, $dataConn);
  
  //create sql
   $sql_user_data="SELECT * FROM info WHERE `id` ='$orig_id' LIMIT 1 ;";

  #print("$sql_user_data");
  // this statement actually executes the sql
  //$result = mysql_query($sql_user_data);
  $result = mysql_query($sql_user_data) or die("ERROR!".mysql_error($dataConn));


  //access record info and build form
  while($row = mysql_fetch_array($result)) {

//title of page and open table
print ("<br><br><center><h3> Please Verify Changed below and Submit to database if correct. </h3></center>
       <form action='".$_SERVER['PHP_SELF']."?pg=info&pgflow=verified' method='POST' name='log_entry'>
       <TABLE>");

   
if ($row[id]== $id) {$fontcolor = 'black';} else {$fontcolor = 'red';}
print("<tr><td><font color=$fontcolor>id</font></td><td><input name='xid' type='text' id='xid' value='$id' disabled></td></tr>");
print("<input name='id' type='hidden' id='id' value='$id'>");
print("<input name='orig_id' type='hidden' id='orig_id' value='$orig_id'>");
      
if ($row[date]== $date) {$fontcolor = 'black';} else {$fontcolor = 'red';}
print("<tr><td><font color=$fontcolor>date</font></td><td><input name='xdate' type='text' id='xdate' value='$date' disabled></td></tr>");
print("<input name='date' type='hidden' id='date' value='$date'>");
print("<input name='orig_date' type='hidden' id='orig_date' value='$orig_date'>");
      
if ($row[total_distance]== $total_distance) {$fontcolor = 'black';} else {$fontcolor = 'red';}
print("<tr><td><font color=$fontcolor>total_distance</font></td><td><input name='xtotal_distance' type='text' id='xtotal_distance' value='$total_distance' disabled></td></tr>");
print("<input name='total_distance' type='hidden' id='total_distance' value='$total_distance'>");
print("<input name='orig_total_distance' type='hidden' id='orig_total_distance' value='$orig_total_distance'>");
      
if ($row[total_time]== $total_time) {$fontcolor = 'black';} else {$fontcolor = 'red';}
print("<tr><td><font color=$fontcolor>total_time</font></td><td><input name='xtotal_time' type='text' id='xtotal_time' value='$total_time' disabled></td></tr>");
print("<input name='total_time' type='hidden' id='total_time' value='$total_time'>");
print("<input name='orig_total_time' type='hidden' id='orig_total_time' value='$orig_total_time'>");
      
if ($row[ave_speed]== $ave_speed) {$fontcolor = 'black';} else {$fontcolor = 'red';}
print("<tr><td><font color=$fontcolor>ave_speed</font></td><td><input name='xave_speed' type='text' id='xave_speed' value='$ave_speed' disabled></td></tr>");
print("<input name='ave_speed' type='hidden' id='ave_speed' value='$ave_speed'>");
print("<input name='orig_ave_speed' type='hidden' id='orig_ave_speed' value='$orig_ave_speed'>");
      
if ($row[rider_id]== $rider_id) {$fontcolor = 'black';} else {$fontcolor = 'red';}
print("<tr><td><font color=$fontcolor>rider_id</font></td><td><input name='xrider_id' type='text' id='xrider_id' value='$rider_id' disabled></td></tr>");
print("<input name='rider_id' type='hidden' id='rider_id' value='$rider_id'>");
print("<input name='orig_rider_id' type='hidden' id='orig_rider_id' value='$orig_rider_id'>");
      
   //add submit and reset buttons
print("
       <tr>
         <td><input type='submit' name='submit' value='submit'></td>
         <td><input type='reset' name='reset' value='reset'></td>
       </tr>
       </table>
       </form>
   ");
   
   } #end while($row = mysql_fetch_array($result))
  exit;
} #end function show_changes(){
   
#-------------------------------------------------------------------------------------------------------------------------------
function update_database(){

include("dataConn.php");
// include("config.php");
// include("opendb.php");
// Variables can be declared here
   
   $id = $_POST[id];
    $orig_id = $_POST[orig_id];
   //print out variables so we can see if they made it
   #print ("$id <br>");
   #print ("$orig_id <br>");
   
   $date = $_POST[date];
    $orig_date = $_POST[orig_date];
   //print out variables so we can see if they made it
   #print ("$date <br>");
   #print ("$orig_date <br>");
   
   $total_distance = $_POST[total_distance];
    $orig_total_distance = $_POST[orig_total_distance];
   //print out variables so we can see if they made it
   #print ("$total_distance <br>");
   #print ("$orig_total_distance <br>");
   
   $total_time = $_POST[total_time];
    $orig_total_time = $_POST[orig_total_time];
   //print out variables so we can see if they made it
   #print ("$total_time <br>");
   #print ("$orig_total_time <br>");
   
   $ave_speed = $_POST[ave_speed];
    $orig_ave_speed = $_POST[orig_ave_speed];
   //print out variables so we can see if they made it
   #print ("$ave_speed <br>");
   #print ("$orig_ave_speed <br>");
   
   $rider_id = $_POST[rider_id];
    $orig_rider_id = $_POST[orig_rider_id];
   //print out variables so we can see if they made it
   #print ("$rider_id <br>");
   #print ("$orig_rider_id <br>");
   


//connect to database
mysql_select_db($database_dataConn, $dataConn);

//create sql
$sql_update="UPDATE `info` SET `id` = '$id',`date` = '$date',`total_distance` = '$total_distance',`total_time` = '$total_time',`ave_speed` = '$ave_speed',`rider_id` = '$rider_id' WHERE `id` ='$orig_id' LIMIT 1 ;";
#print("$sql_update");

// this statement actually executes the sql
$result = mysql_query($sql_update); # make this vaiable match the last one

// close database 
mysql_close();

//show records in table
show_data();
  exit;
}
   
function show_data(){


  // Title of page
  print "<center><h3>Stats</h3></center>";
  
  // this pulls in connection specific information to connect to the database
  include("dataConn.php");

  $rider_id = $_SESSION['login_id'];
  
  //connect to database
  mysql_select_db($database_dataConn, $dataConn);
  
  //create sql
  $sql_user_data="SELECT * FROM info"; // WHERE rider_id = '$rider_id'";

  // this statement actually executes the sql
  $result = mysql_query($sql_user_data);
  
  
  
   // add a final row to the end of the table that has a link to add a record
  print("<tr><td align='right'><a href='".$_SERVER['PHP_SELF']."?pg=info&pgflow=addrecord'>New Record</a><td></tr>");
  //Create table
  print("<table border='1' align='center'><tr>");
  
    //create column headers based on feilds from table 
print("<th>id");
print("<th>date");
print("<th>total_distance");
print("<th>total_time");
print("<th>ave_speed");
print("<th>rider_id");

// add the delete column header
  print("<th>Delete</tr>");
//add edit column header
  print("<th>Edit"); 
  //now we iterate through the results of the SQL Query
  while($row = mysql_fetch_array($result)) {
  //create a new row in the table
  print("<tr>");
  print("<td>$row[id] </td>");
  print("<td>$row[date] </td>");
  print("<td>$row[total_distance] </td>");
  print("<td>$row[total_time] </td>");
  print("<td>$row[ave_speed] </td>");
  print("<td>$row[rider_id] </td>");

  // add a delete icon for the row
  print("<td align='center'><a href='".$_SERVER['PHP_SELF']."?pg=info&pgflow=deleterecord&id=$row[id]' ONCLICK=\"return confirmLink()\"><img src='images/delete_icon.png' width='16' height='16' border='0' alt='delete'></a></td>"); 

  // add an edit icon for the row
  print("<td align='center'><a href='".$_SERVER['PHP_SELF']."?pg=info&pgflow=editrecord&id=$row[id]'><img src='images/edit_icon.png' width='16' height='16' border='0' alt='edit'></a></td>"); 

  print("</tr>");
  } #end while( = mysql_fetch_array())
  
   
  // finally we close the table
  print("</table>");


}
?>

Link to comment
Share on other sites

Are you 100% sure about which query/line of code the error occurred on?

 

Your posted code contains several queries, however you only added the error checking that 947740 posted to one of them.

 

Could you post the actual error message that the last code you posted is causing.

Link to comment
Share on other sites

Adding the or die text now gives me this...

 

Fatal error: Call to undefined function mysql_user_data() in /home/sanderan/public_html/php/cycling/info1.php on line 489

 

 //$result = mysql_query($sql_user_data);
  $result = mysql_query($sql_user_data) or die("ERROR!".mysql_user_data($dataConn));

Link to comment
Share on other sites

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.