PHP - Mysql_fetch_array
Hi. I just have a quick question. If I perform a mysql query like the one below that returns all of the rows in a table. How do I then add all of this data into an array. If I use the following code bellow the variable $array will only contain the first row of the table.
$rs = mysql_query($query, $conn); $array = mysql_fetch_array ($rs); Similar TutorialsHere is my code Code: [Select] // Show the events for this day: $getEvent_sql = "SELECT cal_title, cal_description, date_format(cal_start_time, '%l:%i %p') as fmt_date FROM calendar WHERE month(cal_start_time) = '".$m."' AND dayofmonth(cal_start_time) = '".$d."' AND year(cal_start_time)= '".$y."' ORDER BY cal_start_time"; $getEvent_res = myload($getEvent_sql); echo "<pre>"; print_r($getEvent_res); echo "</pre>"; if (count($getEvent_res) > 0){ $event_txt = "<ul>"; while($ev = mysql_fetch_array($getEvent_res)){ echo "Inside"; echo $ev; $event_title = stripslashes($ev["cal_title"]); $event_shortdesc = stripslashes($ev["cal_description"]); $fmt_date = $ev["fmt_date"]; $event_txt .= "<li><strong>".$fmt_date."</strong>: ".$event_title."<br/>".$event_shortdesc."</li>"; echo $event_title; } $event_txt .="</ul>"; mysql_free_result($getEvent_res); } else { $event_txt = ""; } if ($event_txt != ""){ echo "<p><strong>Today's Events:</strong></p> $event_txt <hr/>"; } I can't get inside the while loop. The array is populated, I've checked that. The count of $getEvent_res = 3. echo "Inside", and echo $ev give me nothing. Any ideas what I'm doing wrong? $getEvent_res = myload($getEvent_sql); The myload is a custom function that just works, I didn't write it, but I know it works. Hi everyone, Bit of a random question and im sure there is good reason so if someone could in lighten me that would be great ! With regards to the below code, it seems to just create an infinite loop when you put mysql_fetch_array. Just want to know what wrong with doing this? and why it act in this way ? Code: [Select] $sql = mysql_query("SELECT * FROM cities"); $results = mysql_fetch_array($sql); while($row = $results) { echo $row['City']."<br />"; } thanks Guys Hello if some one be nice to help me out it be most kind... i got this error "Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in/home/23url/public_html/new.php on line 66" the code is - $getbans =mysql_query("SELECT banned FROM $tab[banned];"); $bans = array(); LINE 66 > while($ban=mysql_fetch_array($getbans)) { array_push($bans, $ban[0]); } many thanks This is a pain. It seems totally logical to me and I had it working before I accidentally saved over the working version (major boo boo). This is two code snippets, first one is how I want it to work - One Query and the loop through results. Second does work, but then its going to have to run mysql 60 times, which could get slow as the site gets more use. $results = mysql_query($query); $response .= "<table><tr>"; if($nit == 0){ for($i = 0; $i < 60; $i++){ $dateCompare = date("Y-m-d", mktime(0,0,0,date("m"),date("d")+$i,date("Y"))); $dateForString = date("D d/m/Y", mktime(0,0,0,date("m"),date("d")+$i,date("Y"))); $response .= "<td height='350px' style='color:white; background-color:grey;'>$dateForString</td>"; while($row = mysql_fetch_array($results)){ $id = $row['id']; $poster = $row['poster']; $date = $row['date']; $response .= "$date : $dateCompare<br>"; if($date == $dateCompare){ $response .= "<td><a href='fullinfo.php?selected_id=$id'><img src='$poster' height='350px' /></a></td>"; } } } } FOLLOWED BY THE SECOND $response .= "<table><tr>"; if($nit == 0){ for($i = 0; $i < 60; $i++){ $dateCompare = date("Y-m-d", mktime(0,0,0,date("m"),date("d")+$i,date("Y"))); $dateForString = date("D d/m/Y", mktime(0,0,0,date("m"),date("d")+$i,date("Y"))); $response .= "<td height='350px' style='color:white; background-color:grey;'>$dateForString</td>"; $results = mysql_query($query); while($row = mysql_fetch_array($results)){ $id = $row['id']; $poster = $row['poster']; $date = $row['date']; if($date == $dateCompare){ $response .= "<td><a href='fullinfo.php?selected_id=$id'><img src='$poster' height='350px' /></a></td>"; } } } } First one results in -it seems - to only go through the while loop once, the first time. So out put is todays date, for the number of results from the database. The second works fine, prints out in order and matches up. I just don't like its inefficiency. Thanks Canty Hey, I have been having a problem with this code for a while now where I connect to the MySQL database to retrieve the desired information, but always get an error message. I have tried to correct it myself by changing bits of the code and searched the internet, but still have no idea how to fix it. Any help would be appreciated. The error is as follows: Notice: An error occured1: in <filelocation> on line 18 Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in <filelocation> on line 21 Notice: An error occured2: in <filelocation> on line 15 My code as is follows: require_once('../mysqli_connect.php') ; $query = "SELECT * FROM database WHERE mem_id='".($_GET['id'])."'"; $result = @mysql_query($dbc, $query) OR trigger_error("An error occured1: " . mysql_error()); $row = mysql_fetch_array($result) OR trigger_error("An error occured2: " . mysql_error()); echo "<b>Date: </b> ".$row['date']; echo "<br><b>ID: </b> ".$row['mem_id']; Thanks. I do a query and then do a while loop to go threw it, but later down the page I need to loop threw that data again, but nothing is there. Example while ($row = mysql_fetch_array($sqlmaps)) { //some code } //some code while ($row = mysql_fetch_array($sqlmaps)) { //it dose not work here now } Do need to do that exact same query 2 times to get the data? Hi All, I am sorry to bother you but wondered if you might be able to spot my mistake... When I click on this page http://www.freepspwallpaper.co.uk/free-psp-wallpapers/free-babes-psp-wallpapers/ if returns an error as below (look carefully its hidden behind the header color). Im a bit of a newbie and cannot seem to remove it. Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/freepspw/public_html/dbconnect.php on line 22 Can anyone help? Peter I am having a problem with my script not returning anything! I echo the variable usually it will say ARRAY and it has something in it.. Nothing at all it coming up no errors I have put or die in it, nothing. I tried error_reporting(E_ALL); , still nothing. If someone can help me with this, I will greatly appreciate it. ~AJ error_reporting(E_ALL); //Get Catagorys and echo $getCatagorys = mysql_query("SELECT * FROM `catagory`") or die("Query failed with error: ".mysql_error()); while($row = mysql_fetch_array($getCatagorys, MYSQL_ASSOC)){ echo ' <tr> <td class="catagoryTD" width="550">'.$row['catagoryTitle'].'</td> <td class="catagoryTD" width="75"><center>Threads</center></td> <td class="catagoryTD" width="75"><center>Posts</center></td> <td class="catagoryTD" width="170"><center>Last Post</center></td> </tr>'; //Get SubCatagorys and echo $catagoryID = $row['catagoryID']; $getSubCatagory = mysql_query("SELECT * FROM `subcatagory` where catagoryID = '$catagoryID'"); while($subrow = mysql_fetch_array($getSubCatagory, MYSQL_ASSOC)){ $subCatagory = $subrow['subcatagoryID']; //Get Stats for number of threads $threads = mysql_query("SELECT * FROM entry WHERE subcatagoryID = '$subCatagory'") or die(mysql_error()); $num_threads = mysql_num_rows($threads); //Get Stats for number of entrys(posts) $posts = mysql_query("SELECT * FROM posts WHERE subcatagoryID = '$subCatagory'") or die(mysql_error()); $num_posts = mysql_num_rows($posts); //Find newest post and find what thread it is from $lastPost = mysql_query("SELECT * FROM `posts` ORDER BY `posts`.`postCreated` DESC") or die(mysql_error()); while($lastPostReturn = mysql_fetch_array($getSubCatagory, MYSQL_ASSOC)){ echo $lastPostReturn; } //Grab User and thread number and date $lastPostUser = $lastPostReturn['postUser']; $lastPostCreated = $lastPostReturn['postCreated']; $lastPostEntry = $lastPostReturn['entryID']; //Find the title of the thread and generate URL\ $lastPostThread = mysql_query("SELECT * FROM `entry` WHERE entryID = '$lastPostEntry'"); while($lastPostThreadReturn = mysql_fetch_array($lastPostThread, MYSQL_ASSOC)){ } echo ' <tr> <td><a href="catagory.php?id='.$subrow['subcatagoryID'].'">'.$subrow['subcatagoryTitle'].'</a></td> <td><center>'.$num_threads.'</center></td> <td><center>'.$num_posts.'</center></td> <td>'.$lastPostThreadReturn['text'].'<br>'.$lastPostCreated.'<br>'.$lastPostUser.'</td> </tr>'; } } Hey guys i have a query that select from multiple tables and works fine however i have the same field name in each table called:: member_id but i want to select specifically the member_id from table1 do i need to change the name when i loop through them? i really dont want to do that but if it confuses php unless is there a way when doin a while loop i specify what table i want it from? while($row = mysql_fetch_array($query)){ $member_id = $row['member_id']; } Hey guys, i'm sure this is simple i have a script that always brings back the same error and I beleive it's going to be the php version im on the code is Code: [Select] function check_user( $u ) { $res = query("SELECT COUNT(*) FROM `usertable` WHERE `userid`=$u"); list($total_rows) = mysql_fetch_array($res) or die (mysql_error()); if( $total_rows > 0 ) return true; else return false; } the error is Code: [Select] Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/bofs/public_html/thief/inc/userinc.php on line 10 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 any idea's well, i tried to fetch field from my database using the following code: Code: [Select] $result=mysql_query("SELECT * FROM Persons WHERE username='$myusername'" ); while($row = mysql_fetch_row($result)) now that gives me following error: Code: [Select] Warning: mysql_fetch_row() expects parameter 1 to be resource, boolean given in.. Hi I post this thread under php, because I guess that my coding is wrong. I always get the following message Quote Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/content/g/w/o/gwolff2005/html/admin/update.php on line 23 Update data in mysql The code is Code: [Select] <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <?php $host="xxxx"; // Host name $username="xxxx"; // Mysql username $password="xxxxx; // Mysql password $db_name="xxxxx"; // Database name $tbl_name="sp_users"; // Table name // Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); // get value of id that sent from address bar $id=$_GET['user_id']; // Retrieve data from database $sql="SELECT * FROM '$sp_users' WHERE id='$user_id'"; $result=mysql_query($sql); $result=mysql_query("select * from '$sp_users' WHERE id='$user_id'"); $rows = mysql_fetch_array($result); ?> <table width="400" border="0" cellspacing="1" cellpadding="0"> <tr> <form name="form1" method="post" action="update_ac.php"> <td> <table width="100%" border="0" cellspacing="1" cellpadding="0"> <tr> <td> </td> <td colspan="3"><div align="center" class="style1">Update data in mysql </div></td> </tr> <tr> <td align="center"> </td> <td align="center"> </td> <td align="center"> </td> <td align="center"> </td> </tr> <tr> <td align="center"> </td> <td align="center"><span class="style7">Name</span></td> <td align="center"><span class="style7">Lastname</span></td> <td align="center"><span class="style7">Email</span></td> </tr> <tr> <td> </td> <td align="center"><input name="name" type="text" id="name" value="<? echo $rows['user_first_name']; ?>"></td> <td align="center"><input name="lastname" type="text" id="lastname" value="<? echo $rows['user_surname']; ?>" size="15"></td> <td><input name="email" type="text" id="email" value="<? echo $rows['user_login']; ?>" size="15"></td> </tr> <tr> <td> </td> <td><input name="id" type="hidden" id="id" value="<? echo $rows['user_id']; ?>"></td> <td align="center"><input type="submit" name="Submit" value="Submit"></td> <td> </td> </tr> </table> </td> </form> </tr> </table> <? // close connection mysql_close(); ?> </body> </html>What am I doing wrong. Thanks for your help! please tell me the reason that why mysql_fetch_array does not work inside a function for example the following works well in its current form $sql="SELECT * FROM $tbl_13"; $query=mysql_query($sql); while($rs=mysql_fetch_array($query)){ $p0 = $rs["pfid"]; $pm = $rs["pm"]; } but when i put it inside a function it gives me an error at function defone() { $sql="SELECT * FROM $tbl_13"; $query=mysql_query($sql); while($rs=mysql_fetch_array($query)){ $p0 = $rs["pfid"]; $pm = $rs["pm"]; } return $pm; } the error message is... Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in... Please identify the problem.. i'm an experience programmer in ruby and perl but new in php I have some OOP code im working on and I have some interactions with the database. However, mysql_fetch_array fails to be working. Have I missed something? Code: [Select] class core { var $array_batsmen = ''; var $array_bowlers = ''; // Query to get batsmen function get_batsmen() { return $this->array_batsmen = mysql_query('SELECT id, name, ability, strength FROM wtw_players WHERE team_id = 1 ORDER BY id ASC'); } // Query to get bowlers function get_bowlers() { $this->array_bowlers = mysql_query('SELECT id, name, ability, strength FROM wtw_players WHERE team_id = 2'); } // Generate array of batsmen to be used function generateBatsmen() { while ($array = mysql_fetch_array($this->array_batsmen)) { echo $array['name']; } } } The functions are called later normally, i've only included the necessary parts. Basically I have the var $array_batsmen which stores the mysql_query. This is then later used with a mysql_fetch_array to return names from the earlier query. But nothing happens! It doesn't return anything (don't worry, I have a return later on). The loop just doesn't seem to work :/ Hi guys! I inputted text into the field 'TSuperQuestion' of my table. Now I am trying to echo that text. I get the following error: Quote Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /webusers/ph6theo/public_html/Code-sandbox.php This is my code: Code: [Select] <?PHP $con = mysql_connect("localhost","ph6theo", "xxxx"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("ph6theo_testDB") or die(mysql_error()); $result = mysql_query("SELECT * FROM Q&ATable"); $row = mysql_fetch_array($result); echo $row['TSuperQuestion']; ?> Can anyone help? Hi evrybody... I'm stuck in the boose now.... it was working fine before someone do a change... now I try to locate the error but I'm unable to do it. my knowledge in php/mysql is VERY low in other word doens'y know nothing at all. heres the message I receive Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/scale24/public_html/search-keyword.php on line 112 you can go directly on the site http://scale24-25.com/search-db-foreach.php?cat= the error appear when the vehicule manufacturer model dropdown was add. this menu autofill when you select one of the Name Of vehicule manufacturer droplist. if someone can help me it will be VERY APPRECIATE. you can view the before change here http://scale24-25.com/francaissource/search-db-foreachfr.php thanks in advance sebastien I want to use all five results separately in order to use in a Jquery slider. Code: [Select] <?php $a = "SELECT * FROM blog_posts ORDER BY id DESC LIMIT 5"; $query = mysql_query($a) or die (mysql_error()); $query_results = mysql_fetch_array($query); ?> I know if i was going to grab vars from an normal array i would just use... Code: [Select] <?php echo $array_reults['0'] ?> Can any one help me with this problem? Getting the following error message: Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in This error occurs when I first navigate to the search page. When I put a search term in the box and click search it works fine and the error is gone. I've been trying to figure this out for days with no luck, any help you can offer would be greatly appreciated. Code: [Select] <?php require('config.php'); ?> <?php $conn = mysql_connect(SQL_HOST, SQL_USER, SQL_PASS) or die('Could not connect to MySQL database. ' . mysql_error()); mysql_select_db(SQL_DB, $conn); $result = NULL; if (isset($_GET['keywords'])) { $sql = "SELECT id, hostname, ip, clust_name, clust_ip, node_name, ". "node_ip, os, patch " . "FROM serv_main " . "WHERE MATCH (hostname,ip) " . "AGAINST ('" . $_GET['keywords'] . "' IN BOOLEAN MODE) " . "ORDER BY MATCH (hostname,ip) " . "AGAINST ('" . $_GET['keywords'] . "' IN BOOLEAN MODE) DESC"; $result = mysql_query($sql) or die('Could not perform search; ' . mysql_error()); } ?> <html> <body> <form method="get" action="search.php"> <p class="head">Type in the Server name below to search for an SLA</p> <p> <input id="searchkeywords" type="text" name="keywords" <?php if (isset($_GET['keywords'])) { echo ' value="' . htmlspecialchars($_GET['keywords']) . '" '; } ?> > <input id="searchbutton" class="submit" type="submit" value="Search"> </p> <?php echo "<h1>Search Results</h1>\n"; if ($result and !mysql_num_rows($result)) { if (isset($_GET['keywords'])) { $k = $_GET['keywords']; } echo "<p>No SLA for <b><font size=\"6\" color=#990000>" . $k . "</b></font> found.</p>\n"; } else { $bg = ''; //Error occurs right here********************* while ($row = mysql_fetch_array($result)) { //($row['id'], TRUE); $bg = ($bg=='F2F2FF'?'99CCCC':'F2F2FF'); $table = "<table border=\"0\" cellpadding=\"5\">"; $table .= "<tr bgcolor=\"#" . $bg . "\">" . "<td><a href=\"servedit.php?s=" . $row['id'] . "\">" . $row['hostname'] . "</a></td><td>" . $row['ip'] . "</td><td align=\"center\">" . $row['clust_name'] . "</td><td>" . $row['clust_ip'] . "</td><td>" . $row['node_name'] . "</td><td>" . $row['node_ip'] . "</td><td>" . $row['os'] . "</td><td>" . $row['patch'] . "</td></tr>"; } $table .= "</table>"; echo $table; } ?> </form> </body> </html> MOD EDIT: [code] . . . [/code] tags added. Hello all, I am having trouble with a piece of code. I have used the same piece of code on a different server and I had no problems. Suddenly I do. - I use this code to load info into a Flash website to browse through all rows in a table; - Inside the table currently there is one record; - NumLow is being passed through the url; - echo "&aantalEvenementen=".$aantalEvenementen; shows 1 record just fine; - the record doesn't get printed though... the error is somewhere in the while loop. Hope someone can point me in the right direction! Cheers, Code: [Select] // Connect to mySQL Server $DBConn = mysql_connect($DBhost,$DBuser,$DBpass) or die("Error bij het verbinden met de MYSQL server: " . mysql_error()); // Select mySQL Database mysql_select_db($DBName, $DBConn) or die("Error bij het verbinden met de MYSQL database: " . mysql_error()); //amount of rows that are shown at once $numComments = 1; //select the right table $sql = "SELECT * FROM $table"; //loading events $nieuweEvenementen = mysql_query($sql, $DBConn) or die("Error bij het ophalen van de concerten: " . mysql_error()); //amount of rows $aantalEvenementen = mysql_num_rows($nieuweEvenementen); //order rows $sql .= ' ORDER BY `concertDatum` DESC LIMIT ' . $_GET['NumLow'] . ', ' . $numComments; //loaded row $geladenEvenement = mysql_query($sql, $DBConn) or die("Error bij het laden van het concert: " . mysql_error()); //echo amount of rows echo "&aantalEvenementen=".$aantalEvenementen; //if no records, display message, otherwise print row if($aantalEvenementen == 0) { echo "&concertOmschrijving=Er zijn geen concerten. U kunt nieuwe concerten aanmaken via de concertenkalender."; } else { while ($array = mysql_fetch_array($geladenEvenement)) { $concertNaam = mysql_result($geladenEvenement, $i, 'concertNaam'); $concertArtiest = mysql_result($geladenEvenement, $i, 'concertArtiest'); $concertDatum = mysql_result($geladenEvenement, $i, 'concertDatum'); $concertTijd = mysql_result($geladenEvenement, $i, 'concertTijd'); $concertOpen = mysql_result($geladenEvenement, $i, 'concertOpen'); $concertOmschrijving = mysql_result($geladenEvenement, $i, 'concertOmschrijving'); echo "&concertNaam=".$concertNaam; echo "&concertArtiest=".$concertArtiest; echo "&concertDatum=".$concertDatum; echo "&concertTijd=".$concertTijd; echo "&concertOpen=".$concertOpen; echo "&concertOmschrijving=".$concertOmschrijving; $i++; } } // if there are no more records (all are loaded), show message if($_GET['NumLow'] > $aantalEvenementen) { echo "$eventOmschrijving=Alle concerten zijn reeds geladen."; } } |