Author Topic: whats wrong with this?  (Read 2089 times)

0 Members and 1 Guest are viewing this topic.

Offline -Felix-Topic starter

  • Enthusiast
  • Posts: 119
    • View Profile
whats wrong with this?
« on: October 06, 2005, 09:13:09 AM »
Code: [Select]
mysql_connect ("localhost", "login", "password") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("login_database");

$username=$_POST['user'];
echo $_POST['user'];

$result=mysql_query("SELECT * FROM Profiles WHERE Email = $username");

   $row=mysql_fetch_assoc($result);
$SQLEmail=$row['Email'];
$SQLPassword=$row['Password'];

I get an error
Quote
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/account/public_html/account.php on line 23

Offline obsidian

  • Managed Insanity
  • Staff Alumni
  • Freak!
  • *
  • Posts: 6,440
  • Gender: Male
  • Talk to me, I won't bite... hard.
    • View Profile
    • Guahan Web
whats wrong with this?
« Reply #1 on: October 06, 2005, 09:18:01 AM »
Quote
Code: [Select]
mysql_connect ("localhost", "login", "password") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("login_database");

$username=$_POST['user'];
echo $_POST['user'];

$result=mysql_query("SELECT * FROM Profiles WHERE Email = $username");

   $row=mysql_fetch_assoc($result);
$SQLEmail=$row['Email'];
$SQLPassword=$row['Password'];

I get an error
[snapback]303428[/snapback]

most likely you're getting the error since you don't have quotes around $username in your query:
[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] * FROM Profiles WHERE Email = '$username';
[!--sql2--][/div][!--sql3--]
You can't win, you can't lose, you can't break even... you can't even get out of the game.

Code: [Select]
<?php
while (count($life->getQuestions()) > 0)
{   
$life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

Offline -Felix-Topic starter

  • Enthusiast
  • Posts: 119
    • View Profile
whats wrong with this?
« Reply #2 on: October 06, 2005, 09:22:08 AM »
nope, doesnt work either.

Somehow Im guessing that there is something wrong with the "Email" Im using as an index? In the database admin I have set Email-field as a Primary field, but it is not an index field. Could this be causing the problem?

Offline obsidian

  • Managed Insanity
  • Staff Alumni
  • Freak!
  • *
  • Posts: 6,440
  • Gender: Male
  • Talk to me, I won't bite... hard.
    • View Profile
    • Guahan Web
whats wrong with this?
« Reply #3 on: October 06, 2005, 09:37:35 AM »
Quote
nope, doesnt work either.

Somehow Im guessing that there is something wrong with the "Email" Im using as an index? In the database admin I have set Email-field as a Primary field, but it is not an index field. Could this be causing the problem?
[snapback]303435[/snapback]

don't know... just run an "or die(mysql_error())" right after your query:
[!--PHP-Head--][div class=\'phptop\']PHP[/div][div class=\'phpmain\'][!--PHP-EHead--]
[span style=\"color:#0000BB\"]<?php
$result[/span][span style=\"color:#007700\"]=[/span][span style=\"color:#0000BB\"]mysql_query[/span][span style=\"color:#007700\"]([/span][span style=\"color:#DD0000\"]\"SELECT * FROM Profiles WHERE `Email` = \'$username\'\"[/span][span style=\"color:#007700\"]) or die([/span][span style=\"color:#0000BB\"]mysql_error[/span][span style=\"color:#007700\"]());
[/span][span style=\"color:#0000BB\"]?>
[/span]
[/span][!--PHP-Foot--][/div][!--PHP-EFoot--]

this will kill the script and give you the error if it's the query that's causing the problem.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

Code: [Select]
<?php
while (count($life->getQuestions()) > 0)
{   
$life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

Offline -Felix-Topic starter

  • Enthusiast
  • Posts: 119
    • View Profile
whats wrong with this?
« Reply #4 on: October 06, 2005, 09:50:30 AM »
ok, it reports
Quote
Unknown column 'abcd' in 'where clause'

abcd is the username Im using.

But I dont get this...I have only one profile in this database because Im testing, I have made sure many times that there really is the correct username, in the correct field. ???

Offline obsidian

  • Managed Insanity
  • Staff Alumni
  • Freak!
  • *
  • Posts: 6,440
  • Gender: Male
  • Talk to me, I won't bite... hard.
    • View Profile
    • Guahan Web
whats wrong with this?
« Reply #5 on: October 06, 2005, 09:57:56 AM »
Quote
ok, it reports

abcd is the username Im using.

But I dont get this...I have only one profile in this database because Im testing, I have made sure many times that there really is the correct username, in the correct field. ???
[snapback]303447[/snapback]

that's because of the quotes. you've got to have strings offset by quotes in a where clause. look carefully at my last post. i offset the column name with tics, and i offset the $username with single quotes. that will get rid of the "unknown column" error
You can't win, you can't lose, you can't break even... you can't even get out of the game.

Code: [Select]
<?php
while (count($life->getQuestions()) > 0)
{   
$life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx