Jump to content

php code for connecting to mysql database


mrchickenkiev

Recommended Posts

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>
Link to comment
Share on other sites

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 by DaveyK
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

At the moment I've got two books in front of me, and the internet, and I'm still stuck  :happy-04:

 

My connection code suggests I'm connected but the following error message suggests otherwise.

 

Connected successfully
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

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 by mrchickenkiev
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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>";
 
?>
Link to comment
Share on other sites

 

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.

Link to comment
Share on other sites

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 by mrchickenkiev
Link to comment
Share on other sites

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 by DaveyK
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.