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

$db = mysql_connect("localhost","login","password");

mysql_select_db("database_name",$db);

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

$db = mysql_connect("localhost","login","password");

mysql_select_db("database_name",$db);

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

<?
$n = mysql_num_rows($result);
while($i < $n){
echo mysql_result($result,$i,'name');
echo mysql_result($result,$i,'msg');
echo mysql_result($result,$i,'date');
echo mysql_result($result,$i,'time');
$i++;
}

?>


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


<?php
if(isset($name)) {

echo "<html>";
echo "<head> <h1> PHP Guestbook </h1> </head>";
echo "<title> PHP/MySQL Guestbook </title>";
echo "<body bgcolor=#FFFFFF>";


$db = mysql_connect("localhost","n5micone","n5micone");
mysql_error();
mysql_select_db("n5micone",$db);

$into = mysql_query("INSERT INTO php_guestbook
(name,msg,date,time) VALUES ($name,$msg,CURRENT_DATE,CURRENT_TIME)",$db);
mysql_error();
mysql_query($into);
mysql_error();
$result = mysql_query("SELECT * FROM php_guestbook",$db);

$n = mysql_num_rows($result);
while($i < $n){
echo mysql_result($result,$i,'name');
echo mysql_result($result,$i,'msg');
echo mysql_result($result,$i,'date');
echo mysql_result($result,$i,'time');
$i++;
}


echo "</body></html>";
exit;
} else {
?>

<html>
<head>Guestbook Form</head>
<title> PHP guestbook</title>
<body bgcolor=#FFFFFF>
<hr>

<form method="POST" action="<? echo "$PHP_SELF";?>" >
Name: <input type="text" name="name"><p>
Message: <input type="text" name="msg"><p>
<input type="submit" value="post">
</form>
<hr>
</body>
</html>

<? } ?>


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.

this below, works..

<?php
if(isset($name)) {

echo "<html>";
echo "<head> <h1> PHP Guestbook </h1> </head>";
echo "<title> PHP/MySQL Guestbook </title>";
echo "<body bgcolor=#FFFFFF>";


mysql_connect("localhost","username","password");
mysql_select_db("guestbook");

$insert_query = "INSERT INTO php_guestbook (name, msg, date, time) VALUES ('".$name."', '".$msg."', CURRENT_DATE, CURRENT_TIME)";
mysql_query($insert_query);
$select_query = "SELECT * FROM php_guestbook";
$result = mysql_query($select_query);
for($i=0;$i < mysql_num_rows($result);$i++){
echo "Name: ".mysql_result($result,$i,'name')."<br>";
echo "Message: ".mysql_result($result,$i,'msg')."<br>";
echo "Date: ".mysql_result($result,$i,'date')."<br>";
echo "Time: ".mysql_result($result,$i,'time')."<br>";
}

echo "</body></html>";
exit;
} else {
?>

<html>
<head>Guestbook Form</head>
<title> PHP guestbook</title>
<body bgcolor=#FFFFFF>
<hr>
<form method="POST" action="<? echo "$PHP_SELF";?>" >
Name: <input type="text" name="name"><p>
Message: <input type="text" name="msg"><p>
<input type="submit" value="post">
</form>
<hr>
</body>
</html>

<? } ?>


but CURRENT_TIME isnt putting time in, i think its because im using the wrong SQL field type though,

nanode
10-13-2000, 10:14 AM
To prove that things are working, it's much easier to manually create a small data set and run some output queries:


<?php
include('../top.in'); //strictly HTML stuff

$sql = "SELECT * FROM updateitems ORDER BY id DESC";
mysql_pconnect(localhost,root);
$rs = mysql_db_query("websitedb", $sql);


?>

<BR>
<h2>News and Updates</h2>


<?php

printf("<TABLE>");
while ($row = mysql_fetch_object($rs)) {
$id = $row->id;
$title = $row->title;
$date = $row->releasedate;
printf("<TR><TD><a href=\"displayarticle.php?id=%s\">%s</a><TD>%s<br>",$id,$title,$date);


}
printf("</TABLE>");
?>
</center>

</font>
</body>
</html>


You can see how it looks:
http://206.191.148.186/updates/

FYI: my table scheme

mysql> describe updateitems;
+-------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+----------------+
| id | int(11) | | PRI | 0 | auto_increment |
| title | varchar(50) | YES | | NULL | |
| author | varchar(35) | YES | | NULL | |
| body | text | YES | | NULL | |
| releasedate | date | YES | | NULL | |
+-------------+-------------+------+-----+---------+----------------+
5 rows in set (0.01 sec)



hope this helped a little

Sweede
10-13-2000, 12:01 PM
just so you know, using print or echo statements are faster than using printf()

printf uses more memory also, i cant remember where on zend.com i read that, but its up there.

nanode
10-13-2000, 02:56 PM
sweede,

thanks for the echo tip. I haven't observed any performance issues, by I'm dealing w/ small amounts of data. Still is best to make that simple change.

So in place of:

printf("<a href=\"%s\">blah</a>", $filename);

I do:

echo "<a href=\"$filename\">blah</a>";

Just making sure I'm with you. http://www.linuxnewbie.org/ubb/smile.gif

thanks.

Sweede
10-13-2000, 05:37 PM
i usually use printf when im doing something like this...

printf("<bar>%s</bar><foo>%s%s</foo>",$this->object,$this->other_object);

but using printf for displaying pure HTML tags is a waste. [ printf("<html>"); ]