Is the php/mysql feature working okay

Hello,

I wanted to inquire, I have a basic account creation page up, and it doesn’t work. I have a paid host and I used a free host not long ago, using what I have that all worked fine. Soon as I tried here, my mysql db doesn’t log anything.

I can only think may be there is a problem with the php 7 setup on this host? If anything else has had a similar problem. Just seems strange that code I had used on two hosts responds as it should, but on another it doesn’t work.

Thank you.

@phplearner said:
Hello,

I wanted to inquire, I have a basic account creation page up, and it doesn’t work. I have a paid host and I used a free host not long ago, using what I have that all worked fine. Soon as I tried here, my mysql db doesn’t log anything.

I can only think may be there is a problem with the php 7 setup on this host? If anything else has had a similar problem. Just seems strange that code I had used on two hosts responds as it should, but on another it doesn’t work.

Thank you.

Wait…
Are you a premium customer or a free customer here on InfinityFree?

There are numerous reasons why a form using a database could fail. What do you see exactly when you try to use it here? Does the database connection or query return any error?

With MySQLi, you can use these functions to check for error:

Hi, thanks.

I am a free user.

As for the code, well it works on the paid host I use currently. So the errors are

mysqli::escape_string(): Couldn't fetch mysqli in  line 12 and 18 in register.php?

I guess this is line 12 $first_name = $mysqli->escape_string($_POST['firstname']);

and 18 $result = $mysqli->query("SELECT * FROM users WHERE email='$email'") or die($mysqli->error());

@phplearner said:
Hi, thanks.

I am a free user.

As for the code, well it works on the paid host I use currently. So the errors are

mysqli::escape_string(): Couldn't fetch mysqli in  line 12 and 18 in register.php?

I guess this is line 12 $first_name = $mysqli->escape_string($_POST['firstname']);

and 18 $result = $mysqli->query("SELECT * FROM users WHERE email='$email'") or die($mysqli->error());

Googling that error leads me to this answer: php - Warning: mysqli_query(): Couldn't fetch mysqli - Stack Overflow

Are you sure the database connection was established successfully? Use the function from the third link I sent to check it.

I’ve done what I did with the paid host, I also used another free host prior to this. Strange.

Not sure what else I can do really, I only left that free host due to the pop up ads, some misleading, dodgy, install a flash player, and another pornographic 3d.

Providing some (related functions) of your code would help us to understand better. Just be careful not to post any secret key or password, in case you wrote them inside the file/script.

But since you mentioned that the script is working well on other host (and assuming that you did not change any of its procedure), then it’s likely that:

  • you give incorrect information of your databases connection. It’s either the host, username, password, and database to use;
  • somehow you close the mysqli too early (as stated by Admin). Try checking related scripts; and/or
  • you did not include, require or declaring the mysqli_connect() at all.

Have you try to check the MySQL connection?
Ref: PHP: mysqli::ping - Manual

@phplearner said:
I’ve done what I did with the paid host, I also used another free host prior to this. Strange.

Not sure what else I can do really, I only left that free host due to the pop up ads, some misleading, dodgy, install a flash player, and another pornographic 3d.

Did you use mysqli_connect_error() to check for any errors?

You wouldn’t be the first or the last to make a typo in their configuration, or to forget to pass their database hostname to the database connection (which defaults to localhost - which is correct for most servers but not for us).

The code I have is simply from a youtube tutorial, it worked on the other two hosts, I don’t know what could be the reason here. All it did was register some details, it wasn’t anything concrete. But it worked on those other hosts.

So could be the problem here I wouldn’t know, I was using that template as a guide to try and learn some php.

This is the sql sql309.epizy.com so no localhost is used, this was the same with the other free host, as for the paid host, that was lh.

Thanks.

@phplearner said:
The code I have is simply from a youtube tutorial, it worked on the other two hosts, I don’t know what could be the reason here. All it did was register some details, it wasn’t anything concrete. But it worked on those other hosts.

