mysqli_error not returning anything - php

I have a php script which is supposed to insert a new row to a remote database. Something is not quite working as it should, but when I try to debug with mysqli_error it doesn't return anyting. Code as follows:
$connect = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname) or die("Unable to Connect to '$dbhost'");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = "INSERT INTO #DBname.TableName# (Section,Gender,WinningTeam,LosingTeam,FixtureD,FixtureT,Venue,Court,Texts,SetsWon,SetsLost,Winner_Score,Loser_Score) VALUES ($Section,$Gender,$WinningTeam,$LosingTeam,$FixtureD,$FixtureT,$Venue,$Court,$Texts,$SetsWon,$SetsLost,$Winner_Score,$Loser_Score)";
$result = mysqli_query($connect, $query) or die (mysqli_error());
The error message says mysqli_error expects exactly 1 paramater, 0 was given. However when I replace it with mysqli_error($connect) I get Error 404 - php script not found on server.
Pretty sure I'm missing something obvious, can anyone help out?
Kind regards

function mysqli_error() needs one parameter according to php.net
So in your case:
$result = mysqli_query($connect, $query) or die (mysqli_error($connect));

Related

GoDaddy can't select database

I'm running this code on GoDaddy webhosting and I'm getting 'The database could not be found' echoed.
Obviously the database in question can't be selected, even though I've privileged the user and checked the db name.
I don't get anything out of here mysqli_error()
$db= 'test2' ;
$con = mysqli_connect('whatever','whatever','whatever') or die ('The connection to the database could not be established.');
mysqli_select_db($db , $con) or die ('The database could not be found' . mysqli_error());
As per the mysqli_select_db documentation, it expects the parameters this way:
mysqli_select_db ( mysqli $link , string $dbname ) : bool
So your parameters are put in backwards, change it to this:
mysqli_select_db($con, $db) ...
Or, alternatively, just select the database inside mysqli_connect().
$con = mysqli_connect('whatever','whatever','whatever', $db) ...
Side note, your die() isn't really doing anything, you won't get an actual error code out of that. To use mysqli_error(), you need to pass your database handle:
die('There was an error: ' . mysqli_error($con));
For the die() that is attached to mysqli_connect(), you should do this:
die('There was an error: ' . mysqli_connect_error());

Can't fetch data from database table.... 500 error

I have tried a ton of different versions of this code, from tons of different websites. I am entirely confused why this isn't working. Even copy and pasted code wont work. I am fairly new to PHP and MySQL, but have done a decent amount of HTML, CSS, and JS so I am not super new to code in general, but I am still a beginner
Here is what I have. I am trying to fetch data from a database to compare it to user entered data from the last page (essentially a login thing). I haven't even gotten to the comparison part yet because I can't fetch information, all I am getting is a 500 error code in chrome's debug window. I am completely clueless on this because everything I have read says this should be completely fine.
I'm completely worn out from this, it's been frustrating me to no end. Hopefully someone here can help. For the record, it connects just fine, its the minute I try to use the $sql variable that everything falls apart. I'm doing this on Godaddy hosting, if that means anything.
<?php
$servername = "localhost";
$username = "joemama198";
$pass = "Password";
$dbname = "EmployeeTimesheet";
// Create connection
$conn = mysqli_conect($servername, $username, $pass, $dbname);
// Check connection
if (mysqli_connect_errno) {
echo "Failed to connect to MySQL: " . mysqi_connect_error();
}
$sql = 'SELECT Name FROM Employee List';
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo "Name: " . $row["Name"]. "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
There be trouble here:
// Create connection
$conn = mysqli_conect($servername, $username, $pass, $dbname);
// Check connection
if (mysqli_connect_errno) {
echo "Failed to connect to MySQL: " . mysqi_connect_error();
}
There are three problems here:
mysqli_conect() instead of mysqli_connect() (note the double n in connect)
mysqli_connect_errno should be a function: mysqli_connect_errno()
mysqi_connect_error() instead of mysqli_connect_error() (note the l in mysqli)
The reason you're getting a 500 error is that you do not have debugging enabled. Please add the following to the very top of your script:
ini_set('display_errors', 'on');
error_reporting(E_ALL);
That should prevent a not-so-useful 500 error from appearing, and should instead show the actual reason for any other errors.
There might be a problem here:
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
If the query fails, $result will be false and you will get an error on the mysqli_num_rows() call. You should add a check between there:
$result = mysqli_query($conn, $sql);
if (!$result) {
die('Query failed because: ' . mysqli_error($conn));
}
if (mysqli_num_rows($result) > 0) {
The name of your database table in your select statement has a space in it. If that is intended try:
$sql = 'SELECT Name FROM `Employee List`';
i think you left blank space in your query.
$sql = 'SELECT Name FROM Employee List';
change to
$sql = "SELECT `Name` FROM `EmployeeList`";

How to get mysqli_query error report properly in php? [duplicate]

This question already has an answer here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 5 years ago.
Really, it drive me crazy when I can't detect what error that happens. I can't handle it.
I managed to make a connection to MySQL, and check it out with:
$connection = mysqli_connect(HOST, USER, PASS, DB) or die('Could not connect!');
if($connection){
echo 'It's connected!';
}
Yeah, that say connected. Then, when I try a query, it fails without error reporting. I've tried do this to check if it fails:
$query = "SELECT $field FROM users WHERE id = ".$_SESSION['user_id'];
$result = mysqli_query($dbc, $query);
if($result){
echo 'Query OK';
}else{
echo 'Query failed';
}
The browser said: Query failed. So, there's an error in my query. Then I echoed the query out with this:
echo $query;
// Printed in the browser: SELECT firstname FROM users WHERE id = 1
Copy that value and use it in phpMyAdmin. It works. So, i guess an error occured in mysqli_query function. But i can't get the error message and so i don't know what's going on. I've tried this:
$result = mysqli_query($dbc, $query) or die(mysqli_error($dbc));
and this:
if(!$result){
echo mysqli_error($dbc);
}
Nothing happens. The browser just blank. Then, I tried to change this:
$result = mysqli_query($dbc, $query);
to this:
$result = mysqli_query($query);
Still nothing happens. What's going on? How can I know what error occured?
I run the server in Debian with phpinfo(): PHP Version 5.4.36-0+deb7u3
Guys read what they have posted, they are mixing the connections up.
$connection = mysqli_connect(HOST, USER, PASS, DB) or die('Could not connect!');
So this line should read:
$result = mysqli_query($connection, $query) or die(mysqli_error($connection));
With mysqli_ you need to pass the connection to a number of things that was not required in mysql_
Or you can switch your original db connect to $dbc
$dbc = mysqli_connect(HOST, USER, PASS, DB) or die('Could not connect!');
I hope this helps!

Error message: Database Query failed

For some reason, the following code inside the query works in my MySQL command console, yet when I try to run it as a Query in PHP, something keeps going wrong and I'm not sure what. Here is the code I've done so far.
//2. Perform database query
$query = "SELECT skills.element_id, content_model_reference.element_id, element_name FROM skills, content_model_reference WHERE (skills.element_id = content_model_reference.element_id)";
$result = mysql_query($query);
//Tests if there was a query error
if(!$result){
die("Database query failed.");
}
Is there something preventing the code that worked in MySQL (The line with SELECT) from working, or is my syntax somehow wrong?
EDIT: So it's saying I didn't select a database. Yet I thought I had. Here is the code above it:
//1. Create a database connection
$dbhost = "host"; //Host: Can be either an IP address, or a domain (like google.com).
$dbuser = "user";//User: The user that is connecting to the database.
$dbpass = "pass";//Password: This is the password that the user is using.
$dbname = "db";//Name: This is the name of the database.
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);//The value, 'handle,' is the connection.
//Test if connection occurred. Die ends the program/php, and in this case, also prints a message
if(mysqli_connect_errno()){
die("Database connection failed: ".
mysqli_connect_error().
" (". mysqli_connect_errno() . ")"
);
}
Like I said, the error message I am getting is pertaining only to the query, the server is fine with my database connection.
You're using mysqli_* for the connection, but you're using mysql_* for the QUERY... don't think you can do that, has to be one or the other (MYSQLI_ preffered). Also the query should be:
$result = mysqli_query($connection,$query);

php script connecting to my SQL database

When trying to connect to my database using PHP I am getting an error.
$connect = mysql_connect("localhost","username","password") or die ("Couldn't Connect!");
mysql_select_db("databasename") or die ("Couldnt find database");
When I try running the script it keeps saying 'Couldnt find database' which means that I am connecting to the server but my database won't connect.
Am I doing something wrong here? I have pretty much copied and pasted my database name from Cpanel so I know there aren't any mistakes. But in Cpanel it is displayed as 'mywebsite'_database
Any ideas?
You specify in your question that your database is called:
'mywebsite'_database
If this is the case, then you need to change your code to
// Connect to the database
$connect = mysql_connect("localhost","username","password") or die ("Couldn't Connect!");
// Select the database
mysql_select_db("mywebsite_databasename",$connect) or die ("Couldnt find database");
If the above method does not work then I would advise debugging your connection by listing the databases for that particular user. Do this like so:
// Connect to the database
$connect = mysql_connect("localhost","username","password") or die ("Couldn't Connect!");
// Get all the databases for this particular user
$databases = mysql_list_dbs($connect);
// Loop through the databases and write them on the screen
while($database = mysql_fetch_assoc($databases)) {
echo $database['Database']."\n";
}
exit;
mysql_select_db("mywebsite_database")
Print out the names of all the databases in the server (using mysql_list_dbs).
Find the correct database name and replace "databasename" with it.
$connect = mysql_connect("localhost","username","password") or die ("Couldn't Connect!");
$db_list = mysql_list_dbs($connect);
while ($row = mysql_fetch_object($db_list)) {
echo $row->Database . "\n";
}
mysql_select_db("databasename", $connect) or die ("Couldnt find database");
I recommend using MySQLi extension as mysql_* is currently in a depreciation process.
I hope this helps

Categories