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";
}









?>

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.