 |
 |
PHP Question
|
 |
|
 |
|
Senior User
Join Date: Oct 2000
Location: Earth
Status:
Offline
|
|
Hi everyone.
I just try to get into PHP and I am stuck, I don't know why this doesn't work.
I do know HTML and we did some Java and SQL at school, so I am not a complete beginner, but still, I need your help.
This script is just supposed to connect to the MySQL server and to read out all the data of the table sprachen, where Garten is in the column German. I don't get what's wrong.
The error_log of Apache tells me that the argument in mysql_num_rows was not a valid MySQL result resource.
I have attached the script. If you can't help me, do you know good PHP forums that don't scorn beginners? Would be helpful.
<html>
<body>
<?PHP
$db = mysql_connect() or exit("Keine Verbindung hergestellt!");
mysql_select_db("testdb",$db);
$tab = "sprachen";
$sqlbef = "SELECT * FROM " . $tab . " WHERE German = Garten";
$sqlerg = mysql_query($sqlbef, $db);
$anz = mysql_num_rows($sqlerg);
$feldliste = mysql_list_fields("testdb", $tab, $db);
$feldanz = mysql_num_fields($feldliste);
mysql_close($db);
echo "<table border>";
echo "<tr>";
for ($h=0; $h<$feldanz; $h=$h+1)
{
$f = mysql_field_name($feldliste, $h);
$f = strtoupper($f);
echo "<td>$f";
}
echo "</tr>";
for ($i=0; $i<$anz; $i=$i+1)
{
echo "<tr>";
for ($j=0; $j<$feldanz; $j=$j+1)
{
$f = mysql_field_name($feldliste, $j);
$v = mysql_result($sqlerg, $i, $f);
echo "<td>$v</td>";
}
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
Thanks,
Steve
|
|
|
| |
|
|
|
 |
|
 |
|
Grizzled Veteran
Join Date: Nov 2003
Location: Hebburn, UK
Status:
Offline
|
|
As far as I remember, mysql_num_rows doesn't work for SELECT's. It's also not intended to if you read the SQL spec. Whilst a bit annoying, it's easy to get round: (please note that I might be talking crap, but I think I'm right)
Code:
while($ar = mysql_fetch_row($sqlerg)) {
echo "<tr>";
for ($i=0; $i < count($ar); $i++) {
echo "<td>" . $ar[$i] . "</td>";
}
echo "</tr>";
}
I think that'll work. I'm not really on the ball at the moment, so apologies if it doesn't...
|
|
Just who are Britain? What do they? Who is them? And why?
Formerly Black Book
|
| |
|
|
|
 |
|
 |
|
Occasionally Useful
Join Date: Jun 2001
Location: Liverpool, UK
Status:
Offline
|
|
Originally posted by Black Book:
please note that I might be talking crap, but I think I'm right
that, right there, is one awesome quote 
|
|
"Have sharp knives. Be creative. Cook to music" ~ maxelson
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Oct 2000
Location: Earth
Status:
Offline
|
|
Well, nice try, apparently. Somehow, it still doesn't run. In fact, apache gives the same errors with this code. As follows:
[Sun Aug 1 23:23:13 2004] [error] PHP Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /Users/ingmar/Sites/test.php on line 9
[Sun Aug 1 23:23:13 2004] [error] PHP Warning: mysql_list_fields(): Unable to save MySQL query result in /Users/ingmar/Sites/test.php on line 17
[Sun Aug 1 23:23:13 2004] [error] PHP Warning: mysql_num_fields(): supplied argument is not a valid MySQL result resource in /Users/ingmar/Sites/test.php on line 18
So, I guess the problem is in the SQL code itself. Although I don't know what's wrong there.
Steve
P.S. An awesome quote indeed. 
|
|
|
| |
|
|
|
 |
|
 |
|
Grizzled Veteran
Join Date: Nov 2003
Location: Hebburn, UK
Status:
Offline
|
|
Put
Code:
$sqlerg = mysql_query($sqlbef, $db) or die(mysql_error());
to see if the mysql_error can shed more light.
p.s. at least part of my first post was awesome 
|
|
Just who are Britain? What do they? Who is them? And why?
Formerly Black Book
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Aug 2000
Location: Minneapolis, MN
Status:
Offline
|
|
$sqlbef = "SELECT * FROM " . $tab . " WHERE German = Garten";
$sqlerg = mysql_query($sqlbef, $db);
$anz = mysql_num_rows($sqlerg);
$feldliste = mysql_list_fields("testdb", $tab, $db);
$feldanz = mysql_num_fields($feldliste);
mysql_close($db);
I would recommend enclosing 'Garten' with single quotes. Depending on your usage it may be best to leave the connection open throughout a user's session, rather than opening and closing it for each request. There is overhead in initiating and closing mysql connections.
|
|
|
| |
|
|
|
 |
|
 |
|
Grizzled Veteran
Join Date: Nov 2003
Location: Hebburn, UK
Status:
Offline
|
|
You know, I never even noticed that. That's an obvious error, and one that mysql_error will bring up. D'oh!
|
|
Just who are Britain? What do they? Who is them? And why?
Formerly Black Book
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Oct 2000
Location: Earth
Status:
Offline
|
|
Hi everyone.
Thanks for your great help, I am nearly done with my scripting now. One big question remains: Is it possible to pass on variables? I have a php script and there are some variables that I need in another one - how do I do that? Is it possible at all?
Thanks in advance!
Steve
|
|
|
| |
|
|
|
 |
|
 |
|
Grizzled Veteran
Join Date: Nov 2003
Location: Hebburn, UK
Status:
Offline
|
|
|
|
|
Just who are Britain? What do they? Who is them? And why?
Formerly Black Book
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

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