...

View Full Version : First post -- help a noob, please.



tefflox
10-30-2007, 01:30 PM
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

$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 (http://programmermeetdesigner.com/listing/view/1961)...

abduraooft
10-30-2007, 01:42 PM
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?

tefflox
10-30-2007, 02:00 PM
$SQL = " SELECT * FROM grav WHERE category = '$category' ";

but it throws the same error. actually, i don't know what 'category' refers to.

abduraooft
10-30-2007, 02:37 PM
To use the above query, you must have a field named 'category' in the table named 'grav'. Check your table structure.

tefflox
10-30-2007, 02:45 PM
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
$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");
}
}
?>

Fumigator
10-30-2007, 05:01 PM
Quit using mysql_db_query(); it is deprecated.

Use mysql_query() (http://us2.php.net/manual/en/function.mysql-query.php) instead.

tefflox
10-30-2007, 05:12 PM
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
$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");
}
}
?>

Fumigator
10-30-2007, 05:32 PM
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).

matak
10-30-2007, 05:45 PM
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...

BWiz
10-30-2007, 05:46 PM
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:


$query = "SELECT name FROM posts";


Example script:


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

aedrin
10-30-2007, 06:20 PM
Try using this to access your rows.



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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum