Multiple mysqli connection problems - php

I am having some strange issues with mysqli connections.
I was working on a page with mysqli, and it has been working fine all day. I then made a copy of this page, and stripped it down to debug a problem, and tested it as a different file. It worked fine connection wise. Upon trying to request the original file I was working on, I get the error:
Access denied for user 'user'#'localhost' (using password: YES)
I don't understand why. I have closed the connections after I have finished using them each time, although I don't see why that would be an issue. Interestingly, an older version of the file works fine, despite containing the exact same connection details and code.
What is going on?

Turn the question around. Rather than saying the two versions (the one that works and the one that doesn't) are identical in the aspects that matter, focus on the ways in which they are different and try to isolate which difference(s) also matter.
Make an additional copy of the working version. Verify that it works. Try making it into a copy of the non-working version by applying as many of the changes as you can, one by one, to this test copy, until you have something that is as close as possible to the broken version but that still works. Compare these two, and that should show you where the problem is.

Weird. If you are testing files from the same machine they should be working (if they have same code).
Check again username & password, i.e. spaces or strange chars.

Just for the sake of it, run a diff between the working copy and older version of the file. Check for any issues like: moved brackets, variable name changes, etc. Maybe the part of the code that defines the username and password never gets run!
If you need a free program for that, check WinDiff

If you put:
error_reporting(E_ALL);
as the first line in your code do you get any errors on either page? There may be something strange like the program can't open an include file anymore.
You said copying File A to File B means File A doesn't work. What happens if you copy File A to File B, delete File A then copy File B to File A?

Ensure that your IP is added to the MySQL allowed connections list, also ensure your password is correct. Try providing a full hostname rather than localhost if possible.
Post your code if un-successful.

Related

XML Parsing Error: xml processing instruction not at start of external entity

I have a vBulletin 3.8 forum.
When we click Edit button of any post (so the Quick Edit form should displayed), I get this error on the browser's console:
XML Parsing Error: xml processing instruction not at start of external entity
Location: http://www.xxxxx.xx/ajax.php?do=quickedit&p=438
Row number 2, Column 1:
... the Quick Edit form is not appearing the the progress bar displayed permanently.
I have try to disable hooks/ plugins, but the problem still appears.
I have this row on config.php: ini_set("display_errors", false); so I don't think it is a fatal error/ warning by PHP which brokes the xml normal syntax.
I have informed that this appear starts after the move of the site to another server. Does it say something to you?
Any general idea about this error?
EDIT:
Well, I found the reason of this issue, but I don't know how to fix it. Exact the same site on a localhost testing board works perfectly, but on the live server ANY html page/ ajax call etc, has a useless empty line as line #1.
For normal html pages, there is no reason for the browser to return an error, but when we're talking about an ajax call, this empty line at the top of the response, breaks the xml parsing from the browser. So it seems it is a server/ PHP/ Apache setting that applies this empty line. Any idea how to fix it? https://imgur.com/a/4neb0
It might be late for you but any new comers with php/nginx/apache can get an understanding of why.
Answer is simple: When moving the code, you might not be using git/rsync/scp but let me guess, you used zip (and probably Windows/Linux involved).
How to discover it was a two-day journey with many things tried:
We have the same error, we were also moving our servers. We tried:
We thought the server software version was a problem.
We thought the cloud provider OS image was a problem.
We used docker to avoid these problems, but the empty line problem persists.
We thought the code ?> ending was a problem, I went through all of them. But it wasn't.
I finally asked my colleague: How did you get the code? From Git? He said he downloaded from ZIP and then uploaded to server.
I removed code on the server (which extracted from a zip) and used git to download a fresh copy from our github.
Magic, problem solved. The empty line gone.
So I think the problem is with the zipping progress might have changed some file empty lines. Always use git.

is there a difference in php with defines from version 5 to version 7?

I migrated to godaddy for a perma-host. Now much of my code is not working.
For years I have used the following code in my gaming site. but now I want to move it to a more permanent host. I've been using php v5 with no issues, never had a reason to change. However I did finally migrate to PDO. With that said, here is the .ini file I use:(keep in mind, it is a giagantic ini file, but the following are what is important that is NOT working..
SQL_TYPE=mysql
HOST=localhost
USER=incognito
PASS=topsecret
DATABASE=mycooldb
to pull it for defines:
$defines = parse_ini_file('defines.ini');
foreach($defines as $field=>$data)
define($field, $data);
Now there is an error when I use godaddy (which i believe is up to v7 php).
If I run
die('host = '.HOST) // gives HOST
On my local server
die('host = '.HOST) // gives localhost - as it should
Why is this happening? I supposed I could line by line the defines, but they have worked for years the way I have it.
Thanks for any input.
Since it appears I have 2 problems, I will answer what addresses my first issue for this topic and close it:
There was not a problem with the DEFINES, but instead, my .ini files that stored the information for the variables that create the DEFINES.
In this ini file, I originally had "#" for comments as was accepted at the time, (which was what I was used to see: https://en.wikipedia.org/wiki/INI_file#Format)
However, not catching some of the "depreciation errors" I got over the years from PHP v5.3 - as I was stubborn changing - "#" was among those warnings. When PHP 7 finally came, the "#" no longer worked as comments, and thus my DEFINES variable ceased to work, as the script was stuck at the first commented line.
Conclusion: As far as my error, there is NO difference in the defines between PHP: v7 and v5, however the use of "#" as comments caused the error.
Thank you #Magnus Eriksson and #Calimero for the tip to display errors to catch it.

Specific php question

I am fairly new to creating websites and I have done a website that I did in a sandbox server on my personal computer, it works brilliantly, but as soon as I try to put it on my companies server to go up on the internet it doesn't work anymore. What the site does is it takes fields from an html form and then does a sql search of a database that I set up and works fine. Then after the user is done with the form they hit submit and the sql is run and results are returned to the same page. The form action="" and then the results are returned. I am not sure if the companies server has php installed, but I don't know if that would be a problem or not because the browser should still be able to display the php code. I have no problems with the html form, it is just when I hit submit that the page is returned and nothing is displayed.
Any help is welcome.
"I am not sure if the companies server has php installed, but I don't know if that would be a problem"
It is. Browsers can do nothing with PHP. Maybe you shoud register for a free hosting provider and learn the basics of hosting there.
PHP is a server-side language (i.e., it runs on the server, not the user's machine). Therefore, it must be installed on the company's server for any PHP code to execute properly. Browsers cannot interpret PHP code, and moreover PHP code is executed before the browser even receives any information (PHP: Hypertext Pre-Processor). Check that the server has PHP installed before you continue. (Also be aware of versions and features. For example, to use many new PHP features your server must be running PHP5.)
If you find that PHP is installed: Check your PHP syntax; make sure that no headers are sent after text is sent to the page; and make sure that there are no loops or anything in your code that could cause the script to continuously run without printing to the page.
Also, since your page is returned when you hit 'Submit,' but no new information is shown, make sure that there is not a problem with the MySQL configuration (e.g., incorrect password, query syntax, etc).
First you have to be sure that server has PHP installed.
make a file named phpinfo.php with this line
<?php phpinfo() ?>
and call it.
If it won't print out PHP config, you have to install PHP first.
Otherwise the problem most likely in the form of PHP tags, and you are using <? instead of <?php.
To solve, you can either change tags or set short_open_tag to on in the php.ini
According to your comment
when I hit submit that the page is returned and nothing is displayed
I am going to make an assumption that you are getting a blank page. If that is correct, then I am also going to follow that up with the assumption that PHP is installed, there is an error in your script (my guess is the database connection), and that the error is not being displayed.
Add these lines to the top of your page and post back with the result:
error_reporting(E_ALL);
ini_set('display_errors', '1');
Ya I think there is a problem in your database connection.First run your connect file whether it is showing some error or not.Plase add this line to your page:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
And please modify your php.ini file with this line:
display_errors = on

Why might this work on my server but not my schools?

I created a captcha just now, and it works PERFECTLY on my own server. On the school's server, it doesn't generate an image. Why might this be? The difference in code is one line.
Edit: Originally, it was working, but I deleted the directory by mistake and I do not know why did it suddenly work in the first place.
Update: I var_dumped() everything and everything is being set correctly.
Source code on school server:
Update: I figured it out! I'll post the answer later.
Make sure that GD library is enabled in your school's server.
Also try putting these lines on top of your script to see if there are any errors:
ini_set('display_errors', true);
error_reporting(E_ALL);
It is just useless to direct such kind of questions to SO.
There must be thousands of reasons.
And, of course, without access to your server and environment, nobody can say, just by looking into working code.
The only person who can answer this question is you yourself.
With help of your server, of course.
You must ask your server for errors.
ini_set('display_errors',1);
error_reporting(E_ALL);
but sometimes (in case of parse errors for example) this won't work. In this case you have to either set these params via .htaccess or check web-server's error log.
Also, you have to do something.
At least add some text output in the script to be sure it being executed.
print out variables using var_dump() to ensure thy contain right values.
Add an intentional error to ensure you CAN see them if any.
Do something, don't sit watching code!
Some more info on how to help yourself: http://www.ibm.com/developerworks/library/os-debug/

PHP "require_once" says it can't find a file in the directory

I'm debugging some PHP code written by an outsourcing company (I'm not a PHP guy at all but know the basics; we have an offshore team of PHP developers working for us on this project) that's not working; the code is supposed to be called every 30 minutes by a cron job but its not firing. I tried to run the PHP script via the command line to test if it's working, but it's giving me the following (anonymized) errror:
PHP Fatal error: require_once(): Failed opening required '/myapp/_lib/_classes/MySql.php' (include_path='.:/usr/share/pear:/usr/share/php') in /var/www/html/myapp/_lib/_base/common.inc.php on line 6
However, the file /var/www/html/myapp/_lib/_classes/MySql.php exists under the proper directory. I'm missing something simple, I'm sure, but like I said I know really nothing beyond the bare basics of PHP and I really need to get this service up and running.
EDIT: The code is using the __autoload() function with all classes in /_lib/_classes
if you start a file name with a slash, it means it's an absolute path. try instead:
require_once('/var/www/html/myapp/_lib/_classes/MySql.php');
Case sensitivity maybe? I've had that problem before.
I'm guessing it's a permissions issue - make sure both your account (for testing purposes) and whatever account cron is going to run your script under (quite possibly yours, but maybe a different one) have read privileges on the file.
Also: the path for the include says /myapp/_lib/_classes/MySql.php, whereas it exists in /var/www/html/myapp/_lib/_classes/MySql.php. Can you change the include to reference the full path?
As said, casing and in addition the correct permissions to access that file are needed.

Categories