Link to home
Start Free TrialLog in
Avatar of BR
BRFlag for Türkiye

asked on

I have already a connection, I do not want to open another connection to execute to mysql query.

I have already a connection, I do not want to open another connection to execute to mysql query.

How can I rewrite the mysql script and php to execute the query?

// Create connection ( I already have one )
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

//how can rewrite below code to execute the sql qurey without above connection script?
$sql = "SELECT isim FROM KullaniciBilgileri where uid='$uid'";
$result = $conn->query($sql);

// after successful connection and execution of sql I have this code below to write the results.

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["uid"]. " - İsim: " . $row["pwd"]. " " . $row["isim"]. "<br>";
    }
} else {
    echo "0 results";
}
$conn->close();
Avatar of Mukesh Yadav
Mukesh Yadav
Flag of India image

If $conn already exists then use it as you are doing right now.

Just remove following code which connects to database:
// Create connection ( I already have one )
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

Open in new window

Avatar of BR

ASKER

I already did this, but it didn't work, because I use $conn variable in the if statement.
I think we should rewrite the if statement and the mysql sentence below

$sql = "SELECT isim FROM KullaniciBilgileri where uid='$uid'";
$result = $conn->query($sql);

// after successful connection and execution of sql I have this code below to write the results.

if ($result->num_rows > 0) {
Every PHP script that connects to a MySQL database must have it's own connection code.  Why do you want to do without it?
ASKER CERTIFIED SOLUTION
Avatar of Olaf Doschke
Olaf Doschke
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BR

ASKER

thank you Olaf,