unable to access certain php pages on localhost - php

I have a login page,login_form, menu page, and download page , this has been running successfully on a server earlier but i want all this to run on my local host, hence, I copied it all and put it in /var/www/html folder and was able to run the login page but the rest like menu.php and all dont come up.
IT has also a sql database which i just added into my system using, mysql -u root -p exaample < cs303.sql
and it is able to process normal select statements as well
Anybody here happens to know what is happening?
<?php
ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);
define ('HOSTNAME', 'localhost');
define ('USERNAME', 'root');
define ('PASSWORD', 'merlin');
define ('DATABASE_NAME', 'wicap');
$db = mysql_connect('localhost', 'root', 'merlin') or die ('Cannot connect to MySQL.'); // connecting to database
//echo"l; km;l";
mysql_select_db('wicap.sql');
$v1=$_REQUEST["username"];
$v2=$_REQUEST["password"];
//echo "sdfdf";
//$v1="ritish";
//$v2="ritish_r";
//echo($user);
//echo($pass);
$q=mysql_query("SELECT * FROM login where pass='".$v2."' and user='".$v1."'");
while($r=mysql_fetch_array($q))
{
echo($r[0].$r[1]+"/n");
echo "mohit";
}
mysql_close();
?>

#MerlinSudar: for more clarity i am demonstrating the code here
<?php //first PHP opening tag
//put these lines
ini_set("error_reporting",1)
ini_set('display_errors',1);
. //you code
. //you code
?>
Hope this helps

Follow the below Step hope it will solve your problem...
1) if you are using a xammp the put all your files in the htdocs folder, and if you are using the wammp the put all you file in www folder.
2)Check the linking of file, give the proper link connection.

Enable error reporting
ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);
Add this code on top of your index file and check
Since mysql is deprecated, please dont use it anymore rather use the improved version
of mysql http://php.net/manual/de/book.mysqli.php

Related

Constant variable already defined Xampp error

I am troubleshooting a site for someone and one of the error goes like this:
Constant serverusername already defined in C:\xampp\htdocs\employee\inc\db.php on line 5
The contents of db.php is as follows:
<?php
//Database Connection Settings
define ('hostnameorservername','localhost'); //Your server name or hostname goes in here
define ('serverusername','root'); //Your database username goes in here
define ('serverpassword',''); //Your database password goes in here
define ('databasenamed','asset'); //Your database name goes in here
global $connection;
$connection = #mysql_connect(hostnameorservername,serverusername,serverpassword) or die('Connection could not be made to the SQL Server. Please report this system error at <font color="blue">info#servername.com</font>');
#mysql_select_db(databasenamed,$connection) or die('Connection could not be made to the database. Please report this system error at <font color="blue">info#servername.com</font>');
?>
When I search the "serverusername" on the entire directory, I can't find it anywhere else other than on
:\xampp\htdocs\employee\inc\db.php on line 5
However the line:
include 'inc/db.php';
exists on numerous files.
What seems to be the problem? How can I resolve it? Thank you in advance
This problem may be occurred due to include of db.php file many times on same page.
Try this :
include_once 'inc/db.php';
Replace your code with this:
defined('hostnameorservername') or define('hostnameorservername', 'localhost');
defined('serverusername') or define('serverusername', 'root');
defined('serverpassword') or define('serverpassword', '');
defined('databasenamed') or define('databasenamed', 'asset');

Having Problems With PHP Connection

