PHP - Mysql_fetch_assoc And Num Rows Problem.
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? Similar TutorialsI 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); ?> Hi, im creating a script for a website that will list products, i have the basic construct which lists the products from the database but it lists then vertically, i have set the width of each 'product' to 200px so i could have around 3 colums and many rows. can anybody advise how i would make them run horizontally aswell as vertically? my code below: <?php include_once("include/globals.php"); ?> <!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"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <link href="styles/stylesheet.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="main"> <?php //Get database Results $result = mysql_query("SELECT * FROM Products WHERE is_active = 1") or die(mysql_error()); //keeps getting the next row until no more records while($row = mysql_fetch_array($result)) { //echo results ($result) echo '<div id="holder">'; echo '<div class="title">' . $row['Name'] . '</div>'; //insert image here echo '<div class="image">'; echo '<a href="product_detail.php?id="><img src="' . $row['image'] . '" width="100" alt="" border="0"></a>'; echo '</div>'; echo '<div class="tag_line">'; echo $row['Tag_Line']; echo '</div>'; echo '<div class="price">Now Only: £'; echo $row['Website_Price']; echo '</div>'; echo '<div class="prod-footer">'; echo "<a href=\"product_detail.php?id={$row['ID']}\"> more info </a></li>"; echo '</div>'; echo '</div>'; } ?> </div> </body> </html> For some reason my php script only echo's out one data, I can't seem to find the problem. <? $show = mysql_query("SELECT * FROM video_comments WHERE pageid='$vidid'"); $numrows = mysql_num_rows($show); if ($numrows > 0){ while($row = mysql_fetch_array($show)){ $userid = $row["userid"]; $username = $row["username"]; $comment = $row["comment"]; $date = $row["date"]; $date = strftime("%b %d, %Y", strtotime($date)); } echo" <table style='background-color:#FFF; border:#999 1px solid; border-top:none;' cellpadding='5' width='100%'> <tr> <td width='90%' valign='top' style='line-height:1.5em;'><span class='greenColor textsize10'>$date<a href='profile.php?id=$userid'> $username </a> said:</span><br /> $comment</td> </tr> </tr></table> "; } else echo"<center><font face='arial' size='2'><p>This video has no comments.</font></center>"; ?> Hi, I've created an admin page that lists all piano sheet music that has been uploaded to my server. It displays the ID (autoincrement), Userid (user that uploaded sheet), Artist (name of the band) and Title (name of the song). On this page, I can either delete the row completely, or click on "Uploaded", which just sets it's uploaded column to 'yes'. Now I'm working on a button called "Uploaded All". This will set the uploaded column on all of the rows on the page to 'yes'. This way I don't have to click Uploaded for every single row. (view attached image if you are unsure what I am talking about) I'm not getting any errors when clicking "Uploaded All". When clicking it, it doesn't update the table rows in the database as it should. Take a look at my code: Tip: The area of the code that I'm currently having troubles with is here $usersids[$i] = $row['id']; $i++; } echo " <tr> <td align='center' width='10' colspan='6'><a href='uploadedsheets.php?confirm=all'>Uploaded All</a></td> </tr>"; if ($confirm=="all") { $i = 0; while($row = mysql_fetch_array($result)) { mysql_query('UPDATE `upload` SET `uploaded`="yes" WHERE id = ' . $usersids[$i]); $i++; } echo "<SCRIPT language='JavaScript'><!-- window.location='uploadedsheets.php';//--> </SCRIPT>"; } Inside the above code after the if statement, I tried echoing out $usersids, and it showed correctly. So the problem must be in the update query or the array. Full Code: <?php session_start(); include_once('../inc/connect.php'); include_once('../inc/admin.php'); if(!isset($_SESSION['sort_counter'])) {$_SESSION['sort_counter'] = 1;} if(($_SESSION['sort_counter']%2) == 0){ //test even value $sortcount = "DESC"; }else{ //odd value $sortcount = ""; } $result = mysql_query("SELECT * FROM upload WHERE uploaded='no' ORDER BY id"); $unconfirmedquery = mysql_query("SELECT uploaded FROM upload WHERE uploaded='no'"); $unconfirmedcount = mysql_num_rows($unconfirmedquery); $confirmedquery = mysql_query("SELECT uploaded FROM upload WHERE uploaded='yes'"); $confirmedcount = mysql_num_rows($confirmedquery); $sort = $_GET['sort']; $delete = $_GET['delete']; $confirm = $_GET['confirm']; ///////////////////////////////// if ($sort=='id'){ // $result = mysql_query("SELECT * FROM users ORDER BY id"); $result = mysql_query("SELECT * FROM upload WHERE uploaded='no' ORDER BY id $sortcount"); $_SESSION['sort_counter'] = $_SESSION['sort_counter'] + 1; //increment after every run } if ($sort=='userid'){ // $result = mysql_query("SELECT * FROM users ORDER BY username"); $result = mysql_query("SELECT * FROM upload WHERE uploaded='no' ORDER BY userid $sortcount"); $_SESSION['sort_counter'] = $_SESSION['sort_counter'] + 1; //increment after every run } if ($sort=='artist'){ // $result = mysql_query("SELECT * FROM users ORDER BY email"); $result = mysql_query("SELECT * FROM upload WHERE uploaded='no' ORDER BY artist $sortcount"); $_SESSION['sort_counter'] = $_SESSION['sort_counter'] + 1; //increment after every run } if ($sort=='title'){ // $result = mysql_query("SELECT * FROM users ORDER BY email"); $result = mysql_query("SELECT * FROM upload WHERE uploaded='no' ORDER BY title $sortcount"); $_SESSION['sort_counter'] = $_SESSION['sort_counter'] + 1; //increment after every run } if ($sort=='file'){ // $result = mysql_query("SELECT * FROM users ORDER BY email"); $result = mysql_query("SELECT * FROM upload WHERE uploaded='no' ORDER BY file $sortcount"); $_SESSION['sort_counter'] = $_SESSION['sort_counter'] + 1; //increment after every run } /// FIX THIS AREA if ($confirm=="true" && isset($_GET['id'])) { mysql_query('UPDATE `upload` SET `uploaded`="yes" WHERE id = ' . (int)$_GET['id']); echo "<SCRIPT language='JavaScript'><!-- window.location='uploadedsheets.php';//--> </SCRIPT>"; } if ($delete=="true" && isset($_GET['id'])) { mysql_query('DELETE FROM `upload` WHERE id = ' . (int)$_GET['id']); echo "<SCRIPT language='JavaScript'><!-- window.location='uploadedsheets.php';//--> </SCRIPT>"; } if ($delete=="false" && isset($_GET['id'])) { echo "<SCRIPT language='JavaScript'><!-- window.location='uploadedsheets.php';//--> </SCRIPT>"; } ///////////////////////////////// echo " <html> <head> <title>Uploaded Sheets</title> <style> a:link{ text-decoration: none; color: #519904; } a:visited{ text-decoration: none; color: #519904; } a:hover{ text-decoration: none; color: #4296ce; } #headcont{ width: 900px; height: 20px; margin-left: auto; margin-right: auto; } #unconfirmed{ width: 450px; text-align: center; float: left; background-color: #cccccc; } #confirmed{ width: 450px; text-align: center; float: left; background-color: #72A4D2; } </style> </head> <body> "; include_once('../inc/navadmin.php'); echo "<div style='font-size: 28px; text-align: center;'>Uploaded Sheets</div> <div id='headcont'> <div id='unconfirmed'>Not Uploaded Sheets: ".$unconfirmedcount."</div> <div id='confirmed'>Uploaded Sheets: ".$confirmedcount."</div> </div><br /> <table border='1' align='center'> <tr> <th bgcolor='#cccccc'><a href='uploadedsheets.php?sort=id'>ID</a></th> <th bgcolor='#cccccc'><a href='uploadedsheets.php?sort=userid'>UserID</a></th> <th bgcolor='#cccccc'><a href='uploadedsheets.php?sort=artist'>Artist</a></th> <th bgcolor='#cccccc'><a href='uploadedsheets.php?sort=title'>Title</a></th> <th bgcolor='#cccccc'><a href='uploadedsheets.php'>Uploaded</a></th> <th bgcolor='#cccccc'><a href='uploadedsheets.php'>Delete</a></th> </tr>"; echo "<script type='text/javascript'> function show_delete() { var r=confirm('Delete?'); if (r==true) { // Delete return true; } else { // Don't Delete return false; } } "; echo " function show_undelete() { var r=confirm('Undelete?'); if (r==true) { // Undelete return true; } else { // Don't Undelete return false; } } </script>"; $usersids = ""; $i = 0; while($row = mysql_fetch_array($result)) { // $active = $row['active']; $color = "#ffffff"; $deleted = "Delete"; if ($active=='no'){ $color = "#f43636"; $deleted = "Undelete"; $active = "false"; $alert = "show_undelete"; } else{ $active = "true"; $alert = "show_delete"; } // echo "<tr>"; echo "<td align='center' width='40' bgcolor='$color'>" .$row['id']. "</td>"; echo "<td align='center' width='40'>" .$row['userid']. "</td>"; echo "<td align='center' width='230'>".ucwords($row['artist'])."</td>"; echo "<td align='center' width='230'>".ucwords($row['title'])."</td>"; echo "<td align='center' width='10'><a href='uploadedsheets.php?confirm=true&id=" .$row['id'] . "'>Uploaded</a></td>"; echo "<td align='center' width='10'><a href='uploadedsheets.php?delete=$active&id=" .$row['id']. "' onclick='return $alert()'>$deleted</a></td>"; echo "</tr>"; $usersids[$i] = $row['id']; $i++; } echo " $usersids[0] <tr> <td align='center' width='10' colspan='6'><a href='uploadedsheets.php?confirm=all'>Uploaded All</a></td> </tr>"; if ($confirm=="all") { $i = 0; while($row = mysql_fetch_array($result)) { mysql_query('UPDATE `upload` SET `uploaded`="yes" WHERE id = ' . $usersids[$i]); $i++; } echo "<SCRIPT language='JavaScript'><!-- window.location='uploadedsheets.php';//--> </SCRIPT>"; } echo "</table>"; // Footer echo " </body> </html> "; ?> i have created my own code of custom shopping cart
i have viewcart.php working great, now when i want to insert the orders from viewcart.php with list of like 5 items, how can i insert 5 names of products into my database 1 row
example
names are
1. jean
2. mond
3. richard
4. gwen
list above is the results of my while loop, now i want to insert those names to my database column[order_productname] so that i can identity what products are paid by my clients.
i tried fetch_array but if i assign variable to fetch array result, it only shows 1 which is "jean"
i wish this is possible
dforth
hello all, i want to display each row from mysql table in a different page using this code: Code: [Select] <? require_once('config.php'); //Connect to mysql server $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die("can't connect: " . mysql_error()); } $db = mysql_select_db(DB_DATABASE); if(!$db) { die("can't select database"); } mysql_query("SET NAMES 'hebrew'"); //mysql_set_charset('utf8',$link); if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; }; $start_from = $page-1; $qry='SELECT * FROM ordering limit 0, 1'; $result = mysql_query($qry); while($row = mysql_fetch_array($result)){ echo "<div dir='rtl' charset='utf8'><h2>"," hover cam: ",$row['hover_camera'], "<br> stills: ",$row['stills'], "<br> video_photographers: ",$row['video_photographers'], "<br> increase: ",$row['increase'], "<br> video_edit: ",$row['video_edit'], "<br> digital_album: ",$row['digital_album'], "<br> photo_album: ",$row['photo_album'], "<br> small_digital_album: ",$row['small_digital_album'], "<br> video_clip: ",$row['video_clip'], "<br> magnets: ",$row['magnets'], "<br> comments: ",$row['comments'], "<br> date: ",$row['date'], "<br><br></h2></div>"; } $sql = "SELECT COUNT(userid) FROM ordering"; $result = mysql_query($sql,$link); $row2 = mysql_fetch_row($result); $total_records = $row2[0]; $total_pages = $total_records ; for ($i=1; $i<=$total_pages; $i++) { echo "<a href='table.php?page=".$i."'>".$i."</a> "; }; ?> this is the table: Code: [Select] CREATE TABLE IF NOT EXISTS `ordering` ( `userid` int(11) unsigned NOT NULL AUTO_INCREMENT, `hover_camera` varchar(10) DEFAULT NULL, `stills` int(5) NOT NULL, `video_photographers` int(5) NOT NULL, `increase` int(5) NOT NULL, `video_edit` varchar(10) NOT NULL, `digital_album` varchar(10) DEFAULT NULL, `photo_album` varchar(10) DEFAULT NULL, `small_digital_album` varchar(20) DEFAULT NULL, `video_clip` varchar(10) DEFAULT NULL, `magnets` int(10) NOT NULL, `comments` text NOT NULL, `date` date NOT NULL, PRIMARY KEY (`userid`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 ; the problem is that only the first row is displayed no matter which page i choose and i want each page to display the row assigned to it (page 15 = row 15 etc...) any suggestions? 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']; } 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. 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']; } 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. hello i have a mysql table and i want to display each row in a different page with an added condition, the code: Code: [Select] if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; }; $start_from = $page-1; $qry="SELECT * FROM ordering WHERE userid='$page' AND email='$email' LIMIT 0,1"; $result = mysql_query($qry); while($row = mysql_fetch_array($result)){ echo "<div dir='rtl' charset='utf8'><h2>"," hover cam: ",$row['hover_camera'], "<br> stills: ",$row['stills'], "<br> video_photographers: ",$row['video_photographers'], "<br> increase: ",$row['increase'], "<br> video_edit: ",$row['video_edit'], "<br> digital_album: ",$row['digital_album'], "<br> photo_album: ",$row['photo_album'], "<br> small_digital_album: ",$row['small_digital_album'], "<br> video_clip: ",$row['video_clip'], "<br> magnets: ",$row['magnets'], "<br> comments: ",$row['comments'], "<br> date: ",$row['date'], "<br><br></h2></div>"; } $sql = "SELECT COUNT(userid) FROM ordering"; $result = mysql_query($sql,$link); $row2 = mysql_fetch_row($result); $total_records = $row2[0]; $total_pages = $total_records ; for ($i=1; $i<=$total_pages; $i++) { echo "<a href='myorders.php?page=".$i."'>".$i."</a> "; }; the table is as so: Code: [Select] CREATE TABLE IF NOT EXISTS `ordering` ( `userid` int(11) unsigned NOT NULL AUTO_INCREMENT, `hover_camera` varchar(10) DEFAULT NULL, `stills` int(5) NOT NULL, `video_photographers` int(5) NOT NULL, `increase` int(5) NOT NULL, `video_edit` varchar(10) NOT NULL, `digital_album` varchar(10) DEFAULT NULL, `photo_album` varchar(10) DEFAULT NULL, `small_digital_album` varchar(20) DEFAULT NULL, `video_clip` varchar(10) DEFAULT NULL, `magnets` int(10) NOT NULL, `comments` text NOT NULL, `date` date NOT NULL, `fname` varchar(100) NOT NULL, `email` varchar(100) NOT NULL, PRIMARY KEY (`userid`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; now to the problem: this code displays all the userid as page numbers however, the email condition is good only for part of the rows it's a little hard to explain, i'll try an example: suppose the email '1@1.com' is at rows 22,25 and 30 my code displays 30 page numbers and page 22 is user 22, page 25 is user 25 etc. what i want is to display only the page numbers of the pages with the conditional email, and if possible to display them as 1,2,3 instead of 22,25,30 <!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"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Untitled Document</title> </head> <body> <?php //MySQL Database Connect include 'sqlconnect.php'; ?> <select name="aktivitet" id="aktivitet"> <?php //MySQL Database Connect include 'sqlconnect.php'; $sql = mysql_query("SELECT title FROM aktiviteter"); while ($row = mysql_fetch_array($sql)){ echo "<option value=\"owner1\">" . $row['title'] . "</option>"; } ?> </select> </body> </html>why dosnt this work?, if you cant see what i want to happen, ill post an explaination 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 - 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"; } 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>"; } 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 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) 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>"; } 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. |