0

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









?>
2
Contributors
3
Replies
28
Views
1 Year
Discussion Span
Last Post by cereal
0

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.

Edited by cereal

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.