Help with Connect Mysqli and Select

I have a code that seems to connect but doesn’t and my registration code does want to select from the database. I was wondering if anyone could help me out since PHP and Mysql have been updated?

Registration code to select from Database;

<?php $Config = mysqli_fetch_object($Confi = mysqli_query("SELECT * FROM Configuration")); if ($Config->Register == "true") { echo "?"; } else { echo "!"; } ?>

Connection to Database;

<?php $servername = "localhost"; $username = "username"; $password = "password"; // Create connection $conn = new mysqli($servername, $username, $password); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } echo "Connected successfully"; ?>

Correction;
I have a code that seems to connect but doesn’t and my registration code doesn’t want to select from the database.

You need to set php’s error_reporting to E_ALL and display_errors to ON, preferably in the php.ini on your system, so that php will help you by reporting and displaying all the errors it detects.

Your mysqli_query(…) call is incorrect. It’s missing the $conn connection parameter. You can find the correct usage for all the php statements in the php.net documentation.

Registration code to select from Database;

<?php
$Config = mysqli_fetch_object($Confi = mysqli_query(“SELECT * FROM Configuration”));
if ($Config->Register == “true”) { echo “?”; } else { echo “!”; } ?>

Connection to Database;

<?php
$servername = “localhost”;
$username = “username”;
$password = “password”;
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); }
echo “Connected successfully”; ?>

Had to reformat your code as I could not read it in your version!
Well, the second section of your code is basically a simple connection to the database. Looks normal.
That should be the first part of your program. It is used only once on a page. After that, you can use it
over and over as needed using the $conn-> format. Then, once the connection is made you use that
as the base for the second part. But, in the second part (which you listed first) you did not refer back to
the actual connection. Therefore, swap the order of these and alter the registration code to something
like this:

Registration code to select from Database;
<?php
$query = “SELECT * FROM Configuration”;
$results = mysqli_query($conn, $query);
$Config = mysqli_fetch_object($results);
if ($Config->Register == “true”) { echo “?”; } else { echo “!”; }
?>

Note that I altered the code to be more readable. You use OOP for part of your code and not for other parts. (OOP is Object Orientated Programming) Normally, you pick one and use it. Your creation of the connection and the query is not done in OOP and the compare is. It still should work that way. Just explaining your code since you are new to this. So, move your connection above the query or registration second and fix the variable names and let us know if it works for you.

Thank You phdr and ErnieAlex, I have now solved my problem and appreciate your help.

Always glad to here a solution was found! Good going…

Sponsor our Newsletter | Privacy Policy | Terms of Service