mrchickenkiev Posted June 24, 2013 Share Posted June 24, 2013 Hi Everyone, Apologies for the basic question, but since I can't see where I am going wrong, I probably won't get far reading the FAQ's. I've been using a section of code just to see whether I can connect up to my MySQL database, and after the last part of the statement I keep getting the text appearing on the relevant web page as if it were a comment. I've changed some of the details of the connection for security reasons, but this is essentially what I have got on the web page. Everything after the "Connected" text gets written onto the page, rather than displaying the database results. I tinkered a bit with the tags around "Connected", whose line originally had a semicolon at the end, but regardless of what I do at the end of this line I still get text. Any suggestions? Thanks. <body> <?php mysql_connect("localhost", "curem5801_pogopat", "chickenkievs427","curem8019_languages") or die('Unable to connect to MySQL'); echo <h1>Connected</h1> $result = mysqli_query($link, 'SELECT * FROM Rebus34_tbl'); while($row = mysqli_fetch_assoc($result)){ echo $row['curem5801_pogopat']; } ?> </body> Quote Link to comment Share on other sites More sharing options...
ginerjm Posted June 24, 2013 Share Posted June 24, 2013 everything after the 'die' is malformed code. Try quoting your echo, add a semi too. Add a <br> tag to your output echo so you can read the results. Quote Link to comment Share on other sites More sharing options...
DaveyK Posted June 24, 2013 Share Posted June 24, 2013 (edited) First of all, put your code in code block identified by the "<>" icon on the top. <?php mysql_connect("localhost", "curem5801_pogopat", "chickenkievs427","curem8019_languages") or die('Unable to connect to MySQL'); echo <h1>Connected</h1> $result = mysqli_query($link, 'SELECT * FROM Rebus34_tbl'); while($row = mysqli_fetch_assoc($result)){ echo $row['curem5801_pogopat']; } ?> So there is your PHP: - You are using mysql_connect() in one case but mysqli_query() in another. Those two dont combine. - Your mysqli_query() has a paramater of $link. Where is this set? You should also make sure you have error reporting turned on! Edited June 24, 2013 by DaveyK Quote Link to comment Share on other sites More sharing options...
mrchickenkiev Posted June 24, 2013 Author Share Posted June 24, 2013 That figures because I culled the code from two separate sites so, I did wonder whether the mysql and mysqli were incompatible. I was just trying to boil everything down to the bare minimum of code just to get the connection up and running. Hope I can get back to you guys because, I'm still not clear. I'm presuming I was correct to place the code between the two body tags in the HTML to begin with? Thanks again. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted June 24, 2013 Share Posted June 24, 2013 You should really read a short book on coding in php. A lot of understanding to be gained there. Quote Link to comment Share on other sites More sharing options...
mrchickenkiev Posted June 25, 2013 Author Share Posted June 25, 2013 (edited) At the moment I've got two books in front of me, and the internet, and I'm still stuck My connection code suggests I'm connected but the following error message suggests otherwise. Connected successfullyWarning: mysql_query() [function.mysql-query]: Access denied for user 'curem801'@'localhost' (using password: NO) in /home/curem801/public_html/connect.php on line 12 function.mysql-query Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/curem801/public_html/connect.php on line 12 Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /home/curem801/public_html/connect.php on line 20 I've got rid of the hyperlink that was in the function.mysql-query text. I created a php doc with the code in it but I am now getting error messages on lines 12 and 20, marked with **. The code is as follows from the sql query onwards: $sql = "SELECT * FROM `Rebus_tbl` LIMIT 0, 30 "; **$query = mysql_query( $sql ); echo "<table>"; //now read and display the entire row of a table one by one looping through it. //to loop we are using While condition here **while( $row = mysql_fetch_assoc($query) ) { echo "<tr><td>$row[Order]</td></tr>"; echo "<tr><td>$row[Word]</td></tr>"; echo "<tr><td>$row[translation]</td></tr>"; } echo "</table>"; ?> I'm getting the impression that these are variables which aren't being recognized? I generated the SQL directly from my phpadmin. Edited June 25, 2013 by mrchickenkiev Quote Link to comment Share on other sites More sharing options...
DaveyK Posted June 26, 2013 Share Posted June 26, 2013 all the warnings you posted are Warning: mysql_query() [function.mysql-query]: Access denied for user 'curem801'@'localhost' (using password: NO) in /home/curem801/public_html/connect.php on line 12 function.mysql-query Appereantly you are not able to connect to the DB. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted June 26, 2013 Share Posted June 26, 2013 your actual code is either making a mysql connection in a different process (file being included via a url) or it is closing the connection or it is making a mysqli connection, ... so that at the time you are trying to run the mysql_query() statement there isn't a valid mysql connection and the mysql_query() statement is attempting to create a connection using defaults (which will fail in most cases.) what's your actual code showing the database connection through to the mysql_query() that is failing? Quote Link to comment Share on other sites More sharing options...
mrchickenkiev Posted June 26, 2013 Author Share Posted June 26, 2013 Thanks. I think the connect code is essentially the same as on my previous attempt (highlighted in blue at the bottom of the page), with no reference this time to mysqli, anywhere in the code. As I said this code is now placed in a dedicated .php extension file in my public_html folder, which I suspect is not good from a security perspective. Perhaps the line I've highlighted below in red, closed the connection, though now when I run the code with that line omitted I'm getting the following error message: Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /home/curem8019/public_html/connect.php on line 20 Line 20 should be part of the following lines of code: while( $row = mysql_fetch_assoc($query) ) { echo "<tr><td>$row[Word]</td></tr>"; echo "<tr><td>$row[Order]</td></tr>"; echo "<tr><td>$row[Translation]</td></tr>"; } <?php $link = mysql_connect('localhost', 'curem5801_pogopat', 'chickenkievs427','curem8019_languages'); if (!$link) { die('Could not connect: ' . mysql_error()); } echo 'Connected successfully'; mysql_close($link); Quote Link to comment Share on other sites More sharing options...
mrchickenkiev Posted June 26, 2013 Author Share Posted June 26, 2013 I've been trying to run the code now with the mysql_close($link);element removed, and the error message I get still relates to the following line while( $row = mysql_fetch_assoc($query) ) albeit that the line number has changed due to editing between lines. When I enter the error message into Google search, I am either told that the error relates to the query, or there is still a connection error. Surely my connection error code would have flagged the latter eventuality and my SQL code is fairly mundane. I am also able to output the table to a CSV for excel format, from MyphpAdmin and it lists all the relevant records, using the same simple piece of SQL. I am presuming that the error message about the password also related to mysql_close($link); I've tried other versions of the code including error messages to indicate whether the SQL was incorrect, but it flagged nothing up, so I've removed them, for simplicity's sake. I'll list my code in full now. <?php $link = mysql_connect('localhost', 'curem5801_pogopat', 'chickenkievs427','curem8019_languages'); if (!$link) { die('Could not connect: ' . mysql_error()); } echo 'Connected successfully'; $sql = "SELECT * FROM `Rebus_tbl`"; $query = mysql_query( $sql ); echo "<table>"; //now read and display the entire row of a table one by one looping through it. //to loop we are using While condition here while( $row = mysql_fetch_assoc($query) ) { echo "<tr><td>$row[Word]</td></tr>"; echo "<tr><td>$row[Order]</td></tr>"; echo "<tr><td>$row[Translation]</td></tr>"; } echo "</table>"; ?> Quote Link to comment Share on other sites More sharing options...
DaveyK Posted June 26, 2013 Share Posted June 26, 2013 Thanks. I think the connect code is essentially the same as on my previous attempt (highlighted in blue at the bottom of the page), with no reference this time to mysqli, anywhere in the code. As I said this code is now placed in a dedicated .php extension file in my public_html folder, which I suspect is not good from a security perspective. Perhaps the line I've highlighted below in red, closed the connection, though now when I run the code with that line omitted I'm getting the following error message: Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /home/curem8019/public_html/connect.php on line 20 Line 20 should be part of the following lines of code: while( $row = mysql_fetch_assoc($query) ) { echo "<tr><td>$row[Word]</td></tr>"; echo "<tr><td>$row[Order]</td></tr>"; echo "<tr><td>$row[Translation]</td></tr>"; } <?php $link = mysql_connect('localhost', 'curem5801_pogopat', 'chickenkievs427','curem8019_languages'); if (!$link) { die('Could not connect: ' . mysql_error()); } echo 'Connected successfully'; mysql_close($link); What makes you think mysql_close() would close the connection? /sarcasm. Ofcourse it closes the connection! Also Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /home/curem8019/public_html/connect.php on line 20 is not an error on line 20, its an error on the line which holds the mysql query. The query itself is making the mysql_query() function return false. There is an error in your query. What you need to start doing is post code in code tags: <?php $link = mysql_connect('localhost', 'curem5801_pogopat', 'chickenkievs427','curem8019_languages'); if (!$link) { die('Could not connect: ' . mysql_error()); } echo 'Connected successfully'; $sql = "SELECT * FROM `Rebus_tbl`"; $query = mysql_query( $sql ); echo "<table>"; //now read and display the entire row of a table one by one looping through it. //to loop we are using While condition here while( $row = mysql_fetch_assoc($query) ) { echo "<tr><td>$row[Word]</td></tr>"; echo "<tr><td>$row[Order]</td></tr>"; echo "<tr><td>$row[Translation]</td></tr>"; } echo "</table>"; ?> like that. The error is, as I said cause by: $query = mysql_query( $sql ); what you need to do is: $query = mysql_query( $sql ) or die(mysql_error()); That will echo an error. Quote Link to comment Share on other sites More sharing options...
mrchickenkiev Posted June 26, 2013 Author Share Posted June 26, 2013 (edited) The error message becomes no database selected. And I now get your point about code tags. Thanks. The database should be curem8019_languages, mentioned in the connection code, and the table within the database is rebus_tbl. So what it is missing now, I wonder? Edited June 26, 2013 by mrchickenkiev Quote Link to comment Share on other sites More sharing options...
DaveyK Posted June 26, 2013 Share Posted June 26, 2013 (edited) Please read: http://php.net/manual/en/function.mysql-connect.php and try to understand what the function does. The fourth parameter you are passing, which you think is the table, is in fact: new_link If a second call is made to mysql_connect() with the same arguments, no new link will be established, but instead, the link identifier of the already opened link will be returned. The new_link parameter modifies this behavior and makes mysql_connect() always open a new link, even if mysql_connect() was called before with the same parameters. In SQL safe mode, this parameter is ignored. This is also from the same PHP.net page. You probably got confused with mysqli_connect() but they are very different So, now you must be wondering: how in the bloody world can I select a database in mysql? ----> http://bit.ly/19t20Ib Edited June 26, 2013 by DaveyK Quote Link to comment Share on other sites More sharing options...
ginerjm Posted June 26, 2013 Share Posted June 26, 2013 There are so many resources available to you if you would JUST do a google search. Yet you have persisted in floundering around trying to explain all kinds of different issues here. Aren't you tired yet? Do Some Work and look up some examples. Really. Quote Link to comment Share on other sites More sharing options...
mrchickenkiev Posted June 26, 2013 Author Share Posted June 26, 2013 DaveyK, I'm appreciating your comments btw. My reading insofar that I understand the links is that I should have done something like the following <?php $link = mysql_connect('localhost', 'curem801_pogopat', 'chickenkievs42'); if (!$link) { die('Could not connect: ' . mysql_error()); } echo 'Connected successfully'; $db = mysql_select_db('curem801_languages',$link)or die("Unable to select database"); Where I am now connecting successfully but getting "Unable to select database". I'm flicking between your links and my Dummies book on Website building. I realise the main bit of code I copied from a website, omitted the $db line which doesn't refer to the variable $link as my Dummies book does. Quote Link to comment Share on other sites More sharing options...
mrchickenkiev Posted June 26, 2013 Author Share Posted June 26, 2013 There are so many resources available to you if you would JUST do a google search. Yet you have persisted in floundering around trying to explain all kinds of different issues here. Aren't you tired yet? Do Some Work and look up some examples. Really. Thanks for you input. Quote Link to comment Share on other sites More sharing options...
DavidAM Posted June 26, 2013 Share Posted June 26, 2013 1) If that is your actual username and password for the database, you may want to change them right away. You have posted them on a public website that is indexed by Google (and others) regularly. In the future, when you post connection code, X-out the username and (especially) the password. $link = mysql_connect('localhost', 'XXXXX', 'XXXXX'); 2) The mysql (old library) funtions do not require the connection to be passed to every function. The library will use the last connection created. It is an optional parameter, and is useful if you need to connect to multiple different database servers. 3) in your "or die()" phrases attached to mysql calls, you should add the mysql_error call as part of the echo, it will tell you why the database rejected it. When the select database fails, it either does not exist, or the user does not have permission in it. Check your spelling (case may matter). Try $db = mysql_select_db('curem801_languages',$link)or die("Unable to select database: " . mysql_error()); and see what problem it reports. 4) If this is new development, you should be using the "improved" mysql library. The original one has been deprecated and there is no sense in learning it. See mysqli in the PHP manual. Quote Link to comment Share on other sites More sharing options...
mrchickenkiev Posted June 27, 2013 Author Share Posted June 27, 2013 Thanks for the tip on passwords and the information I have already given away, which I have now changed. Quote Link to comment Share on other sites More sharing options...
mrchickenkiev Posted June 27, 2013 Author Share Posted June 27, 2013 Thanks Guru. Connected successfully , Unable to select database: Access denied for user mysql_error echo now tells me access denied to the new user name, and one presumes the old user name would have returned the same message. The connection echo still tells me I am connected. Logically I would have thought the same log in details apply to both connecting and to accessing the database, so that if I can't access the database I shouldn't have been able to connect either? Quote Link to comment Share on other sites More sharing options...
boompa Posted June 27, 2013 Share Posted June 27, 2013 You are connecting as a particular user with a particular password. Did you 1. create that user and password in MySQL 2. GRANT that user permissions for the database to which you're connecting Quote Link to comment Share on other sites More sharing options...
mrchickenkiev Posted June 27, 2013 Author Share Posted June 27, 2013 (edited) Hallelujah! Straight through to the promised land this time, with the records listed on my website. Thanks boompa . As ginerjm has alluded to, I was getting really frustrated Now to get user forms, images uploaded..... Edited June 27, 2013 by mrchickenkiev 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.