TEDSON Posted September 6, 2010 Share Posted September 6, 2010 I'm really sorry if this has been answered (I assume it has) I'm unfamiliar with search terms of what I am trying to do. I have this web page <html> <body> <?php $con = mysql_connect("localhost","user","pass"); if (!$con) { echo "Fail"; die('Could not connect: ' . mysql_error()); } echo "Triumph"; ?> </body> </html> Which I got from a snippet. I load the webpage expecting to see fail or triumph, but I see nothing. My question is how to run the php script so I see something on the page (hopefully triumph. I apoligise for my noobness, I'm a PHP know nothing at the moment. Quote Link to comment Share on other sites More sharing options...
TEDSON Posted September 6, 2010 Author Share Posted September 6, 2010 Sorry, seems I'm out of time to edit my post. I've found that the page has to have a .php extension, rather than .htm I changed it and I get what I expected.. "FailCould not connect: Access denied for user 'user'@'localhost' (using password: YES)" My username and password are correct. I notice in the snippet code, it dosent aske the database name, could this be the problem? if so how do I define it? Quote Link to comment Share on other sites More sharing options...
amplexus Posted September 6, 2010 Share Posted September 6, 2010 so, you've set a variable $con to be mysql_connect, but you never actually tell it to connect. you also need some of this: mysql_select_db($database) or die( "Unable to select database") Quote Link to comment Share on other sites More sharing options...
Adam Posted September 6, 2010 Share Posted September 6, 2010 so, you've set a variable $con to be mysql_connect, but you never actually tell it to connect. What? What do you think mysql_connect() is for? @TEDSON As amplexus was showing, selecting the database is done after the connection. The problem must be with the host, username or password. Is MySQL definitely running? Quote Link to comment Share on other sites More sharing options...
TEDSON Posted September 6, 2010 Author Share Posted September 6, 2010 I'm unsure how to tell if its definately running in cpanel PHP: 5.2.14 MySQL: 5.0.91-community-log I can create databases and add tables, so I am assuming that it is running. I could be wrong though, its been known, I get the feeling I'm making some kind of schoolboy error. EDIT: I noticed there was no user associated with my db after manually creating it, so used the wizard, and the user was added, I temp gave user all access. Although as you pointed out I'm not even connecting to Mysql never mind database. Quote Link to comment Share on other sites More sharing options...
Adam Posted September 6, 2010 Share Posted September 6, 2010 No if you're able to query the database through your admin tool it's working. The error you're getting says there's something wrong with the authentication, you must have a problem with your username and/or password. Quote Link to comment Share on other sites More sharing options...
TEDSON Posted September 6, 2010 Author Share Posted September 6, 2010 Thank you so much for your help and time, I really appreciate it. I was using a database user and password, I tried my cpanel login credentiols and I get my Triumph message. I'm a little woried about having those details on my website though, is this normal practice? Quote Link to comment Share on other sites More sharing options...
Adam Posted September 6, 2010 Share Posted September 6, 2010 Yeah there's no problem with having those details in your code; the PHP source isn't displayed to the user. Just be careful of who you give access to your file system. Also a good security measure though is to store them in a file outside of the publicly accessible directory, i.e. not within htdocs / public_html / www - which ever name for it your web server is using. Quote Link to comment Share on other sites More sharing options...
TEDSON Posted September 6, 2010 Author Share Posted September 6, 2010 Thanks, I'l have to look into that. My end goal, is to have user and password authentication via my database in my application, which will be ran on a users local machine. The app starts, the user enters user and pass, the app triggers the php script which in turn querys the data base, if its good, app continues, if not app exits. So I dont want the mysql user and pass viewable from view sourcecode of page. The help I recieved here has been fantastic, and I thank you kindly for it. Quote Link to comment Share on other sites More sharing options...
Adam Posted September 6, 2010 Share Posted September 6, 2010 So I dont want the mysql user and pass viewable from view sourcecode of page. As I touched on before, the source of the PHP is never shown to the user. When the user requests a PHP page, it's parsed by the PHP engine and the resulting HTML passed back; not the PHP code itself. The help I recieved here has been fantastic, and I thank you kindly for it. No problem Quote Link to comment Share on other sites More sharing options...
TEDSON Posted September 6, 2010 Author Share Posted September 6, 2010 I did take note of that, but didnt quite get it, its server side code, I get it now. I'm a happy bunny, and cam move along with my project now. Thanks, this thread is solved. Quote Link to comment 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.