logicopinion Posted February 12, 2008 Share Posted February 12, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/90733-mysql-syntax-error/ Share on other sites More sharing options...
revraz Posted February 12, 2008 Share Posted February 12, 2008 echo $query and see what it looks like. Quote Link to comment https://forums.phpfreaks.com/topic/90733-mysql-syntax-error/#findComment-465073 Share on other sites More sharing options...
logicopinion Posted February 12, 2008 Author Share Posted February 12, 2008 sorry i am realy newbie in MySQL and .. how to echo quary? what should i do? Quote Link to comment https://forums.phpfreaks.com/topic/90733-mysql-syntax-error/#findComment-465074 Share on other sites More sharing options...
marcus Posted February 12, 2008 Share Posted February 12, 2008 echo $query; Try doing something like: $query = "SELECT * FROM `". $login . "`"; Quote Link to comment https://forums.phpfreaks.com/topic/90733-mysql-syntax-error/#findComment-465076 Share on other sites More sharing options...
logicopinion Posted February 12, 2008 Author Share Posted February 12, 2008 oops wait .. mistaken.. wait a moment Quote Link to comment https://forums.phpfreaks.com/topic/90733-mysql-syntax-error/#findComment-465077 Share on other sites More sharing options...
marcus Posted February 12, 2008 Share Posted February 12, 2008 You're changing the wrong query, silly. Quote Link to comment https://forums.phpfreaks.com/topic/90733-mysql-syntax-error/#findComment-465078 Share on other sites More sharing options...
revraz Posted February 12, 2008 Share Posted February 12, 2008 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\">"; } Quote Link to comment https://forums.phpfreaks.com/topic/90733-mysql-syntax-error/#findComment-465080 Share on other sites More sharing options...
logicopinion Posted February 12, 2008 Author Share Posted February 12, 2008 yes ... no it says incorrect table name Quote Link to comment https://forums.phpfreaks.com/topic/90733-mysql-syntax-error/#findComment-465083 Share on other sites More sharing options...
revraz Posted February 12, 2008 Share Posted February 12, 2008 Then `login` is not your tablename? Quote Link to comment https://forums.phpfreaks.com/topic/90733-mysql-syntax-error/#findComment-465084 Share on other sites More sharing options...
logicopinion Posted February 12, 2008 Author Share Posted February 12, 2008 $login="login" this is in my vars.php file so the tablename is login. but even now .. when i check this code on my machine it works fine.. but when putting it online on the server.. it goes wrong... Quote Link to comment https://forums.phpfreaks.com/topic/90733-mysql-syntax-error/#findComment-465088 Share on other sites More sharing options...
marcus Posted February 12, 2008 Share Posted February 12, 2008 Does the table even exist. Quote Link to comment https://forums.phpfreaks.com/topic/90733-mysql-syntax-error/#findComment-465089 Share on other sites More sharing options...
haku Posted February 12, 2008 Share Posted February 12, 2008 Then the table name on your local machine is "login", but on your server it either has a different name, or the table doesn't exist. Have you set up the database on your server? Quote Link to comment https://forums.phpfreaks.com/topic/90733-mysql-syntax-error/#findComment-465090 Share on other sites More sharing options...
logicopinion Posted February 12, 2008 Author Share Posted February 12, 2008 Incorrect table name '' there something wrong withi this sign ( " ) can`t find the reason spent all day but unsuccssesfully Quote Link to comment https://forums.phpfreaks.com/topic/90733-mysql-syntax-error/#findComment-465099 Share on other sites More sharing options...
revraz Posted February 12, 2008 Share Posted February 12, 2008 That means there is nothing in that variable, it's empty. Substitute the actual table name like I did in the example above. Quote Link to comment https://forums.phpfreaks.com/topic/90733-mysql-syntax-error/#findComment-465102 Share on other sites More sharing options...
marcus Posted February 12, 2008 Share Posted February 12, 2008 No, it means there's no value within the two quotes. Therefore, login isn't defined. Quote Link to comment https://forums.phpfreaks.com/topic/90733-mysql-syntax-error/#findComment-465104 Share on other sites More sharing options...
logicopinion Posted February 12, 2008 Author Share Posted February 12, 2008 but then why does it works when i do something like mmmm what if i just echo $login if it will print table name it means that value is defined for $login right? Quote Link to comment https://forums.phpfreaks.com/topic/90733-mysql-syntax-error/#findComment-465110 Share on other sites More sharing options...
marcus Posted February 12, 2008 Share Posted February 12, 2008 Sure, try echoing $login and see if it really is defined. Quote Link to comment https://forums.phpfreaks.com/topic/90733-mysql-syntax-error/#findComment-465113 Share on other sites More sharing options...
logicopinion Posted February 12, 2008 Author Share Posted February 12, 2008 .... 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.. Quote Link to comment https://forums.phpfreaks.com/topic/90733-mysql-syntax-error/#findComment-465123 Share on other sites More sharing options...
marcus Posted February 12, 2008 Share Posted February 12, 2008 Then login isn't defined, just replace $login with login in your query Quote Link to comment https://forums.phpfreaks.com/topic/90733-mysql-syntax-error/#findComment-465126 Share on other sites More sharing options...
logicopinion Posted February 12, 2008 Author Share Posted February 12, 2008 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\">"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/90733-mysql-syntax-error/#findComment-465128 Share on other sites More sharing options...
revraz Posted February 12, 2008 Share Posted February 12, 2008 Check your include file, maybe there is a problem. Quote Link to comment https://forums.phpfreaks.com/topic/90733-mysql-syntax-error/#findComment-465129 Share on other sites More sharing options...
logicopinion Posted February 12, 2008 Author Share Posted February 12, 2008 its done man ! thank you Quote Link to comment https://forums.phpfreaks.com/topic/90733-mysql-syntax-error/#findComment-465132 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.