chris93 Posted April 23, 2014 Share Posted April 23, 2014 im trying to connect to database with mysql . when i run the code im getting Parse error: syntax error, unexpected end of file in C:\xampp2\htdocs\tutorials\ab1.php on line 22 <?php$username = 'root';$pass = 'fireman9';$db = 'testdb';$link = mysql_connect('localhost',$username,$pass);mysql_select_db("testdb",$con);if(!link){echo "Not Connected to DB" . mysqli_connect_error();$result = mysql_query($link, "SELECT * FROM test");while($row = mysql_fetch_array($result)){echo "ID: " . $row['id'];echo "Name: " . $row['name33'];}?> Quote Link to comment https://forums.phpfreaks.com/topic/287959-connecting-to-database-with-mysql/ Share on other sites More sharing options...
QuickOldCar Posted April 23, 2014 Share Posted April 23, 2014 Missing closing curly brace, also made it die() if(!link){ die ("Not Connected to DB" . mysqli_connect_error()); } Quote Link to comment https://forums.phpfreaks.com/topic/287959-connecting-to-database-with-mysql/#findComment-1477035 Share on other sites More sharing options...
QuickOldCar Posted April 23, 2014 Share Posted April 23, 2014 Since you are new and learning, you should use all mysqli_* versus mysql_* functions. http://www.php.net/mysqli_connect Quote Link to comment https://forums.phpfreaks.com/topic/287959-connecting-to-database-with-mysql/#findComment-1477038 Share on other sites More sharing options...
IanA Posted April 23, 2014 Share Posted April 23, 2014 Agreed, using the original php mysql extension in the OP's example leads you exposed to SQL injections. The new mysqli extension the QuickOldCar has stated for you provide a way of using prepared statements/parameterized statements and thus removes the risk of SQL injection. PDO is also another avenue you could look down Quote Link to comment https://forums.phpfreaks.com/topic/287959-connecting-to-database-with-mysql/#findComment-1477047 Share on other sites More sharing options...
cyberRobot Posted April 23, 2014 Share Posted April 23, 2014 Just to clarify, the real reason to switch to MySQLi or PDO is that the mysql_* functions have been depreciated. More information can be found here: http://www.php.net/manual/en/mysqlinfo.api.choosing.php If you're not quite ready to make the switch, you can prevent SQL injections with mysql_real_escape_string(). http://www.php.net/mysql_real_escape_string Quote Link to comment https://forums.phpfreaks.com/topic/287959-connecting-to-database-with-mysql/#findComment-1477068 Share on other sites More sharing options...
ginerjm Posted April 23, 2014 Share Posted April 23, 2014 Deprecated! Deprecated! Deprecated! (A guru should know the difference between deprecation and depreciation. ) Quote Link to comment https://forums.phpfreaks.com/topic/287959-connecting-to-database-with-mysql/#findComment-1477073 Share on other sites More sharing options...
cyberRobot Posted April 23, 2014 Share Posted April 23, 2014 Ah yes, thanks for the correction. Now how do I rewire my brain. Quote Link to comment https://forums.phpfreaks.com/topic/287959-connecting-to-database-with-mysql/#findComment-1477076 Share on other sites More sharing options...
IanA Posted April 23, 2014 Share Posted April 23, 2014 Thanks for that cyberRobot Quote Link to comment https://forums.phpfreaks.com/topic/287959-connecting-to-database-with-mysql/#findComment-1477080 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.