-1

I have no idea what's going on. I usually have simple sign in pages like this done very quickly but this one isn't working and I cannot spot the error.

<?php
$con=mysql_connect("db_server","db_user","db_pass","db");
if (!$con)
{
   echo "Failed to connect to MySQL: " . mysql_error();
}
$username = $_GET['username'];
$password = $_GET['password'];

$query="SELECT username FROM users ";
//$query.="WHERE `username`=".$username;
//$query.=" AND `password`=".$password;
echo "query=".$query."<br/>";
$result = mysql_query($query, $con);
echo "result=".$result."<br/>";
if($result){
    $row = mysql_fetch_assoc($result);
    $data = $row['username'];
    echo "data=".$data;
}else{
    echo "something went wrong:".mysql_error();
}
mysql_close($con);
?>

im using mysql_* instead of mysqli_* as the server im running it on is 5.2; not sure if that is relevant but I was getting an unrecognized function error originally.

There is only one entry in the database. As I said, I use the regular SQL code through phpmyadmin and i get the results i need.

Also not sure if relevant. I'm echoing $result and nothing comes out. Isnt it supposed to echo "false"?

Jess Stone
  • 677
  • 8
  • 21
ebichuhamster
  • 228
  • 1
  • 12
  • Everything is ok until $result – ebichuhamster Jan 31 '14 at 04:22
  • It always comes out false. – ebichuhamster Jan 31 '14 at 04:22
  • Does running the query directly work? – Nikola Jan 31 '14 at 04:23
  • 1
    Essential reading - [Why shouldn't I use mysql_* functions in PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – Phil Jan 31 '14 at 04:24
  • So it just says *"something went wrong"*? How about you do some minimal debugging and change that to `echo 'something went wrong: ', mysql_error();`? – Phil Jan 31 '14 at 04:26
  • Yes, the $query echo i copy/paste it into phpmyadmin SQL tab and it works. – ebichuhamster Jan 31 '14 at 04:26
  • mysql_error() returns nothing – ebichuhamster Jan 31 '14 at 04:27
  • 1
    @ebichuhamster in that case, I don't believe the code you've posted above is your **actual** code. Also, you should probably `exit` or better yet, throw an exception if you can't connect to the database – Phil Jan 31 '14 at 04:28
  • i have one post code and one get code. i just updated it. – ebichuhamster Jan 31 '14 at 04:30
  • 1
    Both PDO and MySQLi have been around since version 5.1. There is **no reason at all** to continue using the deprecated MySQL extension – Phil Jan 31 '14 at 04:34
  • The simple problem here is that the fourth argument to `mysql_connect` is a boolean `new_link` yet you are passing the string `db`. You have simply forgotten to [select a database](http://php.net/manual/function.mysql-select-db.php) – Phil Jan 31 '14 at 04:39

2 Answers2

3

You have a major error in your logic, for one. If there is an error connecting to MySQL, you print the error, but yet proceed to query the broken connection - you are also not selecting a database.

Also, this approach is for PHP4. Unless you are stuck in PHP4 on this project, moving into the PHP5 world would be a good idea.

I recommend looking into PDO: http://www.php.net/manual/en/book.pdo.php

As for not getting errors, check your error_reporting and display_errors settings in your .ini

Fraz0r
  • 396
  • 2
  • 6
-2

Try this one.

<?php
$con=mysql_connect("db_server","db_user","db_pass");
mysql_select_db("db");

if (!$con)
{
   echo "Failed to connect to MySQL: " . mysql_error();
}
$username = $_GET['username'];
$password = $_GET['password'];

$query=mysql_query("SELECT username FROM users");
if($query){
    $row = mysql_fetch_assoc($query);
    $data = $row['username'];
    echo $data;
}else{
    echo "something went wrong:".mysql_error();
}
mysql_close($con);
?>
  • This is no different from the OP's post – Fraz0r Jan 31 '14 at 04:38
  • I don't get it now mysql_error() threw out "no database selected" – ebichuhamster Jan 31 '14 at 04:39
  • 1
    Use `mysql_select_db('dbname');` I highly suggest reading my answer, though – Fraz0r Jan 31 '14 at 04:42
  • It is very wrong. You didn't modify the OP's logic at all – Fraz0r Jan 31 '14 at 04:46
  • 2
    You obviously haven't read my answer. Your code is far from correct, and contains a very major logical error: using a connection which may be broken - you're only printing an error, but still query it either way. Also, if he's just now learning, it would be wise to move into PHP5. It was released around 10 years ago... – Fraz0r Jan 31 '14 at 04:53
  • @RizwanShamsherKaimKhani i clicked the upvote because this helped me solve the problem. I also upvoted Fraz0r's answer because he is correct. It's true that I'm still learning PHP and I didn't know PHP5 was 10 years old. I don't know what else to say except sorry for making everyone waste their time, this error was very mundane. – ebichuhamster Jan 31 '14 at 22:44
  • You did not "waste" anyone's time, @ebichuhamster. It is what this site is for. "There is no such thing as a stupid question." Remember that ;) – Fraz0r Feb 21 '14 at 21:00
  • Also remember that everyone started from a similar position as you. Just keep practicing! :] – Fraz0r Feb 21 '14 at 21:03