Mysqli connection and query

I am new to mysqli and went through a tutorial: http://www.binpress.com/tutorial/using-php-with-mysql-the-right-way/17#comment1

I managed to connect to my database using this:

$config = parse_ini_file('../config.ini'); $connection = mysqli_connect('localhost',$config['username'],$config['password'],$config['dbname']); if($connection === false) { die('Connection failed [' . $db->connect_error . ']'); } echo("hello"); //this worked! 

But then I tried to wrap it in a function (as discussed in the tutorial) ... I saw that you call the join function from another function ... in the tutorial, each function continues to receive a call from the other and the other. and I never found where the initial call came from in order to get a domino effect from functions that call each other. Somehow, I tried to stop him for two, just to test and teach myself .. but it doesnโ€™t work, and I donโ€™t know why:

 function db_connect() { static $connection; if(!isset($connection)) { $config = parse_ini_file('../config.ini'); $connection = mysqli_connect('localhost',$config['username'],$config['password'],$config['dbname']); } if($connection === false) { return mysqli_connect_error(); } return $connection; echo("hello2"); } function db_query($query) { $connection = db_connect(); $result = mysqli_query($connection,$query); return $result; echo("hello1"); } db_query("SELECT `Q1_Q`,`Q1_AnsA` FROM `Game1_RollarCoaster`"); //this didn't work :( 
+5
source share
2 answers

Here is a simple mysqli solution for you:

 $db = new mysqli('localhost','user','password','database'); $resource = $db->query('SELECT field FROM table WHERE 1'); $row = $resource->fetch_assoc(); echo "{$row['field']}"; $resource->free(); $db->close(); 

If you grab a few lines, I do it like this:

 $db = new mysqli('localhost','user','password','database'); $resource = $db->query('SELECT field FROM table WHERE 1'); while ( $row = $resource->fetch_assoc() ) { echo "{$row['field']}"; } $resource->free(); $db->close(); 

With error handling: If there is a fatal error, the script exits with an error message.

 // ini_set('display_errors',1); // Uncomment to show errors to the end user. if ( $db->connect_errno ) die("Database Connection Failed: ".$db->connect_error); $db = new mysqli('localhost','user','password','database'); $resource = $db->query('SELECT field FROM table WHERE 1'); if ( !$resource ) die('Database Error: '.$db->error); while ( $row = $resource->fetch_assoc() ) { echo "{$row['field']}"; } $resource->free(); $db->close(); 

With exception handling try / catch:. This allows you to handle any errors in one place and possibly continue execution if something fails, if necessary.

 try { if ( $db->connect_errno ) throw new Exception("Connection Failed: ".$db->connect_error); $db = new mysqli('localhost','user','password','database'); $resource = $db->query('SELECT field FROM table WHERE 1'); if ( !$resource ) throw new Exception($db->error); while ( $row = $resource->fetch_assoc() ) { echo "{$row['field']}"; } $resource->free(); $db->close(); } catch (Exception $e) { echo "DB Exception: ",$e->getMessage(),"\n"; } 
+2
source

Well, I ended up not using functions and made the code very simple (adhering to procedural rather than OOP, although many textbooks use OOP - they thought it was better to start this way):

 <?php $config = parse_ini_file('../config.ini'); $link = mysqli_connect('localhost',$config['username'],$config['password'],$config['dbname']); if(mysqli_connect_errno()){ echo mysqli_connect_error(); } $query = "SELECT * FROM Game1_RollarCoaster"; $result = mysqli_query($link, $query); while ($row = mysqli_fetch_array($result)) { echo $row[Q1_Q] . '<-- Here is your question! ' . $row[Q1_AnsA] . '<-- Here is your answer! '; echo '<br />'; } mysqli_free_result($result); mysqli_close($link); ?> 
+1
source

Source: https://habr.com/ru/post/1214845/


All Articles