So could be the problem here I wouldn’t know, I was using that template as a guide to try and learn some php.

This is the sql sql309.epizy.com so no localhost is used, this was the same with the other free host, as for the paid host, that was lh.

Thanks.

The fact that you entered it somewhere doesn’t mean it’s used. Again, you wouldn’t be the first to forget to pass the database hostname to the function.

I am happy to help you fix this issue, but I need your cooperation to do so. You’ve said “it works fine on paid host but not here” many times already, but that doesn’t tell me anything.

Could you please use mysqli_connect_error() to check for errors? PHP is happy to tell you what’s wrong, if you ask it. And if you can’t make sense of the error message, at least you can post it here and someone else might know what to do.

// Check if user with that email already exists
$result = $mysqli->query(“SELECT * FROM users WHERE email=‘$email’”) or die($mysqli->error());

// We know user email exists if the rows returned are more than 0
if ( $result->num_rows > 0 ) {

$_SESSION['message'] = 'User with this email already exists!';
header("location: error.php");

}
else { // Email doesn’t already exist in a database, proceed…

// active is 0 by DEFAULT (no need to include it here)
$sql = "INSERT INTO users (first_name, last_name, email, password) " 
        . "VALUES ('$first_name','$last_name','$email','$password')";

// Add user to the database
if ( $mysqli->query($sql) ){

    $_SESSION['active'] = 0; //0 until user activates their account with verify.php
    $_SESSION['logged_in'] = true; // So we know the user has logged in
    $_SESSION['message'] =
            
             "Confirmation link has been sent to $email, please verify
             your account by clicking on the link in the message!";

    mail( $to, $subject, $message_body );

    header("location: profile.php"); 

}

else {
    $_SESSION['message'] = 'Registration failed!';
    header("location: error.php");
}

}

This is on the register.php

Ok, I think this reg/log system is pretty dangerous due to no any kind of prepared queries or at least escaped strings.

We’ve asked you about error, that you can get by functions like mysqli_error.
Add the following code please:
echo mysqli_error($mysqli);
This code kinda will print error description on your page, so comment following lines(with adding two slashes without quotes “//” at the line beggining):
header("location: profile.php");
header("location: error.php");
That will prevent redirecting, and will make you able to read the printed error.

Well it is basic, I don’t really understand any of it, according to a php expert, it is complete tosh. Yet it works on the other hosts.

It isn’t as if the pages can be read.

echo mysqli_error($mysqli);
It still mentions line 18 as being the problem.

@phplearner said:
Well it is basic, I don’t really understand any of it, according to a php expert, it is complete tosh. Yet it works on the other hosts.

And I’m inclined to agree with that expert. This code is very poor.

I think I understand what’s going on here, so I think these are going to be my parting words for this topic:

If you don’t know PHP and just want a system which works: go with proper, pre-built software which does what you need it to. Softaculous offers a great selection of them. Don’t try to gaffer something yourself using snippets you found online and don’t understand, because you get, well, this.

If you don’t know PHP yet and are trying to learn, then that’s great! But don’t accuse a hosting provider of their servers being broken when it might well be that your own rookie code isn’t so flexible yet.

If you do know PHP and want to build your own software, them build some proper error handling first.

If there is an issue with our systems or something which works a bit different than usual, I’m happy to work with you to figure it out. But when I ask you to do a bit of troubleshooting on your own code to figure out more (and give you pointers to do so), I need you to cooperate and provide me with the needed information. Help me help you.

@phplearner said:

// Check if user with that email already exists
$result = $mysqli->query(“SELECT * FROM users WHERE email=‘$email’”) or die($mysqli->error());

else {
$_SESSION[‘message’] = ‘Registration failed!’;
header(“location: error.php”);
}

}

This is on the register.php

Hey, I hope this just some part of your code/there are more of it. Because if it wasn’t, then you may forget to add session_start() at the very beginning.

Just random thought.