Jump to content

Connecting With Mysqli


niche

Recommended Posts

This script works with require_once and produces warnings for line 6 when I try to use $mysqli = new... (which I've commented out). Please help my with this script:

<?phprequire_once "connect_to_mysql.php";//$mysqli = new mysqli("localhost","user","pw","db");$result = mysql_query("SELECT * FROM example") or die(mysql_error());  echo "<table border='1'>";echo "<tr> <th>Name</th> <th>Age</th> </tr>";while($row = mysql_fetch_array( $result )) {	echo "<tr><td>"; 	echo $row['name'];	echo "</td><td>"; 	echo $row['age'];	echo "</td></tr>"; } echo "</table>";//mysqli_close($mysqli);?>             

Thank-you

Link to comment
Share on other sites

I'm not 100% sure but you may need to be consistent with using mysql vs. mysqli commands - I'm not sure you can swap in and out over the same connection. I only use mysqli but I think they have slightly different behaviours when it comes to connecting.The way I do it is:in my db_include.php file:

function doDB() {  global $mysqli;    //connect to server and select database  $mysqli = mysqli_connect(host, username, password, db name);    //if connection fails, stop script execution  if(mysqli_connect_errno()) {	error_log("Connect failed: %s\n", mysqli_connect_error());	exit();  }}

in the script:

<?phpinclude_once("db_include.php");doDB();$query = "SELECT * FROM EXAMPLE";$result = mysqli_query($mysqli, $query) or die(mysqli_error($mysqli));echo "<table border='1'>";echo "<tr> <th>Name</th> <th>Age</th> </tr>";while($row = mysql_fetch_array( $result )) {	echo "<tr><td>"; 	echo $row['name'];	echo "</td><td>"; 	echo $row['age'];	echo "</td></tr>";} echo "</table>";mysqli_close($mysqli);?>

Link to comment
Share on other sites

I'm not 100% sure but you may need to be consistent with using mysql vs. mysqli commands - I'm not sure you can swap in and out over the same connection. I only use mysqli but I think they have slightly different behaviours when it comes to connecting.
I think I need to work something like this: $result = $mysqli->query($query); into line six, but I'm not sure and don't know how, or have a reference to work from.
Link to comment
Share on other sites

Well, I use this mysqli class to connect and it works perfectly:

<?php//Database connection values$db = array (	'host' => "localhost",	'user' => "root",	'pass' => "",	'dbname' => "game");//Make class$mysqli = new mysqli($db['host'], $db['user'], $db['pass'], $db['dbname']);//Connect.... if fails it gives a warningif(mysqli_connect_errno()){	trigger_error('MySQLi connection error: '.$mysqli->error);}?>

in my scripts I use the following code to select things from my database:

<?php//Make query$sql = "SELECT * FROM test_table WHERE test='1'";//Activate query and give error on failif(!$Tests = $mysqli->query($sql)){  trigger_error('Error in query: '.$mysqli->error);}//Query succeeded else{  //Show every row that is given from the test_table  while($test= $Test->fetch_assoc())  {	echo $test['test_name']."<br />";  }}?>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...