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
72 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 8

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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 …

809 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