Click to See Complete Forum and Search --> : PHP/MySQL
lone-nut
10-12-2000, 12:32 AM
I'm having a bit of trouble connecting to a mysql database using php... it's quite new to me. Can anyone tell me why this code doesn't work? I followed a simple example to a 'T', then tried to apply it to something a lil bit more complex and it's not working... well take a look...
$into = mysql_query("INSERT INTO php_guestbook
(name,msg,date,time) VALUES ($name,$msg,CURRENT_DATE,CURRENT_TIME)",$db);
$result = mysql_query("SELECT * FROM php_guestbook",$db);
echo "$result";
it's a simple guestbook... and the output html page gives nothing but "Resource ID#2"
anyone know what that means?
anyhelp will be greatly appreciated
Sweede
10-12-2000, 01:30 AM
$result is a pointer, its not a variable such as a string or an array
check out mysql_result, mysql_object, and the rest of the mysql functions.
blakesterz
10-12-2000, 10:13 AM
Something like this:
$result = mysql("dbname","insert into projects (title, tracker) values ('$title','$tracker')", $db);
Originally posted by lone-nut:
I'm having a bit of trouble connecting to a mysql database using php... it's quite new to me. Can anyone tell me why this code doesn't work? I followed a simple example to a 'T', then tried to apply it to something a lil bit more complex and it's not working... well take a look...
$into = mysql_query("INSERT INTO php_guestbook
(name,msg,date,time) VALUES ($name,$msg,CURRENT_DATE,CURRENT_TIME)",$db);
$result = mysql_query("SELECT * FROM php_guestbook",$db);
echo "$result";
it's a simple guestbook... and the output html page gives nothing but "Resource ID#2"
anyone know what that means?
anyhelp will be greatly appreciated
Sweede
10-12-2000, 01:03 PM
Originally posted by blakesterz:
Something like this:
$result = mysql("dbname","insert into projects (title, tracker) values ('$title','$tracker')", $db);
why would you use that?
it sloppy.
all of his code above is correct, its just not complete yet.
something like this will work much better for displaying results.
which is obviously unformated and will end up looking like this
sweedehello how are you today?10-12-2000120944
or something.
im to lazy to write out tables and what not right now, but if you want a tabled example, let me know
lone-nut
10-12-2000, 02:34 PM
so the $i is the index...
huh ok i was tying to use the myswl_fetch_array..
but i guess i was forgetting the index variable and it was ginv me errors...T_variables er somthing parse error somthing er other
ok Sweede that mysql_result function looks nice.. .i'll try that ...
I'll post how it goes...
thanks fro the help guys...(girls?0
-ln
lone-nut
10-12-2000, 03:11 PM
ok works great but how do I insert data into the mysql database?
I have my pointer reference...
and mysql_reslut gets info out... but I can't find a function
that puts it into the db....?
using this code
$db = mysql_connect("localhost","n5micone","n5micone");
mysql_select_db("n5micone",$db);
$into = mysql_query("INSERT INTO php_guestbook
(name,msg,date,time) VALUES ($name,$msg,CURRENT_DATE,CURRENT_TIME)",$db);
Sweede
10-12-2000, 03:53 PM
mysql_error() after the mysql_query() call will return the error message from mysql.
php has no real "function" to insert data into mysql, you use SQL statements to do that. php provides an interface for it.
my guess is that you're having errors because your not enclosing your input data in quotes.
try this...
$query = "INSERT INTO php_guestbook (name,msg,date,time) VALUES ('".$name."' ,'".$msg."' ,CURRENT_DATE,CURRENT_TIME)";
mysql_query($query);
your ending SQL statement being sent to mysql will look like this now
INSERT INTO php_guestbook (name,msg,date,time) VALUES ('name','Extended message with funky letters',10-12-2000,14:58:44)
which is what it should be anyways.
[This message has been edited by Sweede (edited 12 October 2000).]
lone-nut
10-12-2000, 07:00 PM
Darn... it should be working but it's not...
I mean the html works... but the query is not getting put into the database.
Here's the entired thing... can you see any reason why the sql statements wouldn't work?
the mysql_error() function didn't return anything...
It seems like everything is in place and should work.. but
again thanks for yer help Sweede (or any other peeps who want to lend their expertise!)
-ln
Sweede
10-13-2000, 01:20 AM
you dont need to set a link identifier if you are using the same host information for the script, such as..
$db1 = mysql_connect("host1"...);
$db2 = mysql_connect("host2"...);
for this same reason, you dont need
mysql_query(query,linkID)
you also had way to man mysql_error() and other problems.
the main problem is your SQL queries, they arent formed correctly.