Enjoy an ad free experience by logging in. Not a member yet?
Register .
10-30-2007, 12:30 PM
PM User |
#1
New to the CF scene
Join Date: Oct 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
First post -- help a noob, please.
hello, first post to the forums (my birthday here : )
so, I've established a database for the first time, on my own. I'm very excited to have done that. However, there's a lot to learn. The following script gives an error
Unknown column 'category' in 'where clause'. This is all new to me. I've worked with php, but never with databases, only for rendering html.
PHP Code:
<?php
$username = "root" ;
$password = "-------" ;
$database = "grav" ;
$host = "localhost" ;
# connect to database
$cid = mysql_connect ( $host , $username , $password );
if (! $cid ) { echo( "ERROR: " . mysql_error () . "\n" ); }
?>
<html>
<head>
<title>Display Posts</title>
</head>
<body>
<?php
$category = "Posts" ;
# setup SQL statement
$SQL = " SELECT * FROM grav " ;
# execute SQL statement
$retid = mysql_db_query ( $database , $SQL , $cid );
# check for errors
if (! $retid ) { echo( mysql_error ()); }
else {
# display results
echo ( "<h3><b>$category</b></h3>\n" );
while ( $row = mysql_fetch_array ( $retid )) {
$post = $row [ "post" ];
echo ( $post . "<br/>\n" );
}
}
?>
</body>
</html>
I also welcome you to read
this entry at pmd ...
10-30-2007, 12:42 PM
PM User |
#2
Supreme Master coder!
Join Date: Mar 2007
Location: N/A
Posts: 14,689
Thanks: 158
Thanked 2,184 Times in 2,171 Posts
This is a usual error which comes when we try to access a field-name not in the specified table.
but your query is $SQL = " SELECT * FROM grav "; , no reference to a field named category.
Do you have any other query there?
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
10-30-2007, 01:00 PM
PM User |
#3
New to the CF scene
Join Date: Oct 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Code:
$SQL = " SELECT * FROM grav WHERE category = '$category' ";
but it throws the same error. actually, i don't know what 'category' refers to.
10-30-2007, 01:37 PM
PM User |
#4
Supreme Master coder!
Join Date: Mar 2007
Location: N/A
Posts: 14,689
Thanks: 158
Thanked 2,184 Times in 2,171 Posts
To use the above query, you must have a field named 'category' in the table named 'grav'. Check your table structure.
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
10-30-2007, 01:45 PM
PM User |
#5
New to the CF scene
Join Date: Oct 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
the db is 'grav', the table is 'posts' the fields are the int key and 'post' field.
all this does is echo the h3 element. thank for your patience.
PHP Code:
<?php
$category = "post" ;
# setup SQL statement
$SQL = " SELECT * FROM posts WHERE post = '$category' " ;
# execute SQL statement
$retid = mysql_db_query ( $database , $SQL , $cid );
# check for errors
if (! $retid ) { echo( mysql_error ()); }
else {
# display results
echo ( "<h3><b>$category</b></h3>\n" );
while ( $row = mysql_fetch_array ( $retid )) {
$post = $row [ "post" ];
echo ( $post . "<br/>\n" );
}
}
?>
10-30-2007, 04:01 PM
PM User |
#6
UE Antagonizer
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,687
Thanks: 42
Thanked 637 Times in 625 Posts
Quit using mysql_db_query(); it is deprecated.
Use
mysql_query() instead.
10-30-2007, 04:12 PM
PM User |
#7
New to the CF scene
Join Date: Oct 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
I got the following code to work; however, changing to mysql_query() throws this error:
Warning: Wrong parameter count for mysql_query(). Also, I am befuddled why
$post = $row[$i++]; does not work.
PHP Code:
<?php
$category = "post" ;
# setup SQL statement
$SQL = " SELECT post FROM posts" ;
# execute SQL statement
$retid = mysql_db_query ( $database , $SQL , $cid );
# check for errors
if (! $retid ) { echo( mysql_error ()); }
else {
# display results
echo ( "<h3><b>Posts</b></h3>\n" );
$i = 0 ;
while ( $row = mysql_fetch_array ( $retid )) {
$post = $row [ $i ];
echo ( "<p>" . $post . "</p>\n" );
}
}
?>
10-30-2007, 04:32 PM
PM User |
#8
UE Antagonizer
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,687
Thanks: 42
Thanked 637 Times in 625 Posts
I did not give you a link to the mysql_query() page in the PHP manual for my own health, I gave you the link so you would click on it and determine which arguments it requires (and how many).
10-30-2007, 04:45 PM
PM User |
#9
Banned
Join Date: Apr 2007
Posts: 428
Thanks: 29
Thanked 5 Times in 5 Posts
Quote:
Originally Posted by
tefflox
PHP Code:
while ( $row = mysql_fetch_array ( $retid )) { //try this echo $row [ 0 ]; // $post = $row[$i]; // echo ("<p>".$post."</p>\n"); } } ?>
you can access your posts with
$row[0]. in your case it would be post table field.
and as for i++, just imagine it is done, and you'll be fine.
if you are selecting only one row then it's easier to use mysql_fetch_row...
10-30-2007, 04:46 PM
PM User |
#10
Regular Coder
Join Date: Mar 2006
Location: Sol System
Posts: 471
Thanks: 7
Thanked 21 Times in 21 Posts
1) You need to establish a connection with the server
2) You need to select which database you're going to be using
3) Then, you need to construct a query
example:
if the TABLE name is "posts" and there is a field in it called
"name", then the query to select the names of all the people
who posted would be:
PHP Code:
$query = "SELECT name FROM posts" ;
Example script:
PHP Code:
<?php $host = "localhost" ; $user = "bwhite" ; $pass = "password" ; $db_name = "cwaf" ; $connection = @ mysql_connect ( $host , $user , $pass ) or die ( mysql_error () ); $db_connect = @ mysql_select_db ( $db_name ) or die ( mysql_error () ); $query = "SELECT name FROM posts" ; $result = mysql_query ( $query ); //Now do what you would with the received data (should it exist). ?>
__________________
BWiz :: Happy Coding!
2006 2007 2008 2009 2010 2011
Irrational numbers make no sense.
10-30-2007, 05:20 PM
PM User |
#11
Senior Coder
Join Date: Jan 2007
Posts: 1,648
Thanks: 1
Thanked 58 Times in 54 Posts
Try using this to access your rows.
PHP Code:
$result = mysql_query ( $query );
while ( $row = mysql_fetch_assoc ( $result )) {
echo $row [ 'post' ];
}
Notice the use of mysql_fetch_assoc(). And instead of using $row[0], etc. you can use the actual name of the column.
This improves code readability and maintainability.
Jump To Top of Thread
Thread Tools
Rate This Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT +1. The time now is 01:30 PM .
Advertisement
Log in to turn off these ads.