Help with this code

reacah

Established
$new = 0;
$con = mysqli_connect("localhost","root");
if(!$con)
{
die("Connection Failed");

}
if(!(mysql_select_db("test_db", $con)))
{
die ("Database Not Selected");
}
 
The 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 use 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").

We also added mysqli_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 use 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.

We also added mysqli_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 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 use 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").

We also added mysqli_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 use 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.

We also added mysqli_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.
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;
}
 
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;
}
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 first if statement to encapsulate all the code that depends on it.
2. Renamed the variable $sql to $result for clarity.
3. Corrected the mysqli_query() function by passing the connection object ($con) as the first argument and the query ($query) as the second argument.
4. Added an else condition to handle the case when the query fails, displaying an error message using mysqli_error().
5. Corrected the mysqli_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.
 
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 first if statement to encapsulate all the code that depends on it.
2. Renamed the variable $sql to $result for clarity.
3. Corrected the mysqli_query() function by passing the connection object ($con) as the first argument and the query ($query) as the second argument.
4. Added an else condition to handle the case when the query fails, displaying an error message using mysqli_error().
5. Corrected the mysqli_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.
thank you its now functioning.
 
thank you its now functioning.
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!
 

About this Thread

  • 5
    Replies
  • 403
    Views
  • 1
    Participants
Last reply from:
Unknown user

Online now

Members online
1,050
Guests online
1,070
Total visitors
2,120

Forum statistics

Threads
2,276,368
Posts
28,969,446
Members
1,231,229
Latest member
boss_ej
Back
Top