This is the code that connects to my SQL database. I'm new with this stuff and it seems to be semi-working but certain features on my website still don't work.
<?php
$con = mysql_connect("localhost","username","password");
$select_db = mysql_select_db('database1',$con);
/*$con = mysql_connect("localhost","username2","password2");
$select_db = mysql_select_db('database2',$con);*/
?>
This is the site in question: http://tmatube.com keep in mind the credentials above are filled in with what the programmer used for testing on his own server... ;) unfortunately I don't have access to him for support anymore.
Anyway, here's my thoughts on how this code needs to be edited maybe someone can chime in and let me know if I'm correct in my assumptions:
<?php
$con = mysql_connect("localhost","username1","password1"); -------------<<< leave this line
$select_db = mysql_select_db('DATABASE_NAME_HERE',$con);
/*$con = mysql_connect("localhost","DB_USERNAME_HERE","DB_PASSWORD_HERE");
$select_db = mysql_select_db('DATABASE_NAME_HERE',$con);*/
?>
Ok - now on to a few problems I noticed...
What does this do? /* code here */? It doesn't work at all if I leave that bit in.
Why is it connecting to database twice? and is it two separate databases?
$select_db = mysql_select_db('DATABASE_NAME_HERE',$con); <<<---- single '
When I tried to see if that line was correct the examples I saw had quotes like this
$select_db = mysql_select_db("DATABASE_NAME_HERE",$con); <<<---- double "
Which one is right?
He didn't leave it out. What he did was leave the database to be connected using the root, which has no password. The other connection (which is commented out) is using another user, rajvivya_video, with a password defined.
In testing it MIGHT be okay to connect to root and leave it without password, but even that is not recommended, since its so easy to work with a user and password defined (besides root).
Here is php mysql connect with mysqli:
<?php
$link = mysqli_connect("myhost","myuser","mypassw","mybd");
?>
No difference here with ' or ". (Anyway use mysqli and you can the wanted db as 4th parameter.) php quotes
/* comment */ is a commented out so the php does not care what is inside so only 2 first rows of are affecting (they are same mysql database on the local machine and 2 different user + password combinations). Comment in general are used to explain the code or removing part of the code with out erasing it. php commenting

Connecting to database via php code

