jsoeurt Posted December 7, 2007 Share Posted December 7, 2007 Hello, I've been trying to fix this for hours. I simply don't know what I'm doing wrong. I'm trying to get all the messages that are waiting for a certain userid from a MySQL database. <?php $id=$_SESSION['id']; mysql_connect("localhost", "XXXXXXXX", "XXXXXXXXXXX") or die(mysql_error()); mysql_select_db("XXXXXXXXXXX") or die(mysql_error()); $query = "SELECT * FROM 'messages' WHERE 'to' = $id"; echo $query; $result = mysql_query($query)or die ( mysql_error( ) ); while($record = mysql_fetch_object($query)){ echo ".$record->from."<br>"; echo ".$record->subject."<br>"; echo ".$record->counter."<br>"; } mysql_close($conn); ?> And this is the database... -- phpMyAdmin SQL Dump -- version 2.9.2-Debian-1.one.com6 -- http://www.phpmyadmin.net -- -- Host: localhost -- Generation Time: Dec 07, 2007 at 05:04 PM -- Server version: 5.0.32 -- PHP Version: 5.2.5 -- -- Database: `XXXXXXXXX` -- -- -------------------------------------------------------- -- -- Table structure for table `messages` -- CREATE TABLE `messages` ( `id` int(11) NOT NULL auto_increment, `from` int(11) NOT NULL, `to` int(11) NOT NULL, `subject` text NOT NULL, `body` text NOT NULL, `counter` int(11) NOT NULL, PRIMARY KEY (`id`), KEY `to` (`to`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COMMENT='Messages' AUTO_INCREMENT=4 ; -- -- Dumping data for table `messages` -- INSERT INTO `messages` VALUES (1, 12, 11, 'test2', 'lalala', 1); INSERT INTO `messages` VALUES (3, 14, 11, 'test4', 'adfjadhajdh', 1); It's probably something stupid... Quote Link to comment Share on other sites More sharing options...
boushley Posted December 7, 2007 Share Posted December 7, 2007 You MySQL looks fine... In the php... you want to change this line... while($record = mysql_fetch_object($result)){ And you should see stuff start happening... the mysql_fetch_object functions wants a resource... not the text query. Quote Link to comment Share on other sites More sharing options...
revraz Posted December 7, 2007 Share Posted December 7, 2007 Also, check to see if any rows returned if (mysql_num_rows($result) == 1) { $row = mysql_fetch_assoc($result); Quote Link to comment Share on other sites More sharing options...
jsoeurt Posted December 7, 2007 Author Share Posted December 7, 2007 I got this: SELECT * FROM 'messages' WHERE 'to' = 11You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''messages' WHERE 'to' = 11' at line 1 So I guess there's something wrong with the sql as well. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted December 7, 2007 Share Posted December 7, 2007 LOL. I was just going to post that your query has single-quotes around the table name and the column name. Remove the single-quotes around messages and to (however because to is reserved mysql keyword, either change the name of that column or use back-tacks `to`.) Quote Link to comment Share on other sites More sharing options...
jsoeurt Posted December 7, 2007 Author Share Posted December 7, 2007 Thanks. I guess the quotes were the problem. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.