PHP - Mysql_fetch_assoc() Only Returning 1 Value Instead Of All Of Them.
Okay, I had a similar problem before.
$users = mysql_query("SELECT * FROM users"); while($users = mysql_fetch_assoc($users)){ echo "<tr><td><p><a href='users.php?id={$users['user_id']}'>{$users['user_name']}</a></p></td><td>{$users['user_clan']}</td><td>{$users['troop_donations']}</td></tr>"; } Similar TutorialsHi I am new to php, I am trying to capture the url and place into a variable but I only get the 1st digit to show, I just cant see what I am doing wrong. Sorry to ask such a basic question but I just can't work it out, I have attached a screen shot of all me code, your help would be very very much appreciated. I am receiving this error: Code: [Select] Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /home/shannon/public_html/booter/inc/classes/class.db.php on line 17 For the function: Code: [Select] function fetch_array($query) { $array = mysql_fetch_assoc($query); return $array; } Why would I receive that error, if I am just passing a variable? Not a resource or boolean? Thanks. Not sure what I am doing wrong: Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /public_html/lq/index.php on line 12 $schedule = "LQ"; include('/public_html/en/header_staff.php'); include("connect.php"); $page = $_SERVER["SERVER_NAME"]; $who = explode(".", $page); $sql = "select * from rp_staff WHERE schedule = $schedule"; $rpstaff = mysql_query($sql); while ($row = mysql_fetch_assoc($rpstaff)) { $alt = $row['dj']; $name = $row['name']; $city = $row['city']; $state = $row['state']; $country = $row['country']; $artists = $row['artists']; $about = $row['about']; $genre = $row['genrev']; $position = $row['position']; } Im having a problem coding for our project . here's the code Code: [Select] <?php $value = $_POST['p']; $host="localhost"; $username="root"; $password=""; $db_name="dbquiz"; mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $value = stripslashes($value); $value = mysql_real_escape_string($value); $sql='SELECT * FROM `'. $value .'` ORDER BY RAND() LIMIT 100'; $result=mysql_query($sql) or die(mysql_error()); if(result){ while($row = mysql_fetch_assoc($result)) { $q = $row['question']; $c1= "" .$row['choice1']; $c2 ="" .$row['choice2']; $c3 ="" .$row['choice3']; $c4 ="" .$row['choice4']; $a ="".$row['answer']; $questions[] = array($q,$c1,$c2,$c3,$c4,$a); } } include_once("makequiz.php"); ?> AND FOR THE makequiz Code: [Select] <?php if (isset($_POST['sent'])) { for ($i=0;$i<count($questions);$i++) { echo($questions[$i][0]." - "); if ($_POST['q'.$i]=="c") { echo("<b>Correct!</b><br>\n"); $score++; } else { echo("<b>Wrong!</b><br>\n"); } } $percent = number_format(($score/count($questions))*100,2,".",","); echo("<br>".$score." out of ".count($questions)." (".$percent."% right)<br>\n"); } else { echo("<form action=\"#\" method=\"post\">\n"); echo("<input type=\"hidden\" name=\"sent\">\n"); for ($i=0;$i<count($questions);$i++) { echo("<b>".$questions[$i][0]."</b><br><br>\n"); if ($questions[$i][5]==1) { echo("<input type=\"radio\" name=\"q".$i."\" value=\"c\"> ".$questions[$i][1]."<br>\n"); } else { echo("<input type=\"radio\" name=\"q".$i."\" value=\"w\"> ".$questions[$i][1]."<br>\n"); } if ($questions[$i][5]==2) { echo("<input type=\"radio\" name=\"q".$i."\" value=\"c\"> ".$questions[$i][2]."<br>\n"); } else { echo("<input type=\"radio\" name=\"q".$i."\" value=\"w\"> ".$questions[$i][2]."<br>\n"); } if ($questions[$i][5]==3) { echo("<input type=\"radio\" name=\"q".$i."\" value=\"c\"> ".$questions[$i][3]."<br>\n"); } else { echo("<input type=\"radio\" name=\"q".$i."\" value=\"w\"> ".$questions[$i][3]."<br>\n"); } if ($questions[$i][5]==4) { echo("<input type=\"radio\" name=\"q".$i."\" value=\"c\"> ".$questions[$i][4]."<br><br>\n"); } else { echo("<input type=\"radio\" name=\"q".$i."\" value=\"w\"> ".$questions[$i][4]."<br><br>\n"); } } echo("<input type=\"submit\" value=\"Am I Right?!\">"); } ?> When you run the first code.. it's working but when i clicked the submit button this error keeps on showing and i dont know why.. "Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in " can someone help me fix this problem pls? thanks in advance. Code: [Select] $sql = "select * from user_info where us_name='$username' and md5(us_pass)='$userpass'"; $result=mysql_query($sql); $row = mysql_fetch_assoc($result); $count=mysql_num_rows($result); But it is giving error : arning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\...\verify.php on line 19 I am not getting why it is so Is there any way that I can get mysql_fetch_assoc only to run once? Here is my code: Code: [Select] $query = mysql_query("SELECT * FROM items ORDER BY id DESC"); while ($row = mysql_fetch_assoc($query)) { $id = $row['id']; } Would both of the below snippets do the same job, if so why are both ways taught in text books, can one method do somthing that the other cannot? mysql_fetch_array($result, MYSQL_ASSOC)) And... mysql_fetch_assoc($result) mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\eclipse\test\mysql.php on line 14 this is the error I get if I comment out the if statements. With the If statements I get "query failed" I have to assume the problem is with $extractQuery or $extract ... I just am at a loss for what the problem could be thanks Code: [Select] $connect = mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("Pickem", $connect) or die(mysql_error()); //extract data from key table $extractQuery = "SELECT * FROM key "; $extract = mysql_query($extractQuery, $connect); if ($extract) { if (mysql_numrows($extract)) { echo $extract; print_r(mysql_fetch_assoc($extract)); } else { echo "no results found"; } } else { echo "query failed"; } Following is the simple code to print name of states. However it doesn't print "Alabama" or in short it misses id=1.Can someone please point out my mistake or tell me why won't it print Alabama and ignoring id = 1 ? While debugging i found out it starts from id= 2. Code - Code: [Select] <?php $sql = "SELECT * FROM state"; $result = mysql_query($sql); ?> <SELECT NAME="state"> <OPTION value="">Select State</option> <?php while($arrayRow = mysql_fetch_assoc($result)) { $strA = $arrayRow["id"]; $strB = $arrayRow["state"]; echo "<option value=\"$strB\">$strB</option>\n"; }?> </SELECT> Sql table image - I have the following code Code: [Select] $hidden_client_id_query = mysql_query("SELECT id from `pdp_client_info` WHERE lead_id='$post_lead_id'"); $hidden_client_id=mysql_fetch_assoc($hidden_client_id_query); $this->view->hidden_client_id=$hidden_client_id['id']; I get the following error for the above code. I wonder where I am wrong. I am getting the $post_lead_id as expected. Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean I cannot retrieve my records from my table, neither can I display them on my website. I do not even get any errors when I run the script on my site. Any help or tips would be greatly appreciated. My current php script is: // connection to database server $db_selected = mysql_select_db('database1', $dbc); if (!$db_selected) { die ('Can\'t use database1 : ' . mysql_error()); } $query = "SELECT * FROM chesstable ORDER BY chapter, lastname"; $result = mysql_query($query) or die('Could not connect' . mysql_error()); echo mysql_num_rows($result); If (!$result) { $message = 'Invalid query: ' . mysql_error(); $message = 'Whole query: ' . $query; die ($message); } while($row = mysql_fetch_assoc($result)) { echo $row['firstname'] . " " . $row['lastname'] . $row['chapter']; } mysql_free_result($result); mysql_close($dbc); ?> i get this error Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\website\viewvideos.php on line 25 Code: [Select] $addviews = $views + 1; $query = mysql_query("UPDATE headlines SET views='$addviews' WHERE id=$viewid"); $rows = mysql_fetch_assoc($query); i dont get whats wrong Can I use the data fetched with the mysql_fetch_assoc command more than once? What I'm trying to do is query a MySQL DB, and print a list of the results by gender. So far I've only been able to do it by querying the DB twice, and I know there's got to be a more efficient way. I think I should be able to go through the array once, print all the males, then go through the array again and print all the females. But I'm stumped as to how to do it. Here's basically what I'm doing to get my results: Code: [Select] $cxn=mysqli_connect("localhost","username","password","table_name") or die ("Could not connect to database"); $query= "SELECT * FROM blah blah blah"; $result=mysqli_query($cxn,$query) or die ("Couldn't execute query."); while ( $row = mysqli_fetch_assoc($result)) { extract($row); IF ($gender == 'M') { echo "Here is my data for Males"; } } I basically want to re-run the WHILE statement, but check for $gender == 'F', without having to re-run the $result=mysqli_query command again. But when I do that, nothing is displayed and the script ends. Is it just a matter of moving a pointer to the beginning of an array or something similar? Thanks in advance. I got this code at the top: Code: [Select] <?php require_once("configuration.php"); $uid = $_GET['uid']; ?> And another main one: Code: [Select] <?php if(isset($_GET['uid'])) { $d = ""; $query = mysql_query("SELECT `username`,`level`,`email,`,`alevel`,`tester` FROM `users` WHERE `id`='$uid'"); if(!mysql_num_rows($query)) echo "No user."; while($d = mysql_fetch_assoc($query)) { ?>   <b>Username:</b> <?php echo $d['username']; ?> <br />   <b>Level:</b> <?php echo $d['level']; ?> <br />   <b>Email:</b> <?php echo $d['email']; ?> <br />   <b>Admin level:</b> <?php echo $d['alevel']; ?> <br />   <b>Tester:</b> <?php echo $d['tester']; ?> <?php } } ?> </font> </div> </center> Now I get this error: Code: [Select] Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\learning\viewuser.php on line 20 No user. Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\learning\viewuser.php on line 22 How can I fix it? Thank you for looking into this! I need to use result from mysql query twice or more on a page Should I do this: $sql = mysql_query("SQL"); while ($row = mysql_fetch_assoc($sql)) CODE ... while ($row = mysql_fetch_assoc($sql)) CODE2 OR should I keep result of a query in an array and use FOREACH instead? My concern is performance. Hi, I have tried to merge two queries into one however a piece of code keeps coming up with an error: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource. It relates to this. I cant see it doesn't work and I have tried around 10 different options but this is the nearest I can get. Can anyone advise me what the problem is and why this no longer works please? while($row = mysql_fetch_assoc($myQuery)) <?php ini_set('display_errors', 1); error_reporting(-1); if( isSet($_GET['description'])) $description = $_GET['description']; if(isSet($_GET['price'])) $price = explode('-',$_GET['price']); $low = (int)$price[0]; $high = (int)$price[1]; ($myQuery = "SELECT * FROM productfeed WHERE 1 . if(isset($description)) ' AND if description = '. $description; if(isset($price)) ' AND if price = '. $price; . "); while($row = mysql_fetch_assoc($myQuery)) { $id = $row['id']; $image = $row['awImage']; $link = $row['link']; $description = $row['description']; $fulldescription = $row['fulldescription']; $price = $row['price']; $fulldescription = substr("$fulldescription", 0, 400); echo "<div class='productdisplayshell'> <div class='productdisplayoutline'> <div class='productborder'><center> <a href='$link' target='_blank'><img src='$image' width=\"95%\" /></a> </center> </div></div> <div class='productdescriptionoutline'> <div class='productdescriptionbox'> <a href='$link' target='_blank' >$description</a> </div> <div class='productfulldescriptionbox'>$fulldescription</div> </div> <div class='productpriceoutline'> <div class='productpricebox'> <center>&#163; $price</center> </div> <div class='productbuybutton'> <center><a href='$link' target='_blank' ><img src=/images/buybutton.png /></a></center> </div> </div> </div>"; } Ive been having trouble. Im getting this error: Code: [Select] $select = mysql_query($query1); echo $select; $rows1 = mysqli_fetch_array($dbc, $select); I dont understand whats wrong. Please help, This code is giving me the warning stated in the above given Subject line. And combo box is also not populating. Code: [Select] function populatecombo() { $dropdown = ""; $sqlcmb = "select wardno from wards"; mysql_query($sqlcmb) or die(mysql_error('Unable to query the table')); while($row = mysql_fetch_assoc($sqlcmb)) { $dropdown .= "\r\n<option value='{$row['wardno']}>'{$row['wardno']}</option>"; } echo $dropdown; } i am having problem with this error, could you please help me Code: [Select] function cart () { foreach($_SESSION as $name => $value) { if ($value>0) { if (substr($name, 0, 5) == "cart_") { $productid = substr($name, 5, (strlen($name)-5)); $query = mysql_query("SELECT ProductID, Name, Price FROM product WHERE ProductID = '".mysql_real_escape_string((int)$productid."'")); while ($query_row = mysql_fetch_assoc($query)) { $sub = $query["Price"]*$Value; echo $query["Name"]. ' x ' .$value. ' @ '.$query["Price"]. ' = '.$sub.'<br />'; } } } else { echo "<p>Your Shopping Basket is empty</p>"; } } } Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\test.php on line 58 line 58 is the where the while loop starts.. what have i done wrong? Code: [Select] $query2 = mysql_query("SELECT student.SID, course.CID FROM student, course WHERE student.SID = `student-course.SID` AND course.CID = `student-course.CID` AND GRADE BETWEEN $beginning AND $grade",$this->connect); while($row = mysql_fetch_assoc($query2)) { $sid = $row['student.SID']; $cid = $row['course.CID']; echo "$sid-$cid"; } |