Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
Medium Priority
?
86 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 84

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 30

Accepted Solution

by:
Olaf Doschke earned 2000 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

618 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