PHP - Mysqli_fetch_array($result, Mysqli_assoc) While Loop Problem
Hey everyone I've been working on converting my old mysql code to to the mysqli version and am running into some trouble. I am pulling posts form phpbb and displaying them in a script. My first script below works just fine.
Code: [Select] <?php require("connect.php"); $query="SELECT * FROM phpbb_topics WHERE (forum_id='13' OR forum_id='14') AND topic_status = '0'"; $query2="SELECT * FROM phpbb_topics WHERE (forum_id='15' OR forum_id='16') AND topic_status = '0'"; $result=mysql_query($query); $result2=mysql_query($query2); echo "<table border=0 cellpadding=2>"; echo "<tr><td>Lost</td><td>Found</td></tr>"; while($row=(mysql_fetch_array($result)) || $row2=(mysql_fetch_array($result2))) { $forum_id=mysql_result($result,$k,"forum_id"); $topic_id=mysql_result($result,$k,"topic_id"); $topic_title=mysql_result($result,$k,"topic_title"); $forum_id2=mysql_result($result2,$k,"forum_id"); $topic_id2=mysql_result($result2,$k,"topic_id"); $topic_title2=mysql_result($result2,$k,"topic_title"); echo "<tr><td width='15%'><a href=/sahbb/viewtopic.php?f=$forum_id&t=$topic_id>$topic_title</a></td> <td width='15%'><a href=/sahbb/viewtopic.php?f=$forum_id2&t=$topic_id2>$topic_title2</a></td> </tr>"; $k++; } echo "</table>"; mysql_close($connect); ?> however its not very clean or memory efficient so i have converted it to the following. Code: [Select] <?php require("includes_database/mysqli_connect.php"); $query="SELECT * FROM phpbb_topics WHERE (forum_id='13' OR forum_id='14')"; $query2="SELECT * FROM phpbb_topics WHERE (forum_id='15' OR forum_id='16')"; $result=mysqli_query($dbc, $query); $result2=mysqli_query($dbc, $query2); echo "<table border=1 cellpadding=2>"; echo "<tr><td>Lost</td><td>Found</td></tr>"; while(($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) || ($row2 = mysqli_fetch_array($result2, MYSQLI_ASSOC))) { echo "<tr><td width='15%'><a href=/sahbb/viewtopic.php?f=".$row['forum_id']."&t=".$row['topic_id'].">".$row['topic_title']."</a></td> <td width='15%'><a href=/sahbb/viewtopic.php?f=".$row2['forum_id']."&t=".$row2['topic_id'].">".$row2['topic_title']."</a></td> </tr>"; } echo "</table>"; mysqli_free_result($result); mysqli_free_result($result2); mysqli_close($dbc); ?> I know the error is in my while loop but i cant seem to figure out exactly where. The new code displays all of the first query in the table and then below that it displays the next query insted of displaying them side by side below would be an example of what is happening [table Lost Found Lost, Australian Sheppard Lost German Shepherded / Collie Mix Lost Golden Retriever/Cocker Mix Lost Female Grey Tiger Lost Pitbull Mix Lost 2 Chihuahua Lost Black Lab Lost - German Shepard Mix and Lab Mix Lost - Female German Sheperd Aprox 8 Week Old Kitten Found! 2 Pugs FOUND Found 1 Corgi female/ 1 Shih Tzu male Found Tan and Black Australian Shepard Found Australian Shepard Found Intact Male Pitbull/Lab Mix Any help that any of you could provide would be greatly appreciated! Similar TutorialsI HOPE THIS IS IN THE CORRECT FORUM! I am developing a program that filters a MySQL database to choose a type of verse. I am using an HTML form with check boxes to select the type. The variable that is passed using $POST is a text string. I am using this string to query the database in the table Events to get all of the verses with that Event_Type. Using mysqli_fetch_array, I am then extacting the ID to use in another MySQL query, this time querying the Verses table to list the verses of this type. What is actually happening is that all the types up to 9 are displaying the correct result, but the ones with ID 10 to 19 are displaying verses with ID = 1 and the last 3 are displaying versed with ID = 2. So it is obvious that it is only looking at the first digit of the ID. How do you get it to recognise complete ID number? PHP Version 5.3.6 Here is the PHP code: Code: [Select] <?php include("Loc_cverse_connect.php"); doDB(); //check for required info from the query string //verify the Event exists $verify_Event_sql = "SELECT ID, Event_Type FROM Events WHERE Event_Type = '".$_POST["Event_Type"]."'"; $verify_Event_res = mysqli_query($mysqli, $verify_Event_sql) or die(mysqli_error($mysqli)); echo $_POST["Event_Type"]; if (mysqli_num_rows($verify_Event_res) < 1) { //this Event does not exist $display_block = "<p><em>You have selected an invalid Event.<br/> Please try again.</em></p>"; } else { //get the Event ID while ($Event_info = mysqli_fetch_array($verify_Event_res)) { $Event_ID = stripslashes($Event_info['ID']); } //gather the Events $get_Event_sql = "SELECT ID, Verse FROM Verses WHERE Event = '".$Event_ID["ID"]."' ORDER BY ID ASC"; $get_Event_res = mysqli_query($mysqli, $get_Event_sql) or die(mysqli_error($mysqli)); //create the display string $display_block = " <p> The Event Type is <b>" .$_POST["Event_Type"]."</b> </p> <table width=\"50%\" cellpadding=\"3\" cellspacing=\"1\" border=\"1\" BGCOLOR=\"#87CEEB\" > <tr> <th>ID</th> <th>VERSE</th> </tr>"; while ($Verse_info = mysqli_fetch_array($get_Event_res)) { $Event_id = $Verse_info['ID']; $Verse_text = nl2br(stripslashes($Verse_info['Verse'])); //add to display $display_block .= " <tr> <td width=\"1%\" valign=\"top\">".$Event_id."<br/></td> <td width=\"35%\" valign=\"top\">".$Verse_text."<br/><br/> </tr>"; } //free results mysqli_free_result($get_Event_res); mysqli_free_result($verify_Event_res); //close connection to MySQL mysqli_close($mysqli); //close up the table $display_block .= "</table>"; } ?> <html> <head> <title> List of Verses</title> </head> <body BGCOLOR="#87CEEB"> <h1>Verses</h1> <?php echo $display_block; ?> </body> </html> mysqlnd 5.0.8-dev - 20102224 - $Revision: 308673 $ MySQL Table Structu Event:- ID Event_Type Verses:- ID Event Sub_Type Verse Hi, I would like to ask, how to display the while loop result 3 per row with the following code below: $startDate = strtotime($datefrom); I am having a little bit of trouble with this piece of code. I'm sure it's something simple, but I have been working on this thing all day and want to get it finally finished. Here's the troublesome code: function rrmdir($dir) { if (is_dir($dir)) { $objects = scandir($dir); foreach ($objects as $object) { if ($object != "." && $object != "..") { if (filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object); else unlink($dir."/".$object); } } reset($objects); rmdir($dir); } } $sql_clean = "SELECT * complete WHERE createdate < date_sub(current_date, interval 1 minute)"; $sql_list = mysql_query($sql_clean); while($row = mysql_fetch_assoc($sql_list)) { $directory = "complete/" . $row['fileurl']; rrmdir($directory); } The purpose of this particular bit is to run on a cron every few days. It gets "createdate" and other info from the "complete" table in order to know how old the record is. If the record is older than (in the example, 1 minute; it will be set to several days on public) the defined max age, it removes that directory and everything within it to keep the directory clean and the disk usage down. The error returned is Quote Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home1/latenit2/public_html/kindleprocessor/process/garbagecleaner.php on line 46 Line 46 is Quote while($row = mysql_fetch_assoc($sql_list)) { I may be doing the look-up on the MySQL database incorrectly, too. I haven't discounted that, and I'd be thankful if someone could help me out with this issue. I want to save the results of a loop as a variable ie $output. I have tried encasing the php within quotes but it does not work. Is there a way to save the complete results as a variable? $result = mysql_query("SELECT * FROM $table2 WHERE $db_item_1 OR $db_item_2", $connection); if (!mysql_num_rows($result)) { echo "Error 13424 - not working"; exit(); } while ($row = mysql_fetch_array($result)) { echo "<tr><td scope=\"col\" style=\"font-size: 16px; color: #333333; padding: 10px 8px; border-bottom: 1px solid #919191;\"><div align=\"left\" style=\"font-size: 20px;\">" . $row['date'] . "</div></td> <td scope=\"col\" style=\"font-size: 16px; color: #333333; padding: 10px 8px; border-bottom: 1px solid #919191;\"><div align=\"left\"></div></td> <td scope=\"col\" style=\"font-size: 16px; color: #333333; padding: 10px 8px; border-bottom: 1px solid #919191;\"><div align=\"left\" style=\"font-size: 20px;\">" . $row['title'] . "</div></td> <td scope=\"col\" style=\"font-size: 16px; color: #333333; padding: 10px 8px; border-bottom: 1px solid #919191;\"><div align=\"left\"></div></td> <td scope=\"col\" style=\"font-size: 16px; color: #333333; padding: 10px 8px; border-bottom: 1px solid #919191;\"><div align=\"left\" style=\"font-size: 20px;\">" . $row['cost'] . "</div></td> </tr>"; }echo $complete;() The part I want as one result (ie. $complete) is the result of the while loop. There is always at least one result but sometimes 10 which means that it creates 10 table rows. I need to do it this way as later on in the page I use a pdf converter which does not allow loop checks within it otherwise I would just place it within the converter. I have only one result being displayed in a select query result whereas I'm expecting 4 results in an array. This is my code below. $sq2="SELECT course FROM course_reg WHERE userid=?"; $stm =$conn->prepare($sq2); $stm->bind_param("s",$logged); $stm->execute (); $return2= $stm->get_result(); $r2 = $return2->fetch_all(); //print_r($r2); foreach($r2 as $course){ foreach($course as $courses){ echo $courses; } }
If I do print_r($r2); it comes out with array containing all the possible results. When i loop through the array to get individual result, it only comes out with a single result. I.e CME211 I would be glad if you can help me figure where the issue is. Thanks!!!
Hello, New to PHP and trying to loop through rows to and get the name of the "property" in this case. I have tried foreach and while and neither seem to want to return all rows by calling the function. Does it have something to do with echo'ing the table structure or can I not return an array out of a function like this? I commented out the while loop as that wasn't working and put in a foreach loop though that didn't return all the data either. Any help is greatly appreciated Code: [Select] <?php function getProperties(){ $sql = "SELECT propertyName FROM properties;"; $result = mysql_query($sql); //$count = mysql_num_rows($result); $property = mysql_fetch_array($result); foreach($property as $propertyName){ echo "<td width=\"157\" height=\"24\" valign=\"middle\"><div align=\"left\"><input type=\"checkbox\" name=\"$propertyName\" \"id=\"$propertyName\" />$propertyName</div></td>"; } /* for($i=0;$i>=$count;$i++){ while($property = mysql_fetch_array($result)){ $propertyName = $property['propertyName']; echo "<td width=\"157\" height=\"24\" valign=\"middle\"><div align=\"left\"><input type=\"checkbox\" name=\"$propertyName\" \"id=\"$propertyName\" />$propertyName</div></td>"; } } */ } ?> this is only returning one record, when I know there are more, and it's an endless loop crashing my browser, can someone tell me how my brackets are wrong, or what is wrong? Code: [Select] if (isset($_POST['search'])){ $keyword=$_POST['keyword']; } $sql="SELECT id, fname, lname FROM obituaries WHERE lname LIKE '%$keyword%' OR fname LIKE '%$keyword%' ORDER BY id DESC"; $results = mysql_query($sql); $num = mysql_num_rows ($results); if ($num > 0 ) { $i=0; while ($i < $num) { $id = mysql_result($results,$i,"id"); $fname = mysql_result($results,$i,"fname"); $lname = mysql_result($results,$i,"lname"); ?> <a href="view.php?id=<?php echo ($id); ?>"><?php echo($fname); ?> <?php echo($lname); ?></a> <?php } } ?>Thanks in advance I think I put this in the wrong forum, as it seems to be more of a php problem (I think) I'm trying to populate a profile page with information from the database. Really bizarrely, every $row variable that isn't null returns as 'website', which is the database username. I have absolutely no idea why this is. It might be worth noting I have just changed hosts...the only thing I can think might make a difference. Any help would be great, thankyou $query = "SELECT * FROM member WHERE username='$member'"; $result = mysqli_query ($dbc, $query) or trigger_error ("query: $query\n<br />MySQL Error: " . mysqli_error($dbc)); if ($result) { while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) { $member_id = $row['member_id']; $username = $row['username_id']; $photo = $row['photo']; $about = $row['about']; $slink = $row['soundcloud_link']; $mlink = $row['mixcloud_link']; $flink = $row['facebook_link']; $dlink = $row['divshare_link']; $mylink = $row['myspace_link']; $plink = $row['postbocks_links']; $website = $row['personal_website']; $type = $row['member_type']; $status = $row['status']; } } } I've echo'd the $query and put the result into sql directly and it comes out fine. Hello, So I have only been using php for a few months now and am a noob. This question is probably an easy fix. My problem is that I have an array of info which is user stories they post on my site. I have a delete button for each story for the user to delete the associated story if they wish. The problem is that the delete button code deletes the last $row or story that was fetched! It seems that the variable updates through every loop and the code just deletes that id. Here is my code. Code: [Select] //page with delete button echo "<center><b>Stories Shared</b></center>"; $result = mysqli_query($cxn, "SELECT * FROM Stories WHERE User = '$name' ORDER BY ID DESC"); while($row = mysqli_fetch_array($result)) { if (empty($row)) { echo "<center>N/A</center>"; } $artpixname = $row['Artpixname']; echo "<hr />"; echo "<div id = 'info'>";; echo "ID: " . $row['ID']; echo "<b>Category: </b>" .$row['Type']. " "; $id = $row['ID']; echo "<FORM ACTION='delete.php' METHOD='POST'> <input type='hidden' name='ID' value='$id'> <INPUT TYPE = 'Submit' name='Delete' id='Delete' VALUE = 'Delete'>"; echo "<center><b><h3>" . $row['Name'] . "</h3></b></center> "; echo "</div>"; if (!empty($artpixname)) { echo "<center><img class = 'artimg' src = 'articles/images/".$row['Artpixname']."'></center>"; } echo "<br />"; echo "<br />"; echo $row['Article']; echo "<br />"; echo "<br />"; //now the delete button opens up the delete program here... <?php session_start(); include 'header.php'; include 'connection.php'; $id = $_POST['ID']; $name = $_SESSION['logname']; $result = mysqli_query($cxn, "SELECT ID FROM Stories WHERE ID = '$id'"); while($row = mysqli_fetch_array($result)) { $sql = mysqli_query($cxn, "DELETE FROM Stories WHERE ID = '$id'"); } echo "Your story was deleted successfully!"; echo "<br />"; echo "Click <a href = 'userpage.php?'.$name'><b>Here</b></a> to return to your profile page."; ?> What am I doing wrong? I tried everything including different variables and even putting the delete query in the while loop. Any help would be appreciated thanks! Lance MOD EDIT: [code] . . . [/code] tags added. Hi Guys Using mysqli_fetch_array(), which i had anticipated would simply return a numeric array with all the results from the simple query. However, it keeps coming out as a multidimensional array, which isn;t what i want. Is this just how it works or have i done something wrong? Code Code: [Select] $mysqli = new mysqli("localhost", "root", "", "ik"); if ($mysqli->connect_errno) { echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; } $qry = "SELECT amount FROM teams"; $result = $mysqli->query($qry); while($row = mysqli_fetch_array($result, MYSQLI_NUM)) { $rows[] = $row; //echo $row . "<br/>"; } print_r($rows); Result Code: [Select] Array ( [0] => Array ( [0] => 3 ) [1] => Array ( [0] => 25 ) [2] => Array ( [0] => 26 ) [3] => Array ( [0] => 31 ) [4] => Array ( [0] => 34 ) [5] => Array ( [0] => 44 ) [6] => Array ( [0] => 50 ) [7] => Array ( [0] => 56 ) [8] => Array ( [0] => 65 ) [9] => Array ( [0] => 221 ) [10] => Array ( [0] => 222 ) [11] => Array ( [0] => 225 ) [12] => Array ( [0] => 2210 ) [13] => Array ( [0] => 2600 ) ) Hi guys, I received an error, could anyone explain this current error? Thanks Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in D:\inetpub\vhosts\championtutor.com\httpdocs\questionnaire.php on line 32 Hello im geting this error what im ding wrong? Code: [Select] PHP Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /home/admincom/public_html/tsue/library/plugins/movie_plugin.php on line 7 line 7 is: while ($row = mysqli_fetch_array($sql)) { code he Code: [Select] $query = mysqli_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS, MYSQL_DB); $sql = mysqli_query($query, "SELECT `membername`, `filename`, `tid`, `info_hash`, `name`, `description`, `cid`, `size`, `added`, `leechers`, `seeders`, `times_completed`, `owner`, `options`, `nfo`, `sticky`, `flags`, `mtime`, `ctime`, `download_multiplier`, `upload_multiplier` FROM `tsue_members`, `tsue_torrents`, `tsue_attachments` WHERE `memberid`='owner' AND `content_type`='torrent_images' AND `content_id` = `tid` LIMIT 0, 10"); while ($row = mysqli_fetch_array($sql)) { $movie_plugin_row = ''; $uploader = $row['membername']; $description= $row['description']; $dydis= $row['size']; $name= $row['name']; $leechers= $row['leechers']; $owner= $row['owner']; $filename= $row['filename']; $nunx= $row['tid']; $seeders= $row['seeders']; eval("\$movie_plugin_row = \"".$TSUE['TSUE_Template']->LoadTemplate('movie_plugin_row')."\";"); $movie_plugin .= $movie_plugin_row; } Hi
I am a student who is fairly new to PHP and MySQL. I have been working on creating a registration page for a website and I'm getting the following warnings when I've tested the page:
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /home/ed12e2w/public_html/COMM2735/dynamic_website/registration.php on line 74 Warning: mysqli_free_result() expects parameter 1 to be mysqli_result, boolean given in /home/ed12e2w/public_html/COMM2735/dynamic_website/registration.php on line 80 I think that my query has failed but I'm not completely sure on what to change in order to solve this. Here is the section of code I'm having problems with: Attached Files register.php 733bytes 4 downloads I'm trying to turn this while loop into a for loop and am unable to get my result set to display properly in the for loop. The while works fine I just want to be able to have more control over which information is shown in my table as I loop and was wanting to use a for loop that way I can take advantage of the counter variable while i"m displaying my information. Any help would be appreciated. while ($row = mysql_fetch_assoc($data_result_set)) { echo "<td>".$row["product_id"]."</td>"; echo "<td>".$row["city"]."</td>"; echo "<td>".$row["quantity"]."</td>"; } *** I'm wanting it to look like something like this but can't figure out how to properly work in which row to display with the $i variable. $count=mysql_num_rows($data_result_set); for($i = 0; $i <= $count; $i++){ echo "<td>".mysql_fetch_assoc[$i]["product_id"]."</td>"; echo "<td>".mysql_fetch_assoc[$i]["city"]."</td>"; echo "<td>".mysql_fetch_assoc[$i]["quantity"]."</td>"; } I know the syntax for the for loop is totally off with the method mysql_fetch_assoc just dropped in there like a jerk but I'm just kinda pseudoing it out. Any help would be appreciated. Thanks in advance. Someone help me in this problem please?? Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in line 8 $rec= mysqli_query ($db, "SELECT FROM joborder WHERE id=$id"); $record = mysqli_fetch_array ($rec); // line 8 $fnames= $record ['fnames'] ; Edited March 8 by keii cannot figure out why my first result is not displaying properly it successfully shows all results but me first myurl.com/other.php?id=2 now showing any thing kindly help to solved this problem! it seems like problem is in variable $http$ids but dunno how to combine them to get result $Ids always b numeric number <?php ini_set ("display_errors", "1"); error_reporting(E_ALL); $http = "myurl.com/other.php?id=" ; $conn=odbc_connect('greeting','',''); if (!$conn) {exit("Connection Failed: " . $conn);} $sql="SELECT * FROM mytable"; $rs=odbc_exec($conn,$sql); if (!$rs) {exit("Error in SQL");} echo "<table border= 1><tr>"; echo "<th>ID</th>"; echo "<th>Like</th>"; echo "<th>title</th></tr>"; while (odbc_fetch_row($rs)) { $ids=odbc_result($rs,"ID"); $title=odbc_result($rs,"title"); echo "<tr><td>$ids</td>"; echo "<td><fb:like href=\"$http$ids\" layout=\"button_count\" show_faces=\"false\" width=\"100\" font=\"tahoma\" colorscheme=\"dark\"></fb:like> </td>"; echo "<td>$title</td></tr>"; } echo "</table>"; odbc_close($conn); ?> Hello guys I have here my array result Array ( => Array ( => 1 [1] => [2] => 123456789 [3] => [4] => 2012-02-02 [5] => 09:12:21 [6] => Test Array ) ) I would like to view this result into a string so that it will remove the unnecessary array elements like brackets etc. Can some tell me how to handle this properly? The result would be : 1 123456789 2012-02-02 09:12:21 thanks Hi, Having trouble rounding the output. I would need to be sure the output is not longer than 2 spaces after comma. Any ideas? My code Code: [Select] <script type="text/javascript"> $(document).ready(function() { $("<?php echo "#".$input_end_time; ?>").blur(function(){ var start_time = $("<?php echo "#".$input_start_time; ?>").val(); var end_time = $("<?php echo "#".$input_end_time; ?>").val(); $.getJSON("include/timedifferencecalculation.php?s="+start_time+"&e="+end_time, function(data){ var success = data.success; if (success == 1) { $("<?php echo "#".$input_difference; ?>" ).val(data.hours + " h " + data.minutes + " m "); if (data.hours + data.minutes/60 >= 6){ $("<?php echo "#".$hours_difference; ?>").val(data.hours + data.minutes/60 - 0.5); }else { $("<?php echo "#".$hours_difference; ?>").val(data.hours + data.minutes/60); } }else { $("#message").text("Viga! " + data.reason); } }) }); }); </script> Code: [Select] This is before $("<?php echo "#".$hours_difference; ?>").val(data.hours + data.minutes/60 - 0.5); This is what im trying to get but its not working. $("<?php echo "#".$hours_difference; ?>").val( round(data.hours + data.minutes/60 - 0.5), 2 ); |