Jump to content

[SOLVED] [Newbie] Can't get anything from database


jsoeurt

Recommended Posts

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

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.

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`.)

Archived

This topic is now archived and is closed to further replies.

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