PDA

View Full Version : Reading news from table on server



toplisek
03-23-2006, 04:34 PM
I would like to read all articles in rows.

Do you know why it gives me error
Warning: link(): Permission denied in ...read_article.php on line 55
Line 55 is:


echo '<td><A HREF="'.link("article.php", "ID=".$_GET['id']).'"><B>'.$rec[$i].'</B></A></td>';



code is:


<?

# Param 1 : MySQL Host Name
# Param 2 : MySQL Username
# Param 3 : MySQL Password
# Param 4 : MySQL Database
# Param 5 : SQL Statement (SELECT)

show_table("localhost","...","...","...","SELECT * FROM articles");

function show_table($hostName,$userName,$passWord,$dataBase,$sqlQuery)
{
# Connect to MySQL
$conn=mysql_connect("...", "...", "...");
# Select Database
mysql_select_db($dataBase,$conn);
# Validate SQL Statement
$array=explode(" ORDER",$sqlQuery);
$sqlQuery=$array[0];
if(!strstr($sqlQuery,"SELECT"))
die("Invalid Query : SQL statement should be a SELECT statement.");
# ORDER records by requested column
if($_GET['order'])
$sqlQuery=$sqlQuery." ORDER BY ".$_GET['order'];
# Execute SQL query
$result=mysql_query($sqlQuery) or die("Invalid Query : ".mysql_error());
$row=mysql_fetch_array($result);
# Check whether NULL records found
if(!mysql_num_rows($result))
die("No records found.");

echo "<table border=1><tr>";
# Make the row for table column names
while (list($key, $value) = each($row))
{
$i++;
if(!($i%2))
echo "<td><b><a href='?order=$key'>$key</a></td>";
}
echo "</tr>";
$result=mysql_query($sqlQuery);

// Make rows for records
while($rec=mysql_fetch_array($result))
{
echo "<tr>";
for($i=0;$i<count($rec);$i++)
{
if($rec[$i])
echo '<td><A HREF="'.link("article.php", "ID=".$_GET['id']).'"><B>'.$rec[$i].'</B></A></td>';

}
echo "</tr>";
}
echo "</table>";
}
?>

Twey
03-23-2006, 04:43 PM
Could be that you're using a filesystem that doesn't support hardlinks, or possibly that you don't have write permission in the directory in which you're attempting to create the link. Are you sure this is the function you mean to use?

toplisek
03-24-2006, 07:48 AM
Is there other possibility for link?

I would like to read all articles in rows with all coloumns from server (table).

Each row should have link to e.g. www.mywebsite.com/article?ID=1

Number should change with articles ID number.

Fields for article are
ID
Date
Summary
Content

djr33
03-24-2006, 08:16 AM
Umm... why bother with that? link() just makes a link for you? Is that all it does? I don't care about that.. I'd never bother to memorize it. Just echo a link.


echo '<a href="'.$link.'">linkname</a>';
....And include above however you want.

Can't be that hard, right?

Twey
03-24-2006, 09:51 AM
link() creates a hard link, a special filesystem structure used to transparently point to another existing file. It has nothing to do with HTML hyperlinks, which is why I was dubious as to whether this was the function the OP intended to use.

djr33
03-25-2006, 11:17 AM
Ah.... hmm... seems like he would want a hyperlink.
What exactly is a hardlink, btw?

//dragging the thread off topic... sorry 'bout that.

toplisek
03-26-2006, 07:48 AM
If I put code on line 55 it is error:
Parse error: parse error, unexpected T_STRING, expecting ',' or ';' in .../read_news.php on line 55

Line 55:


echo '<td><a href="'article.php?ID='.$_GET['id'].'"><B>'.$rec[$i].'</B></A></td>';


Each row should have link to e.g. www.mywebsite.com/article?ID=1 in the first row and in second row e.g. www.mywebsite.com/article?ID=2

Number should change with articles ID number (ID for article is in table in first field).

