phpstuck Posted September 22, 2013 Share Posted September 22, 2013 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'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/282367-mysqli-what-am-i-doing-wrong/ Share on other sites More sharing options...
litebearer Posted September 22, 2013 Share Posted September 22, 2013 1. what error(s) are you getting? 2. please show your db.php sans username/password Quote Link to comment https://forums.phpfreaks.com/topic/282367-mysqli-what-am-i-doing-wrong/#findComment-1450731 Share on other sites More sharing options...
.josh Posted September 22, 2013 Share Posted September 22, 2013 3. Explain what the actual problem is. Quote Link to comment https://forums.phpfreaks.com/topic/282367-mysqli-what-am-i-doing-wrong/#findComment-1450735 Share on other sites More sharing options...
phpstuck Posted September 22, 2013 Author Share Posted September 22, 2013 Here are the errors:Notice: Undefined variable: results in C:\xampp\htdocs\report1.php on line 18Warning: 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. Quote Link to comment https://forums.phpfreaks.com/topic/282367-mysqli-what-am-i-doing-wrong/#findComment-1450736 Share on other sites More sharing options...
Ch0cu3r Posted September 23, 2013 Share Posted September 23, 2013 (edited) 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 September 23, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/282367-mysqli-what-am-i-doing-wrong/#findComment-1450811 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.