0
<?php

mysql_connect("localhost", "root", "usbw");
mysql_select_db("europese unie");

$id = $_GET['Land'];
$query = "SELECT `Land`, `Oppervlakte`, `Inwoners`, `Klimaat`, `Motto`, `UTC` FROM `algemene informatie` WHERE `Land` = '$id'";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
$land = $row[0];

echo "$land"; 



print("<h2>$id</h2>");
print("<br />");


die(mysql_error())

?>

when i tried to run this code i expected to echo the first land that came up with this query instead i got nothing. i am not a very experienced PHP or Mysql user nor is english my first language so pleas do not hold annything against me.

Kaminokaze
  • 13
  • 2
  • Whats the output of `var_dump($row)`? – Rahil Wazir Apr 04 '14 at 15:44
  • have you tried echo $land; – csharpwinphonexaml Apr 04 '14 at 15:46
  • This is a dangerous query using a $_GET variable directly in a query. Read up on SQL injections and look into using PDO with prepared statements. – Devon Bessemer Apr 04 '14 at 15:46
  • You are vulnerable to [SQL injection attacks](http://bobby-tables.com) – Marc B Apr 04 '14 at 15:47
  • Sidenote: Slap a semi-colon at the end of `die(mysql_error())` – Funk Forty Niner Apr 04 '14 at 15:51
  • 1
    First try to get anything from database with query : `SELECT `Land`, `Oppervlakte`, `Inwoners`, `Klimaat`, `Motto`, `UTC` FROM `algemene informatie` WHERE 1` . Maybe you don't have a row with THIS particular `$id` . Besides I dont know if you can name your table with space, this might be an error also. And third of all, you should call fields like this: ``algemene informatie`.`Land`` – Sebastian Piskorski Apr 04 '14 at 15:51
  • var_dump($row) gives bool(false) – Kaminokaze Apr 04 '14 at 15:52
  • Spaces in a database could cause some unexpected results `mysql_select_db("europese unie");` – Funk Forty Niner Apr 04 '14 at 15:53
  • `$result = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_row($result); print_r($row);` – csharpwinphonexaml Apr 04 '14 at 15:57
  • @SebastianPiskorski after i used that query i got the first row of the table. and i know there should be a row with that $id because i got the $id from the same table. also thank you for that about the space i will look into it. – Kaminokaze Apr 04 '14 at 16:00
  • @Fred-ii- made it into europese-unie – Kaminokaze Apr 04 '14 at 16:02
  • @user3498790 Ah, ok. Just double-checking. – Funk Forty Niner Apr 04 '14 at 16:02
  • I tested your code and it worked fine. You could try removing the quotes in `'$id'"` to `$id"` but it may not even work any better. Make sure you have data in the given columns, with no spelling mistakes. @user3498790 Plus, remember to add a semi-colon at the end of `die(mysql_error())` should you have any other code below it. That alone will break your code, IF you have more code below that; just saying. – Funk Forty Niner Apr 04 '14 at 16:07
  • You could also try changing `$land = $row[0];` to `$land = $row[1];` @user3498790 if there is no data in your first column, just to test it out. – Funk Forty Niner Apr 04 '14 at 16:10
  • @Fred-ii- well the adress is _http://localhost:8080/details.php?Land=%20Belgie_ and `$id = $_GET['Land'];`is the id so Belgie should be $id but as `var_dump($result)` it keeps giving `bool(false)` – Kaminokaze Apr 04 '14 at 16:23
  • Use some tool like Navicat Premium or SQL console or phpmyadmin to be certain that THIS id in THIS table exists. Your query looks ok. Maybe `$_GET['Land']` is empty. Did you'd check it? – Sebastian Piskorski Apr 04 '14 at 16:25
  • @user3498790 Does typing in `...details?Land=Belgie` without the space give you a result? That `%20` is a space btw. That could be the problem. You could make use of `trim()` – Funk Forty Niner Apr 04 '14 at 16:26
  • Basically what you're asking SQL, is to find `(space)Belgie` and since your column doesn't contain the word Belgie with a space, it will not find it; least that's what I think is happening @user3498790 – Funk Forty Niner Apr 04 '14 at 16:32
  • @Fred-ii- how should i do this? – Kaminokaze Apr 04 '14 at 16:34
  • @SebastianPiskorski i'm using phpmyadmin because im doing this with usbwebserver as a assignment. i'm sorry for not providing this information earlier. – Kaminokaze Apr 04 '14 at 16:36
  • Try using `$id = trim($_GET['Land']);` @user3498790 – Funk Forty Niner Apr 04 '14 at 16:36
  • 1
    @Fred-ii-thank you `print_r($row);` now gives Array ( [0] => Belgie [1] => 31 [2] => 11 [3] => gematigd zeeklimaat [4] => Eendracht maakt macht [5] => 1 ) i can finaly proceed – Kaminokaze Apr 04 '14 at 16:39

3 Answers3

2

Solved

Taken from comments:

"well the adress is localhost:8080/details.php?Land=%20Belgie and $id = $_GET['Land']; is the id so Belgie should be $id but as var_dump($result) it keeps giving bool(false)"

  • Basically what you're asking SQL, is to find (space)Belgie and since your column doesn't contain the word Belgie with a space, it will not find it; least that's what I think is happening

That %20 is a space btw. That could be the problem. You could make use of the trim() function.

Use:

$id = trim($_GET['Land']);

OP:

thank you print_r($row); now gives Array ( [0] => Belgie [1] => 31 [2] => 11 [3] => gematigd zeeklimaat [4] => Eendracht maakt macht [5] => 1 ) i can finaly proceed


Sidenote: Your present code is open to SQL injection. Use mysqli_* functions. (which I recommend you use and with prepared statements, or PDO)

mysql_* functions are deprecated and will be removed from future PHP releases.


Footnotes:

mysql_* functions deprecation notice:

http://www.php.net/manual/en/intro.mysql.php

This extension is deprecated as of PHP 5.5.0, and is not recommended for writing new code as it will be removed in the future. Instead, either the mysqli or PDO_MySQL extension should be used. See also the MySQL API Overview for further help while choosing a MySQL API.

These functions allow you to access MySQL database servers. More information about MySQL can be found at » http://www.mysql.com/.

Documentation for MySQL can be found at » http://dev.mysql.com/doc/.

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
1

Do not use mysql_connect() this is going to become outdated, use mysqli_connect().

I.e.:

$connection = new mysqli('host', 'username', 'password', 'db table', 'portnumber');

    if (mysqli_connect_errno()){
        printf("<b>Connection failed:</b> %s\n", mysqli_connect_error());
        exit;
    } 

run you query through mysqli object and remember to use injection prevention or you will get people hacking you site.

e.g.

http://www.phphaven.com/article.php?id=65

Hope that helps

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • when i do this i get this error **Class 'mysqli_connect' not found in G:\... on line 20** – Kaminokaze Apr 04 '14 at 16:15
  • That's because you're already using `mysql_*` functions. This answer doesn't qualify as an answer, per se. @user3498790 if you haven't changed all your functions for `mysqli` API, then don't use this. – Funk Forty Niner Apr 04 '14 at 16:16
  • If you are using a local LAMP stack then you need to install this hopefully you can just enable this in your php.ini i.e. ;extension=mysqli.so change to extension=mysqli.so – Matthew Thomas Apr 04 '14 at 16:17
  • 1
    Fred's comment is valid although if you enable strict PHP programming you will get a tone of warnings, learn PHP right and use recommended up to date functions and objects!!! – Matthew Thomas Apr 04 '14 at 16:20
0

Quote out the variables.

echo $land;

print("<h2>" . $id . "</h2>");
patapizza
  • 2,398
  • 15
  • 14