For last two days I have been trying to get my head round the basics of using my_sql with php. I found a lot of desent tutorals on making logins. Althought I did the following tutorial http://www.youtube.com/watch?v=2hCXGA0gvdQ My code: //index.php /////////////// <?php error_reporting(E_ALL | E_STRICT); ini_set('display_errors', True); session_start(); ?> <html> <head> <title>My login</title> </head> <body> <div></div> <?php if (isset($_SESSION['username'])) { ?> You are now logged in <a href="logout.php">Logout</a> <?php } else { ?> <form action="login.php" method="post"> username: <input name="username" type="text" /> password: <input name="password" type="password" /> <input type="submit" /> </form> <?php } ?> <!-- Output Error --> <?php if (in_array('error',$_SESSION)) echo $_SESSION['error']; unset($_SESSION['error']); ?> </body> </html> /////////////// //login.php /////////////// <?php session_start(); $db_host = 'localhost'; $db_user = 'root'; $db_pass = ''; $db_db = 'users'; if (isset($_POST['username'])) { // Mysql Connection $db_link = mysql_connect($db_host, $db_user, $db_pass) or die('MySQl Connection Error:'.mysql_error()); mysql_select_db($db_db,$db_link) or die('MySQL Error: Cannot select table'); $username = mysql_real_escape_string($_POST['username']); $password = mysql_real_escape_string($_POST['password']); // MySQL Query $result = mysql_query("SELECT * FROM users WHERE username = '$username' AND password = '$password' "); if(!$result) { $_SESSION['error'] = '<span style="color: red">Login Failed</span>'; } else { // Mysql fetch row results $row = mysql_fetch_assoc($result); $_SESSION['userid'] = $row['id']; $_SESSION['username'] = $username; $_SESSION['error'] = 'Login successful<br> Welcome, '.$username; } mysql_close($db_link); } header('Location: ./') ?> /////////////// //logout.php /////////////// <?php session_start(); if (isset($_GET['logout'])) { $_SESSION = array(); if ($_COOKIE[session_name()]) { setcookie(session_name(), '', time()-42000, '/'); } session_destroy(); header('Location: ./'); } ?> //////////////// Code (markup): I even got his code, which seems to work on his computer but not mine. I keep getting the following error: "MySQL Error: Cannot select table". This is created by the die statement because obviously am not connecting to my sql table. As you can see in the picture below I have one set up. I just dont understand why when I run this code it cant find the table I have created. I cant get my head round it and I was wondering if anyone could help. I done everything he said in last 2 turorials. I am using apache server on my computer "http://localhost/test/index.php", is the address. I have got other php scripts working on it fine. The only problem I seem to be having is connecting to tables. I did the following code to, but did not set up a table or database and it does not work either. <?php $c = mysql_connect(localhost,root, ""); $c = mysql_select_db(user.$c); for($i=1; $i<=50;$i++) { $sql = "SELECT * FROM 'users' WHERE 'username' = $i"; $res = mysql_query($sql) or die(mysql_error()); $row = mysql_fetch_assoc($res); echo "Row $row[id] has the username of $row[username]<br>\n"; } ?> Code (markup): I dont understand why I cant connect to my sql tables. Any help on explaining this would be really appreciated!!! Thanks in advance! P.S sorry for any spelling mistakes, good old dyslexia.
Pleae try following code: $c = mysql_connect('localhost','root', ''); mysql_select_db('users'); PHP: Please check that this code works correct. After this try to make mysql_query()
read this article on Connecting to MySql database using PHP : http://freaksgfx.blogspot.com/2007/10/how-to-connect-database-i-will-not.html this might help you understanding some functions in PHP..
modify this code $sql = "SELECT * FROM 'users' WHERE 'username' = $i"; to: $sql = "SELECT * FROM `users` WHERE `username` = $i";
Thanks that worked but now I get 'No database selected' Would I just make a database called users, with no table now? or what would I call it. database `user` and table `users`... Thanks for replies!
Oh, sorry you must use mysql_select_db('mysite'); PHP: Becasue 'mysite' this is database and 'users' this is just a table.
I did that on the other I put '$db_db = 'mysite'; and mysql_select_db($db_db) below. When I do this it allows me to add any username e.g ssdfs, pass esfdsu. and works says you are loged in. The username in the database is "bob" and pass is "123", and when u log in its suppose to say you have sussesfully loged in! Welcome 'bob', you are loged in, 'log out' and if you input wrong one its suppose to go: 'Login Failed' in red below the log in thing. I think its because I am not connecting to database correctly... any ideas? I coped his code correctly and I understand a lot of his code. I dont think its his code as it works on his tutorial and I downloaded his code, changed it to mysite, and it does the same. I dont know whats going on? Any ideas?