• PHP - a newb needs help
    16 replies, posted
I'm teaching myself PHP, just in case it comes in handy some day. I'm hosting my attempts from my own Win7Pro x32 computer, using Abyss X1 2.6, MySQL Essential 5.1.48, and PHP 5.21. I'm also using MySQL Workbench 5.2.25 to edit my database, and I'm writing my scripts in Notepad++. Anyway, I've been struggling constantly. At the moment, I'm stuck on this problem: I want to use PHP to generate a list of links from my database. The database tables look like this (irrelevant data omitted): [code] test.rooms: // "doors" is an ENUM, containing "doorID" numbers +--------+--------+ | roomID | doors | +--------+--------+ | 0 | '0,'2' | | 1 | '1' | | 2 | '' | +--------+--------+ test.doors: // "from" and "to" are "roomID" numbers +--------+------+----+------------------------+ | doorID | from | to | desc | +--------+------+----+------------------------+ | 0 | 0 | 1 | Read some instructions | | 1 | 1 | 0 | Return to welcome page | | 2 | 0 | 2 | Enter a castle | +--------+------+----+------------------------+ [/code] What I need to do is to list the "doors" (links) on each room's page. My PHP looks like this (again, omitting irrelevant code): [php] <?php $doorsList=mysql_query("SELECT doors FROM rooms WHERE roomID=$roomID"); $i=0; $doors=array(); while ($i <= $doorsList['COUNT(doors)']) { $doors[$i]=mysql_query("SELECT * FROM doors WHERE doorID=$doorsList[$i]"); $i++; } ?> <p><?php $i=0; while ($i <= count($doors)) { echo '<a href="http://www.google.com/' . $i . '">' . $i . $doors[$i]["desc"] . '</a><br />'; $i++; }; ?></p> [/php] That last while should be outputting stuff like "<a href="http://www.google.com/1">Read some instructions</a><br />", however, it seems that $doors[$i]["desc"] gives me nothing at all. Can anyone spot what I'm doing wrong? Chances are it's something really basic, since this is my first PHP script.
Assuming that is your entire code you haven't actually connected to your mySQL server. So you're submitting mySQL queries and your script has absolutely zero idea what database it's supposed to be querying. Look up [url=http://php.net/manual/en/function.mysql-query.php]mysql_connect[/url] and [URL=http://php.net/manual/en/function.mysql-select-db.php]mysql_select_db[/URL] Also, for security purposes, read up on [URL=http://php.net/manual/en/function.mysql-close.php]mysql_close[/URL] which is a function for closing the connection to the mySQL server once you've made your query.
[QUOTE=Cluckyx;23483733]Also, [B]for security purposes[/B], read up on [URL=http://php.net/manual/en/function.mysql-close.php]mysql_close[/URL] which is a function for closing the connection to the mySQL server once you've made your query.[/QUOTE] What?
[QUOTE=Cluckyx;23483733]Assuming that is your entire code you haven't actually connected to your mySQL server. So you're submitting mySQL queries and your script has absolutely zero idea what database it's supposed to be querying. Look up [url=http://php.net/manual/en/function.mysql-query.php]mysql_connect[/url] and [URL=http://php.net/manual/en/function.mysql-select-db.php]mysql_select_db[/URL] Also, for security purposes, read up on [URL=http://php.net/manual/en/function.mysql-close.php]mysql_close[/URL] which is a function for closing the connection to the mySQL server once you've made your query.[/QUOTE] Uh, no, as I said, that isn't the entire code, only the parts related to my problem. I already use mysql_close();, but I didn't include that section of code since that's not an issue. Nor is the logging in, etc.. I /can/ connect to the database just fine. I already have the page showing room descriptions from the database, for instance. But again, no problem there, so I didn't post that part, to reduce clutter.
"while ($i <= $doorsList['COUNT(doors)']) {" huh?
Bah. That's what I get for posting at 2AM. my brain is full of cotton wool. My bad
I'd advise you to use [url=http://phpro.org/tutorials/Introduction-to-PHP-PDO.html]PDO and parameterized/prepared statements[/url], it's good practice.
[QUOTE=Darkimmortal;23484419]What?[/QUOTE] And yes I know the main reason it's best practise to close connections isto stop shitting itself over too many users. But something always feels wrong about leaving persistent connections open regardless of situation even if it's just leaving it to time out. EDIT: mah automerge :saddowns:
I still have no idea what I'm doing wrong... I tried print_r($doors);, but it just returns "Array ( [0] => )", which I would assume means that $doors is empty? Meaning the first section of the code is what's messed up? I'm considering just dropping MySQL and trying SQLite. Does anyone think that's a good idea? To be able to say that I made /some/ sort of progress, I've whipped up an ultraminimalistic, CSS-tastic theme for my site. Completely frame-free, strictly XHTML 1.0 compliant, and using less than 9kb of images. Chances are that anything that doesn't follow web standards will utterly fail to render it (*coughIEcough*), but that's a sacrifice I'm willing to make. You can see it [url=http://97.104.88.48:8080/room.php]here[/url]. Please note that I have a lot to do, still... Most importantly, I need to make the damn doors work properly... But I guess you could critique my bizarre theme, or comment on whichever room I happen to be testing at the moment.
[QUOTE=Cluckyx;23485254]Bah. That's what I get for posting at 2AM. my brain is full of cotton wool. My bad[/QUOTE] It's generally not a good idea to post when you aren't in the best mental state.
[QUOTE=turb_;23490553]It's generally not a good idea to post when you aren't in the best mental state.[/QUOTE] That's some good advice turb_ :v:
[QUOTE=Cluckyx;23485309]And yes I know the main reason it's best practise to close connections isto stop shitting itself over too many users. But something always feels wrong about leaving persistent connections open regardless of situation even if it's just leaving it to time out. EDIT: mah automerge :saddowns:[/QUOTE] Open connections are automatically closed at the end of PHP execution, I believe (unless persistent, of course).
[QUOTE=DEADBEEF;23485275]I'd advise you to use [url=http://phpro.org/tutorials/Introduction-to-PHP-PDO.html]PDO and parameterized/prepared statements[/url], it's good practice.[/QUOTE] Or use MySQLi with prepared statements. [url]http://www.php.net/manual/en/mysqli.query.php[/url]
I'd really suggest using MySQLi, a lot easier.
[QUOTE=DEADBEEF;23485275]I'd advise you to use [url=http://phpro.org/tutorials/Introduction-to-PHP-PDO.html]PDO and parameterized/prepared statements[/url], it's good practice.[/QUOTE] Jesus christ, has this made my life easier! Thank you so much. I'm /finally/ starting to make some progress with PHP 5.3 and SQLite 3.
[QUOTE=turb_;23490553]It's generally not a good idea to post when you aren't in the best mental state.[/QUOTE] :irony: :v:
Doors are working! Hell yes! You can take a look [url=http://97.104.88.48:8080/room.php]here[/url]. Each room has its own door(s) (which are a link to another room, with a customizable description), which get listed after the room's description. Next up, exciting stuff: Making the form that'll allow people to make new doors and rooms.
Sorry, you need to Log In to post a reply to this thread.