/    Sign up×
Community /Pin to ProfileBookmark

mysqli_query(): Couldn’t fetch mysqli

Hi guys I know this might be a repeated post, someone has looked at the code and they seem to think that it all look fine, but I’m getting this following message again

Warning: mysqli_query(): Couldn’t fetch mysqli in C:xampphtdocssubmit-form.php on line 19
The following SQL Failed INSERT INTO ‘users’ (‘firstname’, ‘lastname’, ‘username’, ‘confirmusername’, ‘password’, ‘confirmpassword’, ’email’ ,’confirmemail’) VALUES (‘richard’, ‘Hemmings’, ‘hemmo001’, ‘hemmo001’, ‘password’, ‘password’, ‘[email protected]‘ , ‘[email protected]‘)

I just cant seem to see where I’m going wrong, at first there was a single quote missing from firstname’ this I have now addressed, I’ve been woriking on this now for 2 weeks today!

Config.php

[CODE]<?php

$connection = mysqli_connect(“localhost”,”root”,””,”registration”);

// Check connection
if (mysqli_connect_errno())
{
echo “Failed to connect to MySQL: ” . mysqli_connect_error();
}

mysqli_close($connection);
?>[/CODE]

?>
submit-form.php

[CODE]<?php
$connection = mysqli_connect(“localhost”, “root”, “”, “registration”) or die(“Error!!”);
//select your database
//$b=mysql_select_db(“database_name”,$a);
$firstname=$_POST[‘firstname’];
$lastname=$_POST[‘lastname’];
$username=$_POST[‘username’];
$confirmusername=$_POST[‘confirmusername’];
$password=$_POST[‘password’];
$confirmpassword=$_POST[‘confirmpassword’];
$email=$_POST[’email’];
$confirmemail=$_POST[‘confirmemail’];
//Database connection
require_once(“config.php”);

//mysql query to insert value to database
$query=”INSERT INTOTO ‘users’ (‘firstname’, ‘lastname’, ‘username’, ‘confirmusername’, ‘password’, ‘confirmpassword’, ’email’ ,’confirmemail’) VALUES (‘$firstname’, ‘$lastname’, ‘$username’, ‘$confirmusername’, ‘$password’, ‘$confirmpassword’, ‘$email’ , ‘$confirmemail’)”;

$result = mysqli_query($connection,$query);
//if value inserted successyully disply success message
if(!$result) {

die(“The following SQL Failed $query”);
}
echo ‘Registred successfully..!!</div>’;
?>[/CODE]

Any help would be appreicated

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@bionoidApr 21.2014 — Use backtick () for table and column names, single or double quotes for string values:

<CODE>$query = &quot;INSERT INTO </code></span>users<span><code> (</code></span>firstname<span><code>, </code></span>lastname<span><code>, </code></span>username<span><code>, </code></span>confirmusername<span><code>, </code></span>password<span><code>, </code></span>confirmpassword<span><code>, </code></span>email<span><code>, </code></span>confirmemail`) VALUES ('$firstname', '$lastname', '$username', '$confirmusername', '$password', '$confirmpassword', '$email', '$confirmemail')";


I would also suggest having the user input escaped before inserting it into the database, to help prevent SQL injections. You can use [FONT=Courier New]mysqli_real_escape_string[/FONT] for that.
Copy linkTweet thisAlerts:
@stokie-ruchauthorApr 21.2014 — Use backtick () for table and column names, single or double quotes for string values:

<CODE>$query = &quot;INSERT INTO </code></span>users<span><code> (</code></span>firstname<span><code>, </code></span>lastname<span><code>, </code></span>username<span><code>, </code></span>confirmusername<span><code>, </code></span>password<span><code>, </code></span>confirmpassword<span><code>, </code></span>email<span><code>, </code></span>confirmemail`) VALUES ('$firstname', '$lastname', '$username', '$confirmusername', '$password', '$confirmpassword', '$email', '$confirmemail')";


I would also suggest having the user input escaped before inserting it into the database, to help prevent SQL injections. You can use [FONT=Courier New]mysqli_real_escape_string[/FONT] for that.[/QUOTE]


Hey thanks for this I will take a look and let you know how I get on ?
Copy linkTweet thisAlerts:
@stokie-ruchauthorApr 22.2014 — hey guys i have tried what bionoid has suggested to me but I'm still getting the following message:......


Warning: mysqli_query(): Couldn't fetch mysqli in C:xampphtdocssubmit-form.php on line 18

The following SQL Failed INSERT INTO users (firstname, lastname, username, confirmusername, password, confirmpassword, email, confirmemail) VALUES ('richard', 'Hemmings', 'hemmo001', 'hemmo001', 'password', 'password', '[email protected]', '[email protected]')

I'm ready to bang my head against a brick wall now ?
Copy linkTweet thisAlerts:
@bionoidApr 22.2014 — Hi,

I have created a table with the columns you're inserting into... and used the query above without any changes. I get no error from the query.

A few things to check, in the database:

Please make sure all your column names are spelt exactly the same as what you have in the query.

Make sure the columns are the correct data type for the values you're storing.. I used varchar(255) for each as they are all strings, and a primary key "id".

If there are additional columns you're not populating then make sure they have default values assigned to them.

bionoid
Copy linkTweet thisAlerts:
@NogDogApr 22.2014 — Get rid of the line in red!
<i>
</i>&lt;?php

$connection = mysqli_connect("localhost","root","","registration");

// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

[COLOR="#B22222"][B]mysqli_close($connection);[/B][/COLOR]
?&gt;
Copy linkTweet thisAlerts:
@stokie-ruchauthorApr 22.2014 — Get rid of the line in red!
<i>
</i>&lt;?php

$connection = mysqli_connect("localhost","root","","registration");

// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

[COLOR="#B22222"][B]mysqli_close($connection);[/B][/COLOR]
?&gt;
[/QUOTE]


NogDog thank you sooooooooo much it works yayyy thanks so much for your help, you have made me one happy bunny.

Just one more quick question instead of having "failed to connect" or "Registred successfully..!!" I want to show the web pages instead of white pages.

How would I go about doing this?
Copy linkTweet thisAlerts:
@NogDogApr 22.2014 — ...

Just one more quick question instead of having "failed to connect" or "Registred successfully..!!" I want to show the web pages instead of white pages.

How would I go about doing this?[/QUOTE]


There are many ways to do it, of course. One way would be to create different HTML/PHP "template" files, and require() them based on the circumstance. So you might have a "success" template you would require() at the end if everything worked and you go there okay. If you hit an error condition, you would instead require() an "error" template, that might look for an $error variable for the message to display, which you would set in the main script. Simplified example:

main script:
[code=php]
<?php
// lots of stuff happens, then...
$result = mysqli_query($foo, $bar);
if($result == false) {
error_log(mysqli_error($foo));
$error = "Sorry, there was an unexpected database error."
require "error.php";
exit;
}
// ... more stuff happens, then if all is okay...
require 'success.php';
[/code]


error.php:
[code=php]
<!DOCTYPE html>
<html><head><title>Error!</title></head>
<body>
<p class='error'><?php
if(!empty($error)) {
echo $error;
}
else {
echo "We're sorry, but an unexpected error occurred."
}
?></p>
<p><a 'href=/main_script.php'>Back to whatever you call the page</a></p>
</body>
</html>
[/code]
×

Success!

Help @stokie-ruch spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...