Mysql_fetch_object error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ThatPerson
Forum Commoner
Posts: 50
Joined: Wed Aug 03, 2011 1:57 am

Mysql_fetch_object error

Post by ThatPerson »

I am starting a new project, to create an online notepad for users to use. I have this code:

Code: Select all

<?php
ob_start();
include("database.php");
#Setting variables, to make life easy
if (isset($_COOKIE['loggeduser']) and isset($_COOKIE['loggedpass'])) {
$usr = $_COOKIE['loggeduser'];
$pwd = $_COOKIE['loggedpass'];
} else {
echo <<<END
<head>
<meta HTTP-EQUIV="REFRESH" content="0; url='login.php'">
</head>
END;
}
$sidepanel = "images/sidepanel.jpg";
$background = "images/back.jpg";
$notes = "images/notes.jpg";
$buttons = "images/sidebuttons.jpg";
$update = "images/update.jpg";
$p = 0;
 for ($i = 1; $i <= 8; $i++) {
   $p=$p+1;
   $note = "note".$p;
   $name = "note".$p."name";
   ${$note} = mysql_fetch_object(mysql_query("select '$note' from barker_users where Username = '$usr' and Password = '$pwd'")) -> $note;
   ${$name} = mysql_fetch_object(mysql_query("select '$name' from barker_users where Username = '$usr' and Password = '$pwd'")) -> $name;
  }
#JavaScript
echo <<<END
<head>
<script language="javascript">
  function hideAll(divid){
   document.getElementById(nte1).style.display = 'none';
   document.getElementById(nte2).style.display = 'none';
   document.getElementById(nte3).style.display = 'none';
   document.getElementById(nte4).style.display = 'none';
   document.getElementById(nte5).style.display = 'none';
   document.getElementById(nte6).style.display = 'none';
   document.getElementById(divid).style.display = 'block';
  }
</script>
</head>
END;
#Code for the side panel
echo <<<END
<div style="height:98%;width:20%;top:1%;left:0%;position:absolute;background:url($sidepanel) no-repeat;background-size:100%;">
 <div style="height:80%;width:90%;top:10%;left:0%;background-color:#FFFFFF;">
  <FORM NAME ="form1" METHOD ="POST" ACTION = "notepad.php">
  <input onclick="hideAll(nte1)" type="Submit" name="note1button" value="$note1name" style="background:url('$buttons');height:11%;width:100%;top:0%;">
  <input onclick="hideAll(nte2)" type="Submit" name="note2button" value="$note2name" style="background:url('$buttons');height:11%;width:100%;top:11%;">
  <input onclick="hideAll(nte3)" type="Submit" name="note3button" value="$note3name" style="background:url('$buttons');height:11%;width:100%;top:22%;">
  <input onclick="hideAll(nte4)" type="Submit" name="note4button" value="$note4name" style="background:url('$buttons');height:11%;width:100%;top:33%;">
  <input onclick="hideAll(nte5)" type="Submit" name="note5button" value="$note5name" style="background:url('$buttons');height:11%;width:100%;top:44%;">
  <input onclick="hideAll(nte6)" type="Submit" name="note6button" value="$note6name" style="background:url('$buttons');height:11%;width:100%;top:55%;">
  <div style="height:11%;width:100%;top:66%;background:url("$buttons");">
   <input type="Text" name="search" value="Search">
   <input type="Submit" name="searchbutton" value="Search">
  </div>
 <input onclick="hideAll(settings)" type="Submit" name="settings" value="Settings" style="background:url('$buttons');height:11%;width:100%;top:77%;">
 <input type="Submit" name="logout" value="Log Out" style="background:url('$buttons');height:11%;width:100%;top:88%;">
 </div>
</div>
END;
echo <<<END
<div style="position:absolute;height:90%;width:70%;top:5%;left:25%;background:url('$notes') no-repeat;background-size:100%;">
END;
$p = 0;
 for ($i = 1; $i <= 8; $i++) {
   $p=$p+1;
   $button = "update".$p; 
   $subject = "subject".$p; 
   $note = "text".$p; 
   $text = ${"note".$p};
   $subjecttext = ${"note".$p."name"};
   $id = "nte".$p;
echo <<<END
 <div style="height:100%;width:100%;top:0%;left:0%;background:url('$notes') no-repeat;background-size:100%;display:none; background-color:yellow;" id = "$id">
  <FORM NAME ="form1" METHOD ="POST" ACTION = "notepad.php">
  <input type="Submit" name="$button" value="Update" style = "height:10%;width:30px;left:50%;top:5%;">
  <input type="Text" name="$subject" value="$subjecttext" style = "height:10%;width:35%;left:5%;top:5%;">
  <input type="Text" name="$note" value="$text" style = "height:75%;width:90%;left:5%;top:10%;">
 </div>
END;
 }
echo <<<END
 <div style="height:100%;width:100%;top:0%;left:0%;background:url('$notes') no-repeat;background-size:100%;display:none;" id = "settings">
  <FORM NAME ="form1" METHOD ="POST" ACTION = "notepad.php">
 </div>
END;
?>
The error that occurs is:

[text]
Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in /home/bentatma/public_html/notes/stuff/notepad.php on line 27

Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in /home/bentatma/public_html/notes/stuff/notepad.php on line 28

Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in /home/bentatma/public_html/notes/stuff/notepad.php on line 29

Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in /home/bentatma/public_html/notes/stuff/notepad.php on line 30

Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in /home/bentatma/public_html/notes/stuff/notepad.php on line 31

Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in /home/bentatma/public_html/notes/stuff/notepad.php on line 32
[/text]

Which is most suprisingly, on an area which uses no mysql_fetch_object:

Code: Select all

  }
#JavaScript
echo <<<END
<head>
<script language="javascript">
  function hideAll(divid){
Can anybody see another reason that this may break like this?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Mysql_fetch_object error

Post by Christopher »

Your mysql_query() is failing. You should assign the result of mysql_query() to a variable and check if an error occurred before calling mysql_fetch_object().
(#10850)
ThatPerson
Forum Commoner
Posts: 50
Joined: Wed Aug 03, 2011 1:57 am

Re: Mysql_fetch_object error

Post by ThatPerson »

Ah, I found the problem. The MySQL query was looking for $note1, $note2, etc, where the 'Username' was $usr and 'Password' was $pwd, however the table had a 'users' column and 'password' column, so it could not work. Thanks for the help!
Post Reply