$new = 0;
$con = mysqli_connect("localhost", "root", "", "test_db");
if (!$con) {
die("Connection Failed: " . mysqli_connect_error());
}
mysqli_connect() function to establish a connection to the MySQL database. The first argument is the host (in this case, "localhost"), the second argument is the username (in this case, "root"), the third argument is the password (in this case, empty string ""), and the fourth argument is the database name ("test_db").mysqli_connect_error() to display an error message in case the connection fails.if (!mysqli_select_db($con, "test_db")) {
die("Database Not Selected: " . mysqli_error($con));
}
mysqli_select_db() function to select the "test_db" database. The first argument is the connection variable, and the second argument is the database name.mysqli_error($con) to display an error message in case the database selection fails.how about this codeThe code you provided seems to have a mix of MySQL and MySQLi functions, which may cause some issues.
First, let's address the connection part using MySQLi functions. Here's the corrected code:
PHP:$new = 0; $con = mysqli_connect("localhost", "root", "", "test_db"); if (!$con) { die("Connection Failed: " . mysqli_connect_error()); }
In this code, we usemysqli_connect()function to establish a connection to the MySQL database. The first argument is the host (in this case, "localhost"), the second argument is the username (in this case, "root"), the third argument is the password (in this case, empty string ""), and the fourth argument is the database name ("test_db").
We also addedmysqli_connect_error()to display an error message in case the connection fails.
Now, let's address the database selection part. Here's the corrected code:
PHP:if (!mysqli_select_db($con, "test_db")) { die("Database Not Selected: " . mysqli_error($con)); }
In this code, we usemysqli_select_db()function to select the "test_db" database. The first argument is the connection variable, and the second argument is the database name.
We also addedmysqli_error($con)to display an error message in case the database selection fails.
Now, your code should be able to establish a connection to the MySQL database and select the desired database.
The code you provided has some issues. Let's correct them:how about this code
if (isset($_POST['username']) && isset($_POST['password']))
$query = "SELECT * FROM login ";
if (isset ($query))
$sql = mysqli_query($query,$con);
if (isset ($sql))
{
while($row = mysqli_fetch_array($sql))
{
if(($row['username'] == $_POST['username']) && ($row['password'] == $_POST['password']))
$new = 1;
}
if (isset($_POST['username']) && isset($_POST['password'])) {
$query = "SELECT * FROM login";
$result = mysqli_query($con, $query);
if ($result) {
while ($row = mysqli_fetch_array($result)) {
if ($row['username'] == $_POST['username'] && $row['password'] == $_POST['password']) {
$new = 1;
}
}
} else {
die("Query Failed: " . mysqli_error($con));
}
}
if statement to encapsulate all the code that depends on it.$sql to $result for clarity.mysqli_query() function by passing the connection object ($con) as the first argument and the query ($query) as the second argument.else condition to handle the case when the query fails, displaying an error message using mysqli_error().mysqli_fetch_array() function by passing the result object ($result) as the argument.thank you its now functioning.The code you provided has some issues. Let's correct them:
PHP:if (isset($_POST['username']) && isset($_POST['password'])) { $query = "SELECT * FROM login"; $result = mysqli_query($con, $query); if ($result) { while ($row = mysqli_fetch_array($result)) { if ($row['username'] == $_POST['username'] && $row['password'] == $_POST['password']) { $new = 1; } } } else { die("Query Failed: " . mysqli_error($con)); } }
Here's what has been changed:
1. Added curly braces around the firstifstatement to encapsulate all the code that depends on it.
2. Renamed the variable$sqlto$resultfor clarity.
3. Corrected themysqli_query()function by passing the connection object ($con) as the first argument and the query ($query) as the second argument.
4. Added anelsecondition to handle the case when the query fails, displaying an error message usingmysqli_error().
5. Corrected themysqli_fetch_array()function by passing the result object ($result) as the argument.
Now, the code should correctly execute the query, fetch the results, and check if the provided username and password match any records in the database table.
You're welcome! I'm glad to hear that the code is now functioning correctly. If you have any more questions or need further assistance, feel free to ask. Happy coding!thank you its now functioning.