Avatar of Ridgejp
Ridgejp
Flag for United Kingdom of Great Britain and Northern Ireland

asked on 

Tweaked MySql Connection - Now Select Query's Fail

Hi Guys'

Yes it me again ...

I've updated some of php pages to include an access control function which works great - super pleased about that! However, the code I've used seems to conflict with select queries I've previously written.

Can you tell me what causes the problem please if I post some examples below?

Previously my config.php file looked like this: -

<?php


	/* Access to the databse via link commands requires the $link statement
	in order for them to be successful, also the term "localhost" stops the $link
	failing in the event of a change in your IP Address.*/
	
	$link = mysqli_connect("localhost" , "user", "password", "DbName");


?>

Open in new window


Now it includes some access control functions and the config file looks like this:

// CONNECTION AND SELECTION VARIABLES FOR THE DATABASE
$db_host = "localhost";
$db_name = "DbName";
$db_user = "user";
$db_word = "password";

// OPEN A CONNECTION TO THE DATA BASE SERVER AND SELECT THE DB
$mysqli = new mysqli($db_host, $db_user, $db_word, $db_name);

// DID THE CONNECT/SELECT WORK OR FAIL?
if ($mysqli->connect_errno)
{
    $err
    = "CONNECT FAIL: "
    . $mysqli->connect_errno
    . ' '
    . $mysqli->connect_error
    ;
    trigger_error($err, E_USER_ERROR);
}

Open in new window


The first config allowed the following select queries to run: -

<?php
	
	$query = "select `partID`, `partDescription`, `qtyInStock`, `sales42D`,	`sales14D`, `supplierCompany`, `itemCost` FROM inventory";
	
	if ($result=mysqli_query($link, $query)) {
	
		while ($row = mysqli_fetch_array($result)) {
		
	?>
			<tr>
			<td><?php echo $row['partID'];?></td>
			<td><?php echo $row['partDescription'];?></td>
			<td><?php echo $row['qtyInStock'];?></td>
			<td><?php echo $row['sales42D'];?></td>
			<td><?php echo $row['sales14D'];?></td>
			<td><?php echo $row['supplierCompany'];?></td>
			<td><?php echo $row['itemCost'];?></td>
			</tr>
						
<?php
		}
	
	} else {
	
		echo "It failed!";
	
	}
?>

Open in new window


The 2nd config files causes the connection to fail ... what am I doing wrong?

Thanks J
MySQL ServerPHP

Avatar of undefined
Last Comment
Ray Paseur

8/22/2022 - Mon