You are here: Re: beginners problem « PHP SQL « IT news, forums, messages
Re: beginners problem

Posted by J.O. Aho on 02/03/07 18:58

Juegen Koenig wrote:
> Sorry, I am completely new to the subject and it begins very frustrating.
> I took an example almost word for word from a book and it doesn't work. I
> have no idea where to look next.
>
> What ist wrong with the following file:
>
> <?
> $host="192.168.0.11";
> $user="root";
> $pass="passwort";
> $database="Lehrerverzeichnis";
> $dz=mysql_connect($host,$user,$pass);
> mysql_select_db($database,$dz);
> ?>
> <html>
> <head></head>
> <body>
> <?
> $sql=mysql_query("SELECT * from TelefonEmail");
> while ($ds=mysql_fetch_object($sql)){
> $name=$ds -> name;
> $vorname=$ds -> vorname;
> echo "$name $vorname<br>";
> }
> mysql_close($dz)
> ?>
> </body>
>
> I myself am sitting at machine 192.168.0.200. I can open the database and
> the table using phpmyadmin with those parameters without any problem. I get
> a lost connection. at line 6.
>
> I have for some reason set $host to "localhost" and it works to my surprise
> to line 14, where it says "supplied argument is not valid"

As your database Lehrerverzeichnis don't have a table called TelefonEmail, the
value from mysql_query() that is returned is false and flase isn't a resource
number and therefore mysql_fetch_object() gets a wrong argument.

Instead do:

if($sql=mysql_query("SELECT * from TelefonEmail")) {
while ($ds=mysql_fetch_object($sql)){
echo "{$ds->name} {$ds->vorname}<br>\n";
}
} else {
echo "Can't query TelefonEmail: ".mysql_error()."<br>\n";
}

Sadly there are many examples that don't care of error checking, you should
always check for errors if you work with databases.


> So what is correct, when I am working at 192.168.0.200 and the server ist
> 192.168.0.11. What should hosts be? And if it is really 'localhost', what I
> do not believe, what does the error mean?

If the script is running on 192.168.0.200, then you must allow connections on
the server at 192.168.0.11 (use the GRANT for this).
If you run the script at 192.168.0.200 and use localhost, then it tries to
connect to a server at 192.168.0.200.

If the script is running at 192.168.0.11, then you should use localhost as the
address (this is default address from where root can login). Try to loose the
spaces around '->', even if it's possible to have spaces almost everywhere in
PHP, it makes the code a bit more difficult to follow, as you can easily
misread what the code is trying to do.


--

//Aho

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация