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
61 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
5 Comments
 
LVL 6

Expert Comment

by:Mukesh Yadav
Comment Utility
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
Comment Utility
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 82

Expert Comment

by:Dave Baldwin
Comment Utility
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
Comment Utility
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
Comment Utility
thank you Olaf,
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now