I have the code . It is not properly showing output

<?php
session_start();




$servername = "localhost";
$username = "root";
$password = "";
$dbname = "forextrading";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname );
$link = mysqli_connect($servername, $username, $password, $dbname);




// Check connection
if (mysqli_connect_errno()) {
  //echo "Failed to connect to MySQL: escape string connection    " . mysqli_connect_error();
}else{
  //echo "escape string connection works properly";  
}


// Check connection
if ($conn->connect_error) {
    //die("Connection failed: " . $conn->connect_error);
}

//echo "Connected successfully";






 //$Email =$_POST["Email"];
 //$Password=$_POST["Password"];


$rand="Zen1710";
//$rand= mysql_real_escape_string($rand);
//echo "<br>"."1st one".$rand."<br>";

$rand=mysqli_real_escape_string($link, trim($rand) );
echo "<br>"." 2nd ".$rand."<br>";

//$rand=var_dump($rand);

//echo "<br>"."3rd".$rand."<br>";


$sql = "SELECT ID FROM userinfo WHERE RandomID='$rand' ";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        echo "id: " . $row["ID"]. "<br>";
    }
} else {
    echo "0 results";
}









?>

Dani AI

Generated

Good catch by cereal: mixing $conn (OO) and $link (procedural) is a common source of silent failures. Two practical points to add:

  • mysqli_real_escape_string() must use the same connection (and character set) as the query. Also, it only changes characters like quotes, backslashes, and NUL. Your test value Zen1710 has none of those, so escaping will not modify it. If you still get 0 results, the issue is the data or the WHERE clause, not escaping.

  • Drop the second connection and stop manual escaping for values. Use one connection and a prepared statement. Enable strict error reporting and set the charset up front so you see real errors instead of guessing.

Example (object-oriented mysqli, single connection, no manual escaping):

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

$conn = new mysqli('localhost', 'root', '', 'forextrading');
$conn->set_charset('utf8mb4');

$rand = 'Zen1710';

$stmt = $conn->prepare('SELECT ID FROM userinfo WHERE RandomID = ?');
$stmt->bind_param('s', $rand);
$stmt->execute();
$res = $stmt->get_result();

if ($res->num_rows === 0) {
    echo '0 results';
} else {
    while ($row = $res->fetch_assoc()) {
        echo 'id: ' . $row['ID'] . '<br>';
    }
}

If it still says 0 results, verify that userinfo.RandomID actually contains Zen1710 (no trailing spaces), that you are connected to the expected database, and that column and table names are exact. If RandomID was created as a fixed-length CHAR and padded, either switch to VARCHAR or trim the stored data. You can quickly sanity-check with SELECT COUNT(*) FROM userinfo WHERE RandomID = 'Zen1710'; in your SQL client.

Recommended Answers

All 3 Replies

Hi, you're mixing object $conn with procedural $link and opening two indipendent connections to the database, if you want to stick with procedural, then change:

$result = mysqli_query($conn, $sql);

to:

$result = mysqli_query($link, $sql);

and then it should work properly.

it doesn't work

Then add error checking to the connection and to the query:

$link = @mysqli_connect($servername, $username, $password, $dbname);

if ( ! $link)
    die('Connect Error: ' . mysqli_connect_error());

# . . .

if( ! $result = mysqli_query($link, $sql))
    echo mysqli_error($link);

if you don't get any error, then post the updated script.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.