Dynadot โ€” .com Registration $8.99

What's wrong here [Mysql]

Spaceship Spaceship
Watch

w1ww

Established Member
Impact
9
Whats wrong on this code (Mysql: 4.1.21-standard)?

I get this message:
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 ''title', 'desc', 'data') VALUES('Sandy Smith', '21', '24' )' at line 2

PHP:
{ 


mysql_connect("localhost", "username", "password") or die                      

('Error connecting to mysql');

mysql_select_db("news");

mysql_query("INSERT INTO news 
('title', 'desc', 'data') VALUES('Sandy Smith', '21', '24' ) ") 
or die(mysql_error());  


mysql_close($conn);

}

Thank you
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
Firstly, well done on using error reporting before asking for help lol. Most people don't which is just completely useless.

The problem is field names must, if at all, be encased with back ticks not single quotes/apostrophes

So your query should be:
PHP:
INSERT INTO news (`title`, `desc`, `data`) VALUES('Sandy Smith', '21', '24')

Matt
 
0
•••
Oh thank you, this was quick :P!

I was getting quite frustrated now that I finally decided to get into PHP/MySQL

Thank you once more!
 
0
•••
No problem, I'm the right guy to ask for this exact problem, since when i started out i did it 10000 times before i finally realized what i was doing wrong lol.

I also remember outputting "Resource ID #x" many times to my complete frustration - now i know that mysql_query returns a resource which means i would have most likely been trying to output the result of the function instead of using mysql_fetch_object/array etc :lol:

Little things like that you will pick up and won't make the mistakes in time.

*feels nostalgic*
 
Last edited:
0
•••
Eh,

We all need to start with something,

Do you know any place where I can see how to display data from mysql ?

Can't find a good tutorial :\!

Thank you!
 
0
•••
Indeed we do :)

http://www.pixel2life.com/tutorials/php_coding/mysql_integration/

There will 100% be something to help you there, it basically involves using a series of mysql functions (predominantly the 2 to connect and 1 to query) along with the basicmysql syntax. SELECT(from)/WHERE/ORDER and then using a function such as mysql_fetch_array or mysql_fetch_object in a loop (such as while) to output the data.

Stuff to click:
http://uk.php.net/manual/en/function.mysql-connect.php
http://uk.php.net/manual/en/function.mysql-select-db.php
http://uk.php.net/mysql_query
http://uk.php.net/mysql_fetch_array
http://uk.php.net/while
http://www.pixel2life.com/tutorials...ng_mysql_with_php_massive_introductory_guide/ <---
http://www.phpfreaks.com/tutorials/33/0.php <----
http://www.scriptplayground.com/tutorials/php/Fetch-MySQL-Row/


Example code:

PHP:
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password') or die('Could not connect: ' . mysql_error());
mysql_select_db('mysql_database', $link);

$query = mysql_query("SELECT * FROM `table`") or die(mysql_error());  # optional WHERE/other syntax before/after FROM 

while($row = mysql_fetch_array($query))
{
	echo $row['field_name'];
}

Read it logically and look up what you don't know or ask here again and you should be able to grasp it.
 
0
•••
Yup, I have it working now!

Any of this tutorials explains how to filter results? Like, I want to display only results that on the field 'date' have the text 'Jan'

Thanks Matt
 
0
•••
w1ww said:
Yup, I have it working now!

Any of this tutorials explains how to filter results? Like, I want to display only results that on the field 'date' have the text 'Jan'

Thanks Matt

If the date field contains exactly/just "Jan" then you would modify the query to something like:

PHP:
SELECT * FROM `table` WHERE date  = 'Jan'

However i am assuming it contains a full date or timestamp?

The best way (i find) to do this is by using a field with the value being the mysql function "NOW". I.e you date field becomes something like:

2007-01-10 18:33:50

You can then run a query like:
PHP:
SELECT * FROM `table` WHERE MONTH(timestamp) = 1

Sorry i can't explain better, but i am extremely busy right now with work. Pixel2life.com may be able to help you more :)

There are slightly less complicated ways of doing this by using a timestamp which can be researched.
 
0
•••
It just contains "Jan" so the first example works :)!

Thanks for your time, Matt.
 
0
•••
w1ww said:
It just contains "Jan" so the first example works :)!

Thanks for your time, Matt.

Haha, right o' :)

No problem.
 
0
•••
Unstoppable Domains
Domain Recover
NameMaxi - Your Domain Has Buyers
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back