I've had this problem before, but I can't remember how I got over it. Basically I have a code something like this:
$con = mysqli_connect("localhost", "user", "password", "database");
$grab = mysqli_query($con, "SELECT * FROM Tables;");
while($row = mysqli_fetch_array($grab)){
if($row['BasketId'] == $basketId){
$total = $row['Total'];
}
}
But I keep getting access denied for user # localhost. The problem is that immediately before this php there is another php file with the exact same code and it connects and works fine. Why would this php file be different.
I've checked all the privileges and they are fine. Plus like I said there is an identical piece of code previously that works fine. For some reason this mysql doesn't like this php file. What could be the reason. I'm guessing that mysql is just buggy crap.
Test that you connection attempt has succeeded and if not report the error message, then you will knwo what is wrong.
Its also a good idea to make sure that error reporting is set to show errors on the web page, during development, assuming you are using a live server where error reporting will probably be turned off
Ripped off for the strangest place The PHP Manual
<?php
php error_reporting(E_ALL);
ini_set('display_errors', 1);
$con = mysqli_connect("localhost", "user", "password", "database");
if (!$con ) {
echo 'Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error();
exit;
}
Related
I have quite some experience with PHP and MySQL, but I have always been working on already set up servers. Now I have my own server (it's actually just a webspace) but it still has all of the standard settings. I have following problems with those:
I set up a connection to my database.
$mysqli = new mysqli("mydatabase", "myuser", "mypw", "mydatabasename");
if ($mysqli->connect_error) {
echo "Error: " . mysqli_connect_error();
exit();
} else {
echo "succesfully connected";
}
Which gives me the output succesfully connected, as you would expect. If I want to do a simple query like this
if ($stmt = $mysqli -> prepare ("SELECT id FROM Users") {
$stmt -> execute();
$stmt -> bind_result($id);
while($stmt -> fetch) {
echo $id;
}
}
I get a blank HTTP ERROR 500. The SQL query works fine in PHPmyAdmin though.
What I did so far is:
Checking the Error log (did not contain any errors)
Trying to add error_reporting(E_ALL);
ini_set('display_errors', 'On'); in front of my script (did not change anything)
Trying to find my php.ini-file to modify (did not find it on the server)
If anyone could help me out and tell me how to get the mysql working, that would be great. Thanks a lot in advance.
I have a PHP web app which has been working fine until today (it is sitting on an Windows/IIS server). When I attempt to access a page which connects to the MYSQL database, I get a blank page in firefox. In IE I get a 500 Internal Server Error. Normal PHP works fine, it is simply when I try to connect to the database. The mysqli connection is not giving any error messages.
The weird thing - I have phpmyadmin on the same server and it is working fine, I can see my database and interact with it.
I also have another database on the server which is working fine, the user attached to that database has read only access (as it is an archive database), but the PHP web app connecting to it works.
What have I tried?
Initially I thought it was a user issue, so I created a new user in phpmyadmin with full privileges and had the PHP app log on using it instead, same result.
I then thought that it was an issue with the database, so I copied the data and structure to a new database, same result.
The MYSQL.err log file has no errors and indicates that the server is accepting connections (which it is, as phpmyadmin is working).
I have even tried writing a test script:
<?php
//Debugging
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
include "settings.php";
echo $HOST . "<br>";
echo $DBNAME . "<br>";
echo $DBUSERNAME . "<br>";
echo $DBPASSWORD . "<br>";
$CON = new mysqli($HOST, $DBUSERNAME, $DBPASSWORD, $DBNAME);
//Check the connection and report error if it failed
if ($CON->connect_errnum > 0)
{
die('Unable to connect to database [' . $CON->connect_error . ']');
}
$result = $CON->query("select user() AS us");
if (!$result)
die("There was an error running the query [" .$CON->error ."]");
}
while($row = $result->fetch_assoc())
{
echo $row["us"] . "<br>";
}
?>
If I don't have he code from the $result = $CON->query("select user() AS us"); down, it displays the 4 values. Once I include it, I get the 500 Internal server error page.
I am at a loss to figure out what is happening or where to start looking.
One of my support classes had a syntax error in it, this caused the page to fail before it got to the database connection. Because my error reporting was turned off, I could not see this (even though I thought it was on). I had to go in to PHP.ini and set display_errors = off to display_errors = on. This then showed the error message.
I'm in troubles...
I have a web server apache/php under linux, also i have another server with SQL Server database under windows. I need to get access to SQL Server database from my apache/php.
What do i need, please help. I have no idea how to start.
**sorry for my english.
Could do with a bit more clarification but I'll try my best..
If you mean through the code, you would do it like this:
Create a new php file called "connect.php" and insert all of this code, replacing the tags like USERNAME with the correct details.
<?php error_reporting(E_ALL); ini_set('display_errors', 1);
// Create connection
$con = mysqli_connect("HOST","USERNAME","PASSWORD","DATABASE");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$select_db = mysqli_select_db($con, 'DATABASE');
if (!$select_db){
die("Database Selection Failed" . mysqli_error($con));
}
?>
Next, EVERY file that requires a connection to the database, simply add
include("connect.php"):
To it at the start of the page and it should be fine :)
I seriously recommend you read up a bit more though as you seem like a bit of a newb (we all started somewhere)!
Hope this is what you were after!
Look for dblib, you will need to add that driver.
I have researched my question but there just isn't much help out there for MySQLI functions. I'm also new here so, please bear with me.
I have a test database named "website" and it contains a table called "users" which has data for all of the members of my website. In a separate file I have the php code that connects to my localhost and selects the database "website." I'm using the newest functions of MySQL so instead of the connection string containing mysql_connect it contains mysqli_connect. The registration process works great and I can find no error in any of the other code that uses the mysqli functions.
My include file (connect.php)
<?php
$link = mysqli_connect("localhost", "useracc", "useracc1", "website");
?>
I set up a test script for the login section.
<?php
include('connect.php');
$user = $_POST['user'];
$query = "SELECT birthday FROM users";
$result = mysqli_query($link, $query) or die ("RESULT ERROR");
echo "$user";
echo "$result";
?>
There is a password box on the previous page but for the test script I left that out and so far, only the $user is shown on the page. I get no error even when I replace "RESULT ERROR" with mysqli_error($link). I've tried interchanging ' for " in every part of the code that uses quotes but that didn't work. I've tried rewording the $query line but that also had no effect. I'm really new to MySQL and php so if you guys could tell me where I'm going wrong I'd really appreciate it.
Thanks for reading my question. Have a great day.
Add the following lines to the connect.php after you connect line
if (mysqli_connect_errno()) {
printf("Can't connect Errorcode: %s\n", mysqli_connect_error());
exit;
}
this will ensure the connection is made without any errors and return them if there are ...
I'm trying to set up a login script for PHP using the tutorial on this site. The problem is that the site stops when it hits these lines, no error, no text:
<?php
$conn = mysql_connect('localhost', 'root', 'password') or die('error line7' . mysql_error());
mysql_select_db('mydb', $conn) or die('error line8' . mysql_error());
?>
If I take out these lines, the rest of the html runs perfectly. I've double checked my passwords and everything, nothing is working.
I know very little about php and mysql other than what I've learned trying to set this up.
Maybe you don't have errors enabled.
Try enabling them before those lines like so
error_reporting(E_ALL & ~E_NOTICE);
ini_set('display_errors', '1');
Good luck.
Beside all the other tips, which are good ones, Learn to tail -f the apache error log and the mysql log (all the possible logs which make sense to follow).
While in development. that is.
Do something like this:
$mysqli = new mysqli("localhost", "root", "password", "database");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
if ($result = $mysqli->query("YOUR QUERY")) {
while($row = $result->fetch_row()){
// do what you want with resutls
}
$result->close();
}
Check that the MySQL extensions are enabled in your install PHP. You can check by putting the following in one file and falling from the browser:
<?
phpinfo();
?>
It's a lengthy page, but it tells you almost everything about your PHP install. Including what extensions are installed.