Jump to content

MySQL Syntax Error


logicopinion

Recommended Posts

hello... there is a code beloew.. which i created... and on my local mashin it works and does not make any error.. but as soon as i upload it to server it give me error.. i do not understant why...

 

This is error:

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

 

and this is code of that page:

 

<?php
$login_username =  stripslashes($_POST['login_username']);
$login_password =  stripslashes($_POST['login_password']);

$oneday = 60 * 60 * 24 + time();
setcookie("firstschool_username", "$login_username", 
$oneday);
setcookie("firstschool_password", "$login_password", $oneday);
include ("includes/vars.php");
mysql_connect("$hostname",  "$username",  "$password") or die(mysql_error());
mysql_select_db("$database") or die(mysql_error());
$query = "SELECT * FROM $login";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{

if ($login_username == $row[username] && $login_password == $row[password] )
{
echo "<meta http-equiv=\"REFRESH\" content=\"0;url=index.php\">";
}
else
{

echo "<meta http-equiv=\"REFRESH\" content=\"0; url=login.php\">";

exit;
}


}
?>

 

 

also here is the code of vars.php file which is included in this page ...

 


<?php

$hostname="localhost";
$username="schoge_admin";
$password="mypassword";
$database="schoge_firstschool";
$database2="forum";
$news="mynews";
$bulletin="mybulletin";
$navigation="menu";
$login="login";

?>

 

 

Thanks for your attention

 

Link to comment
https://forums.phpfreaks.com/topic/90733-mysql-syntax-error/
Share on other sites

Looking at your code, it appears you are selecting every record and looping through them to check the id and pw.  This seems like a waste of resources.  What you want to do is do a SELECT statement but only return the record that matches.

 

$query = "SELECT * FROM `login` WHERE `username` = '$login_username' AND `password` = '$login_password'";
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) > 0) {
echo "Logged in, redirecting";
echo "<meta http-equiv=\"REFRESH\" content=\"3;url=index.php\">";
}
else {
echo "Wrong username or password";
echo "<meta http-equiv=\"REFRESH\" content=\"3; url=login.php\">";
}

 

....
while($row = mysql_fetch_array($result))
{

if ($login_username == $row[username] && $login_password == $row[password] )
{
echo "$login";
}
.....

 

 

yes on my local mashine it works and print just  a word login

 

 

but when on the server ... it prints nothing just an empty white page..

selecting statement but returning the record that matches solved the problem !!!

 

NOT IT WORKS.

 

but i wonder why?

 


<?php
$login_username =  stripslashes($_POST['login_username']);
$login_password =  stripslashes($_POST['login_password']);

$oneday = 60 * 60 * 24 + time();
setcookie('firstschool_username', '$login_username', $oneday);
setcookie('firstschool_password', '$login_password', $oneday);
include ("includes/vars.php");
mysql_connect("$hostname",  "$username",  "$password") or die(mysql_error());
mysql_select_db("$database") or die(mysql_error());
$query = "SELECT * FROM `login` WHERE `username` = '$login_username' AND `password` = '$login_password'";
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) > 0) {
echo "Logged in, redirecting";
echo "<meta http-equiv=\"REFRESH\" content=\"3;url=index.php\">";
}
else {
echo "Wrong username or password";
echo "<meta http-equiv=\"REFRESH\" content=\"3; url=login.php\">";
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.