I am programming a game in PHP and have the following code to connect to a database
//$sqldb=mysql_connect('godaddy.hostedresource.com', 'godaddyUserName', 'godaddyPassword') OR die ('Unable to connect to database! Please try again later.');
$sqldb=mysql_connect('localhost', 'root', 'mypassword') OR die ('Unable to connect to database! Please try again later.');
The trick here is that if I am on the production server I comment out the godaddy database; when I upload the code to the server I then comment out the localhost code instead.
Unfortunately the ineveitable has happened and I uploaded the code with the wrong connection commented out; this led to 24 hours of locked out customers! :(
Is there a way to have the code to tell if it is on the localhost server, and if it isn't it then looks for the godaddy connection?
you can try this to identify if its on live or localhost
if($_SERVER["SERVER_NAME"] == "localhost"
&&
$_SERVER["SERVER_ADDR"] == "127.0.0.1"){
// in localhost
$hostname = "localhost";
$username = "localuser";
$password = "localpassword";
}else{
// not in localhost
$hostname = "livehost";
$username = "liveuser";
$password = "livepassword";
}
and fail if couldn't connect to database but save the error into a file.
if(!mysql_connect($hostname,$username,$password)){
file_put_contents("mysql_connect.error",mysql_error(),FILE_APPEND);
die("Couldn't connect to database");
}
a suggestion, try not to use mysql_* anymore, switch to PDO or mysqli ..
if ($_SERVER['SERVER_NAME'] == 'the.name.of.your.devel.server') {
$host = 'localhost';
} else {
$host = 'name.of.godaddy.server';
}
$sqldb = mysql_connect($host, ....);
i normally use a method of obtaining the URL / domain of the site? This can work in certain situations and setups. Otherwise if your operating with a fixed IP than you can also use this method
Have a look over the methods using $_SERVER
PHP $_SERVER
One way would be for you to check your external IP address and see where you are. A solution should present itself by looking at the properties inside the $_SERVER global variable.
I have a good suggestion : You coding a game , game is a big program, you don't use mysql* function directly in big program , because yourself should handling them, such as error handling.i suggest you use a DB-Handler. please google for : DB-Handler PHP
As has been mentioned by other people, you can obtain the current site your script is running on using the $_SERVER variable. However, I would like to provide an alternative solution.
You could make a folder in your website (both local and production), something like config, then store a configuration file in it, for example config.php, with the following:
<?php
// Local
$db_host = 'localhost';
$db_username = 'root';
$db_password = 'mypassword';
?>
And for production:
<?php
// Production
$db_host = 'godaddy.hostedresource.com';
$db_username = 'godaddyUserName';
$db_password = 'godaddyPassword';
?>
and disallow access to the directory with a .htaccess file in the directory, something like:
deny from all
Then, in your PHP code, do the following:
<?php
require_once($_SERVER["DOCUMENT_ROOT"] . "/config/config.php");
$sqldb=mysql_connect($db_host, $db_username, $db_password) OR die ('Unable to connect to database! Please try again later.');
?>
Now, simply leave the different configuration files where they're at and upload everything else, so your code will access different configuration files whenever it runs.
Also, the .htaccess file should prevent anyone from accessing the file via HTTP, and having the file contents in PHP tags, as well as a .php extension should prevent anyone from seeing any contents if they were able to access the file (PHP would parse the file before it is rendered, and would output nothing).

Moving website to another local host to try - no errors, but no data is shown

This is my first time transferring my local dev site to another local site to test out, and can't access the database in the new site. Both computers are running XAMPP on Macs. The test site has successfully installed XAMPP, and has placed database files under Applications/XAMPP/xamppfiles/var/mysql. The test user is able to start Apache and MySQL, and see database files in PHPMyAdmin. The test user is also able to access the local web site with no error messages.
EDIT: The database files were copied over by copying/zipping the file on my end, and unzipping/dropping the copied files on the test end.
However, the local web site does not seem to see the database. Pages that are supposed to return data just show the default "sorry, we're sold out" message or show "image not found" graphics, and the admin is unable to log in.
The index page calls
include_once "common/base.php";
which consists of
<?php
// Set the error reporting level
error_reporting(E_ALL);
ini_set("display_errors", 1);
// Start a PHP session
session_start();
$_SESSION['docroot'] = $_SERVER['DOCUMENT_ROOT'] . '/mysite/';
// Include site constants
include_once $_SERVER['DOCUMENT_ROOT'] . "/inc/constants.inc.php";
if ( !isset($_SESSION['token']) )
{
$_SESSION['token'] = md5(uniqid(rand(), TRUE));
$_SESSION['token_time'] = time();
}
// Create a database object
try {
$dsn = "mysql:host=".DB_HOST.";dbname=".DB_NAME;
$_db = new PDO($dsn, DB_USER, DB_PASS);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
exit;
}
?>
If there was a connection problem, I would expect to see an error message returned, which I can verify by changing the constants in inc/constants.inc.php to bogus values. So I don't believe there's a connection problem.
If you were in my position, what would you check?
EDIT: I just found out about the Export function in PHPMyAdmin. I'm going to try deleting the db files that were copied over, exporting the files from PHPMyAdmin, and then importing them in the test machine.
EDIT: I just had the test machine do a simple
$sql = "SELECT FirstName From users";
$stmt = $_db->prepare($sql);
$stmt->execute();
$aaData = array();
while($row = $stmt->fetch(PDO::FETCH_NUM)){
$aaData[] = $row;
}
$stmt->closeCursor();
print_r($aaData);
on the index page, it shows the correct list of users. So the connection is good,
and there's an issue elsewhere. Would y'all say that I still need to delete the copied db and use the "correct" way (export/import), or is that a waste of time?
FINAL EDIT: I did redo the move using Export/Import and everything is working well now. Thanks to all.
First off, zipping files in the mysql data directory and moving them is not the best way to go. It can work if you have completely flushed/shut down the mysql server before moving them. You are better off using mysqldump or a similar tool to move entire databases at a time.
It sounds to me like you moved over your app's database files, but you probably forgot to add the user/password credentials to the new MySQL setup. You mentioned you use phpMyAdmin, so when you login on the other machine click the "Privileges" tab and make sure it matches up with the Privileges tab on your dev machine.
If it's not that, then it is entirely likely that by zipping and copying the files some/all of the tables were corrupted. Try using phpMyAdmin's Export and Import capabilities to move the database over. If the data is very large let me know and I can give you some command line entries to run.
Hope this helps.

Doing a query in wordpress?

I am editing an index.php file in the themes folder and I just need to run a query but I cant seem to run a query...here is what i tried
i tried to include the config files
<?php require( dirname(__FILE__) . '/wp-load.php'); ?>
<?php print DB_NAME;exit; ?>
that printed nothing
then i tried to do the mysql_query command
mysql_connect($server, $username, $password)
$users = mysql_query("select * ....");
print_r($users); exit;
nothing in either one....i know there has got to be an easier way to do a query in wordpress...any ideas
Don't use mysql directly (as a mysql connection already exists). Instead use the wpdb 'abstraction' - see the wordpress documentation here: http://codex.wordpress.org/Function_Reference/wpdb_Class

Categories