PDA

View Full Version : accessing MySQL database



KierDEvin
04-21-2008, 04:13 AM
I'll start by saying I'm pretty new to PHP and MySQL, so please bear with me.

I'm trying to make a forum (just for learnings sake). I've created a database, and two tables. But, I can't seem to access them. I keep getting these errors=

-------------

Warning: mysqli_connect() [function.mysqli-connect]: (42000/1044): Access denied for user 'accountname_userName'@'localhost' to database 'accountname_databaseName' in /home/magickby/public_html/causticreality/addTopic.php on line 10

Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given in /home/m*****y/public_html/c******y/addTopic.php on line 15

Warning: mysqli_error() expects parameter 1 to be mysqli, boolean given in /home/m*****y/public_html/c******y/addTopic.php on line 15

--------------

here is the php code I'm using=

--------------
<?php
//check for required fields from the form
if ((!$_POST["topic_owner"]) || (!$_POST["topic_title"]) || (!$_POST["post_text"]))
{
header("location: addTopic.htm");
exit;
}

//connect to server
$mysqli = mysqli_connect("localhost", "account_userName", "password", "account_databaseName");

//create and issue the first query
$add_topic = "INSERT INTO forum_topics (topic_title, topic_create_time, topic_owner) VALUES ('".$_POST["topic_title"]."',now(),
'".$_POST["topic_owner"]."')";
$add_topic_res = mysqli_query($mysqli, $add_topic_sql) or die(mysqli_error($mysqli));

//get the id of the last query
$topic_id = mysqli_insert_id($mysqli);

//create and issue the second query
$add_post_sql = "INSERT INTO forum_posts (topic_id,post_text,post_create_time,post_owner) VALUES ('".$topic_id."',
'".$_POST["post_text"]."', now(),
'".$_POST["topic_owner"]."')";
$add_post_res = mysqli_query($mysqli, $add_post_sql) or die(mysqli_error($mysqli));

//close connection to MySQL
mysqli_close($mysqli);

//create a nice message for user
$display_block = "<p>The <strong>".$_POST["topic_title"]."</strong> topic has been created.</p>";
?>
<html>
<head>
<title>New Topic Added</title>
</head>
<body>
<h1>New Topic Added</h1>
<?php echo $display_block; ?>
</body>
</html>

------------------

I've tried everything I can find, about the connections... Am I just missing something very basic? Or, am I going about this all wrong? Any help on this would be greatly appreciated.

Thanks,
Charles...

sjlplat
04-21-2008, 05:31 AM
Did you create a user and add it to the database access list with the appropriate permissions?

KierDEvin
04-21-2008, 06:28 AM
yea, I've created one. And I was able to use it to access mySQL from navicat (although, I couldn't access my new database from there??? ) In the scripts I pasted above, where I put username, password, etc., my script has the actual information.

Seb
04-21-2008, 09:20 AM
Do a echo mysqli_connect_error(); right after your connection. Maybe you forgot to prepend the user and database parameters with the cpanel username?

sjlplat
04-21-2008, 09:59 AM
yea, I've created one. And I was able to use it to access mySQL from navicat (although, I couldn't access my new database from there??? ) In the scripts I pasted above, where I put username, password, etc., my script has the actual information.

MySQL denies access to all users by default, so you must add the username to the database and set the permissions. Creating the username is only half of what is required.

pghcollectibles
04-21-2008, 10:53 AM
im just learning this stuff myself and everything looks good to me. i noticed one thing different than my code is you use the newer MYSQL Improved code. does hm support this?

mysqli vs. mysql

i am guessing it does as it gave the next errors $mysqli=0 because you didnt get connected. --->on line 15

host, user, password, database... the format seems good unless there is a typo in there somewhere? something misspelled that shouldnt have been and now your spelling it the way it should have been?
good luck

pghcollectibles
04-21-2008, 11:23 AM
i tried changing host, user, password and database and the only error of the four i got that were similar to yours is when i put in the wrong password or user name.

your error message also has the database name in the error but that could be because i connect to sql then i connect to the database. also i see that my error has (using password: YES) and i did not see that in yours.

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'myusername'@'localhost' (using password: YES) in /home/myusername/public_html/tests/test23.php on line 2

bottom line make sure you have correct spelling of username and password and that the user has been added with the proper permissions

KierDEvin
04-21-2008, 03:18 PM
OK, so I went through all the suggestions here, and finally found my problem. I had added my username to the database, but never clicked on it again to set the permissions. I completely missed that part. It works find now. Thanks a lot, for everyone's help!

Thanks,
Charles...