djr33
03-26-2006, 07:54 AM
What the heck? That error is saying you should've ended some line with a ;.... meaning your syntax is off.
BUT.... I don't see a missing semicolon... I've seen that error alot, and its usually quite easy to fix.
Maybe it's saying there's an open loop.
I dunno.

toplisek
03-26-2006, 08:05 AM
I have solved and it works now. Problem was in href=""

echo '<td><a href="article.php?ID='.$_GET['id'].'"><B>'.$rec[$i].'</B></A></td>';

Do you know how to get $_GET['id'] form table on server where is ID first column?

djr33
03-26-2006, 08:47 AM
Ah, cool. Hmm... guess the error was due to that. strange.

toplisek
03-26-2006, 10:13 AM
Do you know how to get $_GET['id'] form table on server where is ID first column?

Do you know how to put code and receive $_GET['id'] ? Id is first column in table articles

djr33
03-26-2006, 10:29 AM
So... you want to get the value of $_GET['id'] and find the article that matches that?

Well... you need to use a mysql query.... try this:


$var = "SELECT * FROM articles WHERE id='".$GET_['id']."'";
mysqlquery($var);

...I think that'll work. The WHERE command is what you need.

I might be slightly off on syntax... hope it helps, though.

toplisek
03-26-2006, 11:05 AM
I have put while sentence :



// Make rows for records
while($rec=mysql_fetch_array($result))
{
echo "<tr>";
for($i=0;$i<count($rec);$i++)
{
if($rec[$i])


$query = "SELECT ID FROM articles where ID = '$rec[$i]' ";
$s = mysql_query($query);
echo(mysql_error($s));

$result = mysql_fetch_assoc($s);
$ID=$result['ID'];
//echo the article
//echo("<p>".$result['ID']."</p>");


echo '<td><a href="article.php?ID='.$ID.'"><B>'.$rec[$i].'</B></A></td>';
}
echo "</tr>";
}
echo "</table>";
}


It gives me error:
Warning: mysql_error(): supplied resource is not a valid MySQL-Link resource in .../read_news.php on line 59

Line 59 is:
echo(mysql_error($s));
Do you know how is correct to get ID number in this while sentence? Need help

djr33
03-26-2006, 11:11 AM
Hey, Twey, or someone else, is the syntax right on the $query, line?
Looks like he's trying to embed a variable in it... inside single quotes, but it's in double quotes overall. that might be the problem, but I'm not sure.

Ok... wait... nevermind. I figured it out.

Here's the deal...

You are using mysql_error... that's a function that is SUPPOSED to show an error. That's not really an error you're getting... but you're just displaying an error there.

Or... maybe you're testing with that.

I'm confused.

toplisek
03-26-2006, 11:16 AM
Do you know how is correct to get ID number in this while sentence? Need help


I do not know how to get Id form each row of news.

Twey
03-26-2006, 11:38 AM
guess the error was due to that. strange.Not at all. Nothing more than what usually happens when you forget to escape a quote.


Hey, Twey, or someone else, is the syntax right on the $query, line?
Looks like he's trying to embed a variable in it... inside single quotes, but it's in double quotes overall. that might be the problem, but I'm not sure.Yes, that bit's fine; the single quotes are just part of the double-quoted string, and have no special meaning. However, there may be other problems... ID is presumably a number, which means it doesn't need to be quoted. You might as well select *, being as you'll want it later, and there's no performance decrease.
$query = 'SELECT * FROM articles where ID = ' . $rec[$i] . ';';More importantly, I just noticed the layout of the code. If we add in the effective braces:
// Make rows for records
while($rec=mysql_fetch_array($result))
{
echo "<tr>";
for($i=0;$i<count($rec);$i++)
{
if($rec[$i])
{
$query = "SELECT ID FROM articles where ID = '$rec[$i]' ";
}
$s = mysql_query($query);
echo(mysql_error($s));

$result = mysql_fetch_assoc($s);
$ID=$result['ID'];
//echo the article
//echo("<p>".$result['ID']."</p>");


echo '<td><a href="article.php?ID='.$ID.'"><B>'.$rec[$i].'</B></A></td>';
}
echo "</tr>";
}
echo "</table>";
} So, if !$rec[$i], you're trying to execute a non-existant query. The fix is to move that brace to where it ought to be, or, better, to use the continue keyword to skip the rest of that iteration:
// Make rows for records
while($rec=mysql_fetch_array($result))
{
echo "<tr>";
for($i=0;$i<count($rec);$i++)
{
if(!$rec[$i]) continue;
$query = "SELECT ID FROM articles where ID = '$rec[$i]' ";
$s = mysql_query($query);
echo(mysql_error($s));

$result = mysql_fetch_assoc($s);
$ID=$result['ID'];

echo '<td><a href="article.php?ID='.$ID.'"><B>'.$rec[$i].'</B></A></td>';
}
echo "</tr>";
}
echo "</table>";
} This is the sort of problem that can be avoided by using a consistent and accurate indentation scheme.

