Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.
Moderator: General Moderators
taubi19
Forum Newbie
Posts: 7 Joined: Wed Mar 03, 2010 9:53 am
Location: Oplotnica, Slovenia
Post
by taubi19 » Wed Mar 03, 2010 9:59 am
Hello!
I am very new to sql and php so I have many problems. I could solve most of them alone but this one is giving me a headacke. This is what my browser says: Resource id #4 and I don't know how to solve this.
Please help!
This is my code:
Code: Select all
<?php
$host="localhost";
$username="username";
$password="password";
$db_name="content";
$table_name="home";
//SELECT DATABASE
mysql_connect("$host","$username","$password") or die ("cannot connect");
mysql_select_db("$db_name") or die ("cannot select table");
echo "selected and conected";
if(isset($_POST['elm1'])){
$input = $_POST['elm1'];
echo $input;
}
$sql="INSERT INTO $table_name (content) VALUES ($input)";
mysql_query($sql);
$output = " SELECT * FROM $table_name;";
$result = mysql_query($output);
echo $result;
?>
Thank you for your help!
Weirdan
Moderator
Posts: 5978 Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine
Post
by Weirdan » Wed Mar 03, 2010 10:42 am
mysql_query() returns a resource, a kind of identifier for a fetched recordset. To get actual data from the recordset you need to use mysql_fetch_row()/mysql_fetch_assoc()/etc on it.
taubi19
Forum Newbie
Posts: 7 Joined: Wed Mar 03, 2010 9:53 am
Location: Oplotnica, Slovenia
Post
by taubi19 » Wed Mar 03, 2010 10:59 am
so this means that first i have to use the mysql_query() function and then the mysql_fetch_row() on the result of the first function?
Weirdan
Moderator
Posts: 5978 Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine
Post
by Weirdan » Wed Mar 03, 2010 11:12 am
taubi19 wrote: so this means that first i have to use the mysql_query() function and then the mysql_fetch_row() on the result of the first function?
exactly
taubi19
Forum Newbie
Posts: 7 Joined: Wed Mar 03, 2010 9:53 am
Location: Oplotnica, Slovenia
Post
by taubi19 » Wed Mar 03, 2010 11:15 am
so i didi this:
Code: Select all
$sql="INSERT INTO $table_name (content) VALUES $input";
$rez = mysql_query($sql);
$neki=mysql_fetch_assoc($sql);
$output = " SELECT * FROM $table_name";
$result = mysql_query($output) or die(mysql_error());
mysql_fetch_row($result);
echo $result;
And it still doesn't work, it says:
mysql_fetch_assoc() expects parameter 1 to be resource, string given in D:\wamp\www\dbtext.php on line 23
Resource id #4
Weirdan
Moderator
Posts: 5978 Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine
Post
by Weirdan » Wed Mar 03, 2010 12:12 pm
$neki=mysql_fetch_assoc($sql);
Just strip out that line and you should be fine. First, you shouldn't call mysql_fetch_* functions on string ($sql is a string, initialized 2 lines above that code). Second, data modifying statements (like INSERT, UPDATE, REPLACE, etc) do not return a resultset. They return boolean value that reflects the status of the statement: true if statement succeeded and false if it failed.
taubi19
Forum Newbie
Posts: 7 Joined: Wed Mar 03, 2010 9:53 am
Location: Oplotnica, Slovenia
Post
by taubi19 » Wed Mar 03, 2010 12:25 pm
Thank you very much, how could i been so stupid :S
Is anything wrong if the browser says resource id #4?
Thanks again!
taubi19
Forum Newbie
Posts: 7 Joined: Wed Mar 03, 2010 9:53 am
Location: Oplotnica, Slovenia
Post
by taubi19 » Wed Mar 03, 2010 12:30 pm
OK i got rid of the error, but i still can't see what has been inserted in the db. any idea?
Code: Select all
$sql="INSERT INTO $table_name (content) VALUES $input";
$rez = mysql_query($sql);
$output = " SELECT * FROM $table_name";
$result = mysql_query($output) or die(mysql_error());
$r=mysql_fetch_row($result);
echo $r;
Weirdan
Moderator
Posts: 5978 Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine
Post
by Weirdan » Wed Mar 03, 2010 12:42 pm
$r is an array. You don't echo arrays, you either iterate over them or print_r/var_dump them (for debugging):
Code: Select all
$output = " SELECT * FROM $table_name";
$result = mysql_query($output) or die(mysql_error());
while ($r=mysql_fetch_row($result)) {
print_r($r);
}
taubi19
Forum Newbie
Posts: 7 Joined: Wed Mar 03, 2010 9:53 am
Location: Oplotnica, Slovenia
Post
by taubi19 » Wed Mar 03, 2010 12:53 pm
damn still doesn't work, it says bool(false) :S maybe the problem lies somewhere else.. well I am very grateful for your answers!
edit: now i made it! tnx!