checking the database query ,always shows (database query failed) - php

i'm in a middle of making a query with my new database and i keep getting the message (Database query failed) through the code below:
<?php
//create a database connection
$dbhost= "localhost";
$dbname= "widget_corp";
$connection=mysqli_connect($dbhost , $dbname);
if(mysqli_connect_errno()){
die("Database connection failed :" . mysqli_connect_error ."(". mysqli_connect_errno .")");
}
?>
<?php
//perform a database query
$query = "SELECT * FROM subjects";
$result = mysqli_query($connection ,$query);
if (!$result){
die("Database query failed.");
}
?>
please advise

You aren't passing in a username or password. mysqli_connect() requires four parameters to be passed in : http://php.net/manual/en/function.mysqli-connect.php
You're only passing in a host and database name.

Related

failed in count rows in php sql

Thank you in advance!
Please tell me! what is mistake in this code. I want to show total rows of sql on my site. I searched many of codes be like this but all failed. Please help me.
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
$conn = new mysqli($servername, $username, $password, 'sahiwalservices');
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "hunnysheikh99";
;
mysqli_select_db("sahiwalservices", $conn);
$result = mysqli_query("select count(1) FROM login");
$row = mysqli_fetch_array($result);
$total = $row[0];
echo "Total rows: " . $total;
mysqli_close($conn);
Posting as a community wiki; I don't want rep from this.
mysqli_select_db() - The syntax for that is:
Db connection comes first
Database name is second
Yet, you don't need that line since you're already passing that in:
$conn = new mysqli($servername, $username, $password, 'sahiwalservices');
Then, you didn't pass db connection to the query.
Example from the manual:
if ($result = mysqli_query($link, "SELECT Name FROM City LIMIT 10"))
Reference:
http://php.net/manual/en/mysqli.query.php
Check for errors on the query also:
http://php.net/manual/en/mysqli.error.php

mysql insert error check

I have a PHP script that collects form data and inserts some of that data into a MySQL database. I just noticed that some inserts/records were NOT, or never created in the database. I would like to write a retry routine that if the insert fails to retry 3 times and then error out to the user.
Just so you can see my code for the DB and the insert so you can see that I am NOT nuts...
mysql_connect($hostname,$username, $password) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());
Here is my insert code:
$query = "INSERT INTO contacts VALUES ('','$name','$address','$city','$state','$zip','$phone','$email_address','$arrive','$depart','$room','$found','$promocode','$message','$datetimestamp','$ip')";
mysql_query($query);
mysql_close();
I started out with an IF statement then into a loop but got lost.
#Jay:
So something like this:
$conn = new mysqli($hostname, $username, $password, $dbname);
// check connection
if (mysqli_connect_errno())
{
exit('Connect failed: '. mysqli_connect_error());
}
$query = "INSERT INTO contacts VALUES ('','$name','$address','$city','$state','$zip','$phone','$email_address','$arrive','$depart','$room','$found','$promocode','$message','$datetimestamp','$ip')";
// Performs the $query on the server to insert the values
if ($conn->query($query) === TRUE) {
//echo 'users entry saved successfully';
}
else {
echo 'Error: '. $conn->error;
}
$conn->close();
I am already checking for injection before the insert query
Your query is perfect, make sure that number of parameter you are passing in sql query is same as number of column in database table & parameter value in sql is same order of database table column order

MySQL Syntax error near '' myguests '' at line 1