toplisek
03-26-2006, 11:57 AM
It gives me error with your above code:
Warning: mysql_error(): supplied resource is not a valid MySQL-Link resource in .../read_news.php on line 56

Line 56: echo(mysql_error($s));

and it shows only first row. I need to showw all rows which are on server from ID1 to last ID)

djr33
03-26-2006, 10:10 PM
You do realize you're telling it to tell you an error, right?
You're using it for error checking purposes, yes?

All that's gonna do is tell you the last mysql error, so it would be anything in the entire page that resulted as an error... doesn't have anything to do with line56 except that's when you decided to display it.

I'm not saying you're wrong though... just wanted to be sure that is what you wanted.

-------

It would only show the first row. What Twey posted says 'select ID from...', meaning you're only gonna get data from the ID column.

Twey
03-27-2006, 06:41 AM
It ought to show 0: Success, though.

djr33
03-27-2006, 06:54 AM
Hmm... Yeah.

But if there were an error before that point, it would show that, right?

So it doesn't realy prove what the error is from?

Maybe you should be sure the error is right there by doing an error check like that after the last command, but before this one.

Also, use the command die('erroritshouldsay')... that way, when/if the funciton doesn't work, it'll stop working and display your error message, making it quite clear that's exactly where it didn't work.




mysql_query('something') or die('error doing something');

toplisek
03-27-2006, 08:53 AM
I have put


$result = mysql_query($query)
or die("<b>A fatal MySQL error occured</b>.\n<br />Query: " . $query . "<br />\nError: (" . mysql_errno() . ") " . mysql_error());


There is no error shown. so, this works Problem is:
1.it gives me just first row
2.only first column has correct link like:
http://mywebsite/article.php?id=1
It should have all columns above link

I have put echo count($rec)."<br>"; and number is 14

Twey
03-27-2006, 03:13 PM
In this case, I would ask you to give the URI to the page in question.

toplisek
03-27-2006, 07:57 PM
Dear Twey,
I have now tested and tested. I have code and it shows me all rows and columns.
First column (ID for article) shows correct link like: http://www.mywebsite/article.php?ID=1 (for first row) and ID=2 for second row)


Do you have any suggestions how to do that also other columns will have this link?:)

Code is the following for while:


while (list($key, $value) = each($row))
{
$i++;
if(!($i%2))
echo "<td><b><a href='?order=$key'>$key</a></td>";
}
echo "</tr>";
$result=mysql_query($sqlQuery);
// Make rows for records
while($rec=mysql_fetch_array($result))
{
echo "<tr>";
for($i=0;$i<count($rec);$i++)
{
if($rec[$i])/*continue;*/


$query = "SELECT ID FROM news where ID = '$rec[$i]' ";
$s = mysql_query($query);
//echo(mysql_error($s));

$result2 = mysql_fetch_assoc($s);
$ID=$result2['ID'];

echo '<td><a href="article.php?id='.$ID.'"><B>'.$rec[$i].'</B></A></td>';
}
echo "</tr>";
}
echo "</table>";
}