Solved

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

Posted on 2016-08-16
5
79 Views
Last Modified: 2016-08-17
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();
0
Comment
Question by:Braveheartli
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 9

Expert Comment

by:Mukesh Yadav
ID: 41758942
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

0
 
LVL 1

Author Comment

by:Braveheartli
ID: 41758947
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) {
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 41758952
Every PHP script that connects to a MySQL database must have it's own connection code.  Why do you want to do without it?
0
 
LVL 29

Accepted Solution

by:
Olaf Doschke earned 500 total points
ID: 41758954
Since you also close the connection after being done, I actually doubt the connection already exists. The general reason for going through connecting before querying is, even though you have a Session, every request to a PHP script starts from scratch, you build up variables, objects and a MySQL Server connection to do things, then shut down. That's the nature of PHP, or any server side script.

One thing is correct, if you have a connection, you can also use it for multiple queries, like that:

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

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

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

$sql = "SELECT * FROM someothertable";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "something<br>";
    }
} else {
    echo "0 results";
}

$conn->close();

Open in new window

So in short, you simply reuse the still existing $conn variable to do another query. But that's only possible within the same PHP request, next time a browser request to your.php comes in, the $conn has to be redone, not only because you closed it, but since nothing lasts and stays but $_SESSION variables. And they can't persist a $conn, so don't even try $_SESSION['conn']=$conn;

For best practice you DRY (don't repeat yourself), so you put the connection into a PHP script you can require, also the general code to do a query could be made a script or even class. Obviously, the part of the code querying is very similar, and the query is just a parameter of $conn->query($sql). Only the inner part of what to do with the result differs, so a general routine would just return the result and not do anything with it. It seems you're at the point where you could use some knowledge about class design or at least structural programming, in detail user defined functions.

Bye, Olaf.
0
 
LVL 1

Author Closing Comment

by:Braveheartli
ID: 41758965
thank you Olaf,
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question