I'm getting the MySQL database backup using the php. But currently facing the given error. Anybody can tell me that how can I get rid from this error.
<?php
$db_server="server";
$db_username="username";
$db_password="";
$db_database="database";
$db_tablename="myguests";
$db_connection = mysqli_connect("$db_server","$db_username","$db_password","$db_database");
if(!$db_connection){
die("Database connection error: ".mysqli_errorno());
}
$db_backup= "/db_backup/";
$db_select = "SELECT * INTO OUTFILE '$db_backup' FROM '$db_tablename'";
$retval = mysqli_query($db_connection,$db_select);
if(!$retval){
die(/*"Could not take data backup: "*/mysqli_error($db_connection));
}else{
echo "Database backup successfully done";
}
mysqli_close($db_connection);
?>
You have put your database name in single quotes.
It should be back ticks.
Change:
$db_select = "SELECT * INTO OUTFILE '$db_backup' FROM '$db_tablename'";
To:
$db_select = "SELECT * INTO OUTFILE '$db_backup' FROM `$db_tablename`";
Also, no need to put double quotes here, it will work without them also:
$db_connection = mysqli_connect($db_server, $db_username, $db_password, $db_database);

Open an SQL connection just once - mysqli_query

I used
mysqli_connect("infos in here");
at the top of my page, and tried to use
mysqli_query("INSERT INTO and other info here");
When I do that, I get this error:
Warning: mysqli_query() expects at least 2 parameters, 1 given in (...)
But if I instead use
$con = mysqli_connect("infos in here");,
$mysqli_query($con,"INSERT INTO and other info here");
The error goes away, and my script works.
My problem is that I need to use mysqli_query two different times in my page, and I don't want to open the connection again when it's already open.
How can I handle this?
Thanks.
My problem is that I need to use mysqli_query two different times in
my page, and I don't want to open the connection again when it's
already open.
How is it a problem ? open once query as many times then close the connection, example:
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Perform queries
mysqli_query($con,"SELECT * FROM Persons");
//one more
mysqli_query($con,"INSERT INTO Persons (FirstName,LastName,Age)
VALUES ('Glenn','Quagmire',33)");
mysqli_close($con);
?>
No need to connect two times. An example here..
$con = mysqli_connect("host", "user", "password", "db");
if(mysqli_connect_errno()){
die(mysqli_connect_error());
}
//Query one
$result1 = mysqli_query($con, 'Query String');
//Query Two
$result2 = mysqli_query($con, 'Query String'); //Used same $con variable
//After finishing all queries
mysqli_close($con);
If you need to select multiple database then
$con = mysqli_connect("host", "user", "password");
//for DB bd_name1
mysqli_select_db($con, 'bd_name1');
$result1 = mysqli_query($con, 'Query String');
//for DB bd_name2
mysqli_select_db($con, 'bd_name2');
$result2 = mysqli_query($con, 'Query String');
But not connect frequent time.

PHP DB Connection questions

I am getting the error:
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in /home/mjcrawle/public_html/home/index.php on line 23
Line 23 turns out to be $num_results = mysqli_num_rows($result); but I am thinking the error is further up but I am having trouble finding it.
The actual code that I am using to connect to the DB is (I do understand there is a redundancy if the database cannot connect):
Any help would be wonderful and a reason for the error would be awesome!
/*Connect To DB*/
$conn = mysqli_connect($host, $user, $pwd)
or die("Could not connect: " . mysql_error()); //connect to server
mysqli_select_db($conn, $database)
or die("Error: Could not connect to the database: " . mysql_error());
/*Check for Connection*/
if(mysqli_connect_errno()){
/*Display Error message if fails*/
echo 'Error, could not connect to the database please try again later.';
exit();
}
/* Query for states */
$query = "SELECT StateAbbreviation, StateName, FROM USState ORDER BY StateName";
$result = mysqli_query($conn, $query);
$num_results = mysqli_num_rows($result);
?>
You have an extra comma before the FROM in query = "SELECT StateAbbreviation, StateName, FROM USState ORDER BY StateName";, you may be getting an error and not having a result when you execute the query.
If the query fails, mysqli_query returns boolean false
After $result = mysqli_query($conn, $query);, you should test the return value before continuing:
if ( ! $result){
$error = mysqli_error($conn);
//do something with the error message
}
See EmCo's answer for why your query is failing.
<?php
$con=mysqli_connect('localhost','root','','dbname') or die ("Connection Failed");
?>
This is the simple method of DB Connection

Categories