/    Sign up×
Community /Pin to ProfileBookmark

issue fetching values from mysql plz help

Hi Developers.
i’m new to php and trying to build up my first log-in form,
the database has already a table with few rows filled in, what i would like to do is to pass a variable into the WHERE clause so that it could be matched with the data stored into the database’s table, if so…throw out whatever “in this case the user’s page, but as soon as i press the submit it tells me that there is no row with that name while instead there is! “Unknown column ‘root’ in ‘where clause'”;
can you take a look to the code?
logging into mysql and prevalidations are ok, the variable ‘$conn’ it represent the connection.
// code

if(isset($_POST[‘submit’])){

if(! get_magic_quotes_gpc()){
$user= addslashes($_POST[‘user’]);
$pass=addslashes($_
POST[‘pass’]);
}
$user= $_POST[‘user’];
$pass= $_
POST[‘pass’];

$sql= “SELECT age,mail,sub_date FROM tableusers WHERE username= $user AND pass= SHA1($pass)”;

mysql_select_db(‘MEMBERS’);

$fetch= mysql_query($sql,$conn);

if(! $fetch){
die(“could not get data from mysql:”. mysql_error());
}

while($row= mysql_fetch_array($fetch, MYSQL_ASSOC)){

echo “your age is: {$row[‘age’]}<br />, your mail is: {$row[‘mail’]}<br />,and the submission date is{$row[‘sub_date’]},”;

}
}
?>

<div id=”logholder”>
<table width=”450px” border=”0″ cellspacing=”1″ cellpadding=”2″>

<form method=”POST” action= “<?php PHP_SELF ?>”>

<tr>
<td width=”100″><p class=”logs”>Enter your username: </p></td>
<td><input type=”text” name=”user” id=”user”></td></tr>
<tr>
<td width=”100px”><p class=”logs”>Enter the password</p></td>
<td><input type=”password” name=”pass” id=”pass”></td>
</tr>
<tr>
<td width=”200″></td>
<td><input type=”submit” name=”submit” id=”submit” value=”log in”></td>
</tr>

</table>
</form>
</div>

</body>
</html>

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@criterion9Feb 15.2009 — Hi Developers.

$sql= "SELECT age,mail,sub_date FROM tableusers WHERE username= $user AND pass= SHA1($pass)";
[/QUOTE]


In PHP when declaring variables with inline variables you will need to open and close your quotes to concatenate:
[code=php]
$sql = "SELECT age, mail, sub_date FROM tableusers WHERE username=".$user." AND pass=".SHA1($pass).";";
[/code]


Try that and see if it works for you. You were asking MySQL to search for "$user" etc instead of the value of $user.
Copy linkTweet thisAlerts:
@chazzyFeb 15.2009 — you need to put single quotes around variables going to mysql, unless you use mysqli and prepared statements. it's also customary to drop the ; at the end of a query, since the client app puts it there for you.

[code=php]
$sql = "SELECT age, mail, sub_date FROM tableusers WHERE username='".$user."' AND pass='".SHA1($pass)."'";
[/code]
Copy linkTweet thisAlerts:
@jsfreakauthorFeb 16.2009 — hi guys, thx for the help but both advices gives wrong results.

// code

$sql = "SELECT age, mail, sub_date FROM tableusers WHERE username='".$user."' AND pass='".SHA1($pass)."'";

//eofcode

this just returns the same log in page without any data output and....

//code

$sql = "SELECT age, mail, sub_date FROM tableusers WHERE username=".$user." AND pass=".SHA1($pass).";";

//eofcode

gives me the error: Unknown column 'root' in 'where clause'
Copy linkTweet thisAlerts:
@toenailsinFeb 16.2009 — how bout this?

[CODE]$sql = "SELECT age, mail, sub_date FROM tableusers WHERE username='".$user."' AND pass=[COLOR="Red"]SHA1('".$pass."')[/COLOR]";[/CODE]
Copy linkTweet thisAlerts:
@martswiteFeb 17.2009 — Try this
[code=php]$sql ="SELECT age, mail, sub_date FROM tableusers WHERE username='$user' AND pass='SHA1($pass)'"; [/code]
Copy linkTweet thisAlerts:
@criterion9Feb 17.2009 — Is it possible that the issue is the data in the MySQL database? Are you sure you have data in the fields you are pulling?
Copy linkTweet thisAlerts:
@martswiteFeb 17.2009 — Is it possible that the issue is the data in the MySQL database? Are you sure you have data in the fields you are pulling? [/QUOTE]

Also check that the column names in your sql query correspond exactly to the column names in the mysql table.
Copy linkTweet thisAlerts:
@jsfreakauthorFeb 17.2009 — still nothing, ok, i've double checked and i do have all the right columns names in phpmyadmin here is the whole code i'm using:
[code=php]
<body>

<?php

if(isset($_POST['submit'])){

if(! get_magic_quotes_gpc()){
$user= addslashes($_POST['user']);
$pass=addslashes($_POST['pass']);
}
$user= $_POST['user'];
$pass= $_POST['pass'];

$host= "localhost:8888";
$user= "root";
$pass= "root";

$conn= mysql_connect($host,$user,$pass);

if(! $conn){
die("could not connect to mysql: ". mysql_error());
}

$sql = "SELECT age, mail, sub_date FROM tableusers WHERE username='".$user."' AND pass=SHA1('".$pass."')";

mysql_select_db('MEMBERS');

$fetch= mysql_query($sql,$conn);

if(! $fetch){
die("could not get data from mysql:". mysql_error());
}

while($row= mysql_fetch_array($fetch, MYSQL_ASSOC)){

echo "your age is: {$row['age']}<br />, your mail is: {$row['mail']}<br />,and the submission date is{$row['sub_date']},";

}
}
?>

<div id="logholder">
<table width="450px" border="0" cellspacing="1" cellpadding="2">

<form method="POST" action= "<?php PHP_SELF ?>">

<tr>
<td width="100"><p class="logs">Enter your username: </p></td>
<td><input type="text" name="user" id="user"></td></tr>
<tr>
<td width="100px"><p class="logs">Enter the password</p></td>
<td><input type="password" name="pass" id="pass"></td>
</tr>
<tr>
<td width="200"></td>
<td><input type="submit" name="submit" id="submit" value="log in"></td>
</tr>
</table>
</form>
</div>

</body>
</html>
[/code]

and this is the code i'm using to enter "successfully" the data from another page
[code=php]
$sql= "INSERT INTO tableusers(username,age,mail,pass,sub_date)".
"VALUES".
"('$username','$age','$mail',SHA1('$pass'),NOW())";

[/code]

if i use exactly the same names to insert the datas it goeds ok, if i wanna fetch tem from the database, it does not work, i cant understand.......

i've tried all of the options you guys have adviced over there, it just gives me back the same form with empty field like if the page would have just been refreshed.

i'm about to freak
Copy linkTweet thisAlerts:
@roondogFeb 17.2009 — try
[code=php]
mysql_select_db('MEMBERS', $conn);

$sql = "SELECT age, mail, sub_date FROM tableusers WHERE username='".$user."' AND pass=SHA1('".$pass."')";

$fetch= mysql_query($sql);
[/code]



instead of

[code=php]
$sql = "SELECT age, mail, sub_date FROM tableusers WHERE username='".$user."' AND pass=SHA1('".$pass."')";

mysql_select_db('MEMBERS');

$fetch= mysql_query($sql,$conn);
[/code]
×

Success!

Help @jsfreak spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...