Jump to content

Recommended Posts

I have read and re-read everything I can find. I could re-write my whole site but that doesn't seem practical. I am trying to get this old piece of code to work within the new standard and just can't quite grasp it I guess... I am an amateur at this I know, but any help would be appreciated... Thanks

 

<?PHP

include_once 'db.php';
include_once 'header.html';

$sql = mysqli_query($dbc,
"ALTER TABLE inven ORDER BY cat ASC, descrip ASC, brand ASC");
echo "<br><hr>";
$deflist=mysqli_query($dbc,
        "SELECT quant, brand, descrip, size, flavor, cat FROM inven");
while ($all = mysqli_fetch_array($deflist)) {
   $results[$all['cat']][] = array ('quant' => $all['quant'], 'brand' => $all['brand'], 'descrip' => $all['descrip'], 'size' => $all['size'], 'flavor' => $all['flavor']);
}

//print_r($results);


foreach ($results as $catName => $catData)
{
   print('<b><br><font face=tahoma size=4>'.$catName.'</b><br/></font>'."\n");
   foreach ($catData as $itemNum => $itemData)
   {
      // if you want to access the row data in this loop, use the following method:
      print($itemData['descrip'].', '.$itemData['brand'].', '.$itemData['size'].', '.$itemData['flavor'].' - '.$itemData['quant'].'<br/>'."\n"); // etc. (you must code the field names in hard coded this way)
      //foreach ($itemData as $fieldName => $value)
      
   }
}

include_once 'footer.html';

?> 
Link to comment
https://forums.phpfreaks.com/topic/282367-mysqli-what-am-i-doing-wrong/
Share on other sites

Here are the errors:

Notice: Undefined variable: results in C:\xampp\htdocs\report1.php on line 18

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\report1.php on line 

 

Here is the db.php stuff

 

<?php

$dbc = new mysqli("127.0.0.1", "root", "abc1234", "supplies");
if ($dbc->connect_errno) {
    echo "Failed to connect to MySQL: (" . $dbc->connect_errno . ") " . $dbc->connect_error;
}


//if (mysqli_ping($dbc))
//{echo 'MySQL Server ' .mysqli_get_server_info($dbc) .
//                      ' on ' . mysqli_get_host_info($dbc);
//
//}
?>

I tested the connection with a ping and it returned the right printed info... Just getting the mysqli changed seems to be where I get hung up.   Thanks again.

In your first post the code looks ok. I can't see any errors. The problem is your query does not appear to returning any results. This is why the foreach loop is returning an error on line 18.

 

You should check that your query returned any results using  mysqli_num_rows before processing the results on line 18. For example:

// check that the query returned any results
if(mysqli_num_results($all))
{
	// process results
	foreach ($results as $catName => $catData)
	{
	   print('<b><br><font face=tahoma size=4>'.$catName.'</b><br/></font>'."\n");
	   foreach ($catData as $itemNum => $itemData)
	   {
	      // if you want to access the row data in this loop, use the following method:
	      print($itemData['descrip'].', '.$itemData['brand'].', '.$itemData['size'].', '.$itemData['flavor'].' - '.$itemData['quant'].'<br/>'."\n"); // etc. (you must code the field names in hard coded this way)
	      //foreach ($itemData as $fieldName => $value)
	      
	   }
	}
} 
// no results found, display a message or error
else 
{
	echo 'No results found! Possibly an error:' . mysqli_error($dbc);
}
Edited by Ch0cu3r
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.