PDA

View Full Version : PHP code included within html



nacunis
05-16-2011, 09:41 AM
I have a landing page in which there is an include of a php module. I know it is executing because it is delivering echo output to page. However, the mysql query that seems to work in the authentication module, when ported and replacing code in this included code generates the following nasty-grams.

Warning: mysql_query() [function.mysql-query]: Access denied for user 'acunishi'@'localhost' (using password: NO) in /home1/acunishi/public_html/go/auth/birthdays_holidays-greetingsv2.php on line 29

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home1/acunishi/public_html/go/auth/birthdays_holidays-greetingsv2.php on line 29
Invalid query: Access denied for user 'acunishi'@'localhost' (using password: NO)

Any thoughts?

Here is the beginning of the code with name of database, table, username and password eliminated for security.

<?php
session_start();
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name=""; // Table name

// Connect to server and select databse.
$dblink = mysqli_connect($host,$username, $password,$db_name);
//mysqli_select_db("$db_name")or die("cannot select DB");

/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysqli_real_escape_string($dblink,$myusername);
$mypassword = mysqli_real_escape_string($dblink,$mypassword);
//end of setup and database open

$result3 = mysql_query("SELECT nickname, announce, lastlogin from '$db_name'.'$tbl_name' WHERE '$tbl_name'.'username' = '$myusername'");
if (!$result3) {
die('Invalid query: ' . mysql_error());
}


$num_rows = mysql_num_rows($result3);
$row3 = mysql_fetch_assoc($result3);
echo " \n <br>";
echo $row3['nickname'.' '.'announce'.' '.'lastlogin'];
echo " ";
echo " \n";


- Show quoted text -

felgall
05-16-2011, 02:38 PM
The most likely cause is that you left off your account name from the fromt of the database name and username. They should be written as

accountname_databasename
accountname_username

nacunis
05-16-2011, 03:02 PM
My account name is "acunishi". When I created the database called "loginsv4" I used "acunishi_loginv4" as the database name in the code.

The database has a created user with admin priviledges that when appended to account name, was "acunishi_php" (an arbitrary user name selection). That username is in the code.

So I don't understand your reply ... and they are the same database name as in accountname_databasename and accountname_username. That cam directly from the authentication module which is working.

Clearly, I am not understanding your response. What am I missing? This worked the same way with a previous webhosting company (accountname_username and databasename), so I am familiar with how that works.

Respectfully...

hollenback
05-17-2011, 08:21 PM
I noticed that you used mysqli to connect to the database and mysql for the query. Try using all mysqli or mysql functions without mixing them.

Bill

Basil
05-17-2011, 09:15 PM
Hollenback is right, it's defaulting to "mysql_connect" with no arguments because it has nothing else to go on. Switch to whichever the majority of your code uses.

Clarkey
05-18-2011, 04:26 AM
This is the code I use for the database and never have a problem for 5 years;
function db()
{
$dbuser = "texleath_drmills";
$dbserver = "localhost";
$dbpass = "texcig";
$dbname = "texleath_sales";

//CONNECTION STRING
$db_conn = mysql_connect($dbserver, $dbuser, $dbpass)
or die ("UNABLE TO CONNECT TO DATABASE");
mysql_select_db($dbname)
or die ("UNABLE TO SELECT DATABASE");
return $db_conn;
}//end function db