6

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

I was able 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 wrapping it in a function (as discussed in the tutorial)... I saw that you call the connection function from another function... in the tutorial each function keeps getting called from another and another... and I never quite found where the initial call started from to get the domino effect of functions calling eachother.. so anyway, I tried to stop it at two just to test and teach myself.. but it's not working 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 :( 
6
  • 1
    It's a scope issue. Pass the connection variable in your functions. Commented Mar 6, 2015 at 16:34
  • I used scope this morning Ralph, but the coffee has wiped out any notion that I did. @Fred-ii- ;-) Commented Mar 6, 2015 at 16:35
  • @JayBlanchard You sure you didn't have a double espresso? Oh wait, I did. Silly Sam I am! Commented Mar 6, 2015 at 16:36
  • 1
    Here, have a read stackoverflow.com/q/7452989 about scope, unlike what @JayBlanchard 's using. It's not strong enough to "cope" with his Scope. Commented Mar 6, 2015 at 16:40
  • Fred (or Jay), can you give me an example how to re-write it? The link you sent about scope says to use the "global" keyword..... I'm still not sure how that get's the db_query function running in order to call the db_connect function... Commented Mar 6, 2015 at 16:52

2 Answers 2

4

Well I ended up taking it out of the functions and made the code super simple (sticking with procedural instead of OOP even though a lot of tutorials use OOP - 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); ?> 
Sign up to request clarification or add additional context in comments.

Comments

2

Here's 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're grabbing more than one row, 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 will terminate 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 try/catch exception handling: This lets you deal with any errors all in one place and possibly continue execution when something fails, if that's desired.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.