emarket Posted September 9, 2009 Share Posted September 9, 2009 Hi, I'm not a coder so I need a little help with this code. What I baisicly want to do is to grab the last entry from a column from a table of a database and if that last entry is 1 that go to link1 and if not go to second link. This code doesn't give me error but is just redirecting me to the first link. Thanks, <?php $db_host=""; $db_name=""; $username=""; $password=""; $db_con=mysql_connect($db_host,$username,$password); $connection_string=mysql_select_db($db_name); // Connection mysql_connect($db_host,$username,$password); mysql_select_db($db_name); $sql = "SELECT id FROM table1 ORDER BY id DESC LIMIT 0,1"; if ($sql=1) { header('Location: http://www.exemple.com/first.php'); } else { header('Location: http://www.exemple.com/second.php'); } ?> Link to comment https://forums.phpfreaks.com/topic/173643-need-little-help/ Share on other sites More sharing options...
trq Posted September 9, 2009 Share Posted September 9, 2009 Given your example $sql is a string. You need to actully execute that query using mysql_query. Something like.... $sql = "SELECT id FROM table1 ORDER BY id DESC LIMIT 0,1"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { $row = mysql_fetch_assoc($result); if ($row['id'] == 1) { header('Location: http://www.exemple.com/first.php'); } else { header('Location: http://www.exemple.com/second.php'); } } } Link to comment https://forums.phpfreaks.com/topic/173643-need-little-help/#findComment-915299 Share on other sites More sharing options...
emarket Posted September 9, 2009 Author Share Posted September 9, 2009 Thanks for you answer. I get this error: Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in... on line 16 so I think this part of my code is not good SELECT id FROM table1 ORDER BY id DESC LIMIT 0,1 where "id" is my column Link to comment https://forums.phpfreaks.com/topic/173643-need-little-help/#findComment-915325 Share on other sites More sharing options...
trq Posted September 9, 2009 Share Posted September 9, 2009 Post your actual code. Link to comment https://forums.phpfreaks.com/topic/173643-need-little-help/#findComment-915328 Share on other sites More sharing options...
emarket Posted September 9, 2009 Author Share Posted September 9, 2009 <?php $db_host=""; $db_name=""; $username=""; $password=""; $db_con=mysql_connect($db_host,$username,$password); $connection_string=mysql_select_db($db_name); // Connection mysql_connect($db_host,$username,$password); mysql_select_db($db_name); $sql = "SELECT id FROM ap_form_4 ORDER BY id DESC LIMIT 0,1; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { $row = mysql_fetch_assoc($result); if ($row['id']==1); { header('Location: http://www.exemple.com/first.php'); } else { header('Location: http://www.exemple.com/second.php'); } } } ?> Link to comment https://forums.phpfreaks.com/topic/173643-need-little-help/#findComment-915335 Share on other sites More sharing options...
trq Posted September 9, 2009 Share Posted September 9, 2009 Notice how your code changes color after the $sql line? You are missing the closing " You should get yourself a decent editor, these simple mistakes are easily picked up with syntax highlighting on. Link to comment https://forums.phpfreaks.com/topic/173643-need-little-help/#findComment-915342 Share on other sites More sharing options...
emarket Posted September 9, 2009 Author Share Posted September 9, 2009 You should get yourself a decent editor, these simple mistakes are easily picked up with syntax highlighting on. You are right. I'm still working with the old Notepad. Now I get another error: Parse error: syntax error, unexpected T_ELSE in.. on line 18 <?php $db_host=""; $db_name=""; $username=""; $password=""; $db_con=mysql_connect($db_host,$username,$password); $connection_string=mysql_select_db($db_name); // Connection mysql_connect($db_host,$username,$password); mysql_select_db($db_name); $sql = "SELECT id FROM ap_form_4 ORDER BY id DESC LIMIT 0,1"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { $row = mysql_fetch_assoc($result); if ($row['id']==1); { header('Location: http://www.exemple.com/first.php'); } else { header('Location: http://www.exemple.com/second.php'); } } } ?> Link to comment https://forums.phpfreaks.com/topic/173643-need-little-help/#findComment-915521 Share on other sites More sharing options...
TeNDoLLA Posted September 9, 2009 Share Posted September 9, 2009 See this line, <?php if ($row['id']==1); { The if condition is not supposed to have semicolon ";" after it, remove it. Link to comment https://forums.phpfreaks.com/topic/173643-need-little-help/#findComment-915539 Share on other sites More sharing options...
emarket Posted September 9, 2009 Author Share Posted September 9, 2009 Thanks. Now it works, but it goes just to the second link even if the value=1 Link to comment https://forums.phpfreaks.com/topic/173643-need-little-help/#findComment-915555 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.