justlukeyou Posted February 24, 2011 Share Posted February 24, 2011 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>£ $price</center> </div> <div class='productbuybutton'> <center><a href='$link' target='_blank' ><img src=/images/buybutton.png /></a></center> </div> </div> </div>"; } Quote Link to comment https://forums.phpfreaks.com/topic/228733-mysql_fetch_assoc-no-longer-works/ Share on other sites More sharing options...
ChemicalBliss Posted February 24, 2011 Share Posted February 24, 2011 mysql_fetch_assoc doesnt take a string (the actual query), it takes a "Result Resource Identifier", which is returned by using a mysql_query() call or similar function. Also, I don't know why your enclosing the query in brackets but, they are in the wrong place for a start lol you don't need them . $myQuery = mysql_query("SELECT * FROM productfeed WHERE 1 . if(isset($description)) ' AND if description = '. $description; if(isset($price)) ' AND if price = '. $price; . "); hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/228733-mysql_fetch_assoc-no-longer-works/#findComment-1179261 Share on other sites More sharing options...
justlukeyou Posted February 24, 2011 Author Share Posted February 24, 2011 Hi many thanks, I have simplified it down but it still returns the same error. It looking for something it kind find from the code? $myQuery = mysql_query("SELECT * FROM productfeed WHERE 1 . if(isset($description)) ' AND if description = '. $description; if(isset($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>£ $price</center> </div> <div class='productbuybutton'> <center><a href='$link' target='_blank' ><img src=/images/buybutton.png /></a></center> </div> </div> </div>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/228733-mysql_fetch_assoc-no-longer-works/#findComment-1179270 Share on other sites More sharing options...
KevinM1 Posted February 24, 2011 Share Posted February 24, 2011 Your query has syntax errors. Specifically, you're attempting to use PHP code (if(isset(....))) within it. Quote Link to comment https://forums.phpfreaks.com/topic/228733-mysql_fetch_assoc-no-longer-works/#findComment-1179273 Share on other sites More sharing options...
justlukeyou Posted February 24, 2011 Author Share Posted February 24, 2011 Thanks, how can I resolve that. I have the full error messages switched on. I have looked for a tutorial which explains how to build multiple queries but I cant seem to find one which explains it well. Im also a bit lost of which technique to use. Quote Link to comment https://forums.phpfreaks.com/topic/228733-mysql_fetch_assoc-no-longer-works/#findComment-1179275 Share on other sites More sharing options...
KevinM1 Posted February 24, 2011 Share Posted February 24, 2011 Fixing it requires you taking the PHP code out of the query. You need to put your query in its own variable, and build it through that. $query = "SELECT * FROM productfeed WHERE 1"; if(isset($description)) { $query .= " AND description = $description"; } // etc. for other dynamic clauses $result = mysql_query($query); while($row = mysql_fetch_assoc($result)) { // do stuff } Quote Link to comment https://forums.phpfreaks.com/topic/228733-mysql_fetch_assoc-no-longer-works/#findComment-1179279 Share on other sites More sharing options...
justlukeyou Posted February 24, 2011 Author Share Posted February 24, 2011 Thanks, I have reduced it to the $description but I still have the same issue. This part mysql_fetch_assoc(): fails to work. Its the same code I am using before on individual queries but now I am trying to merge them it coming up with an error. $query = "SELECT * FROM productfeed WHERE 1"; if(isset($description)) { $query .= " AND description = $description"; } // etc. for other dynamic clauses $result = mysql_query($query); while($row = mysql_fetch_assoc($result)) { $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>£ $price</center> </div> <div class='productbuybutton'> <center><a href='$link' target='_blank' ><img src=/images/buybutton.png /></a></center> </div> </div> </div>"; } Quote Link to comment https://forums.phpfreaks.com/topic/228733-mysql_fetch_assoc-no-longer-works/#findComment-1179285 Share on other sites More sharing options...
KevinM1 Posted February 24, 2011 Share Posted February 24, 2011 Try running your query directly in phpMySQLAdmin. Also, why do you have a WHERE 1 clause? It's redundant. Quote Link to comment https://forums.phpfreaks.com/topic/228733-mysql_fetch_assoc-no-longer-works/#findComment-1179288 Share on other sites More sharing options...
justlukeyou Posted February 24, 2011 Author Share Posted February 24, 2011 Hi, I have tried to run a qeury in phpMySQLAdmin? What will that give me? I thought multiple queries would be quite standard. Is there a common method for performing them? Quote Link to comment https://forums.phpfreaks.com/topic/228733-mysql_fetch_assoc-no-longer-works/#findComment-1179293 Share on other sites More sharing options...
Pikachu2000 Posted February 24, 2011 Share Posted February 24, 2011 You have no logic in that code to handle errors. You really need to add some so you don't have to get help with every problem you have. $query = "SELECT field FROM table"; if( !$result = mysql_query($query) ) { echo "<br>Query: $query<br>Failed with error: " . mysql_error() . '<br>'; } else { while( $array = mysql_fetch_assoc($result) ) { // do your echoing, etc. } } Quote Link to comment https://forums.phpfreaks.com/topic/228733-mysql_fetch_assoc-no-longer-works/#findComment-1179300 Share on other sites More sharing options...
jcbones Posted February 24, 2011 Share Posted February 24, 2011 Hi, I have tried to run a qeury in phpMySQLAdmin? What will that give me? I thought multiple queries would be quite standard. Is there a common method for performing them? PHP doesn't allow mysql_query() to perform multiple queries (ironically, it says so at the top of the page in the manual). However, it will handle JOIN queries quite well. Quote Link to comment https://forums.phpfreaks.com/topic/228733-mysql_fetch_assoc-no-longer-works/#findComment-1179304 Share on other sites More sharing options...
justlukeyou Posted February 25, 2011 Author Share Posted February 25, 2011 Hi, I have tried to run a qeury in phpMySQLAdmin? What will that give me? I thought multiple queries would be quite standard. Is there a common method for performing them? PHP doesn't allow mysql_query() to perform multiple queries (ironically, it says so at the top of the page in the manual). However, it will handle JOIN queries quite well. Hi, Im not looking to use seperate queries. I am looking to create one query which is able to use different strings. Can I do that with mysql_query or do I need to use implode http://uk.php.net/manual/en/function.implode.php If you were to create a query which can use different strings what approach would you use? Quote Link to comment https://forums.phpfreaks.com/topic/228733-mysql_fetch_assoc-no-longer-works/#findComment-1179498 Share on other sites More sharing options...
KevinM1 Posted February 25, 2011 Share Posted February 25, 2011 What do you have for your query now? Also, have you tried what Pikachu suggested above re: error handling? mysql_query and implode are two very different things. Implode simply glues an array of strings together. Mysql_query runs a database query. I'm not sure why you'd ask your question as though the two are related. For combining strings, I find that the concatenation operator is a bit more flexible as you don't have to put a substring in an array in order to add it to the main string. Quote Link to comment https://forums.phpfreaks.com/topic/228733-mysql_fetch_assoc-no-longer-works/#findComment-1179513 Share on other sites More sharing options...
justlukeyou Posted February 25, 2011 Author Share Posted February 25, 2011 Thanks, This is what I'm trying to achieve: .php?$enginesize=1600&$carcolour=red&$carlocation=newyork or .php?$enginesize=1600&$carlocation=newyork&$carcolour=red ***The results from both URL links returns the same results from one database.*** I have the seperate queries but I am a bit bewildered on how to merge these into one query. I thought I could use mysql_query() but it appears I cant. Would you use the concatenation operator to acheive this? Quote Link to comment https://forums.phpfreaks.com/topic/228733-mysql_fetch_assoc-no-longer-works/#findComment-1179529 Share on other sites More sharing options...
KevinM1 Posted February 25, 2011 Share Posted February 25, 2011 First, remove the '$' from your URL variables, so you simply have something like: .php?enginesize=1600&carcolour=red&carlocation=newyork That said, I fail to see where these values match what you're trying to get from your db. And, again, mysql_query simply executes a single db query. Constructing the query (e.g., "SELECT yadda yadda FROM...") falls on you. Finally, note that a query string, when said in the context of a URL IS NOT the same thing as a database query. They are two completely separate things which unfortunately have similar names. All that said, you still haven't said: 1. What you currently have for your query 2. What you're actually hoping it would be/do 3. What values you're expecting to be passed in via $_GET Quote Link to comment https://forums.phpfreaks.com/topic/228733-mysql_fetch_assoc-no-longer-works/#findComment-1179536 Share on other sites More sharing options...
Muddy_Funster Posted February 25, 2011 Share Posted February 25, 2011 $myQuery = mysql_query("SELECT * FROM productfeed WHERE 1 . if(isset($description)) ' AND if description = '. $description; if(isset($price)) . "); is a total mess. Try the following: $wArray = array(0 =>" WHERE description ='".$description."'", 1 => " WHERE description = '".$description."' AND price = ".$price, 2 => " WHERE price = ".$price, 3 => " WHERE 1") $query = 'SELECT id, awImage, link, description, price FROM productfeed'; if ((isset($description)) && ($description != '') && ((!isset($price)) || ($price = ''){ $wList = 0; } if ((isset($description)) && ($description != '') && ((isset($price)) && ($price != ''){ $wList = 1; } if ((!isset($description)) || ($description != '') && ((isset($price)) && ($price != ''){ $wList = 2; } if ((!isset($description)) || ($description = '') && ((!isset($price)) || ($price = ''){ $wList = 3; } $qry = $query . $wArray[$wList]; $result = mysql_query($qry) or die ('Error querying the database -- '.mysql_error()); I've never been much use at arrays, but that should work. Quote Link to comment https://forums.phpfreaks.com/topic/228733-mysql_fetch_assoc-no-longer-works/#findComment-1179549 Share on other sites More sharing options...
KevinM1 Posted February 25, 2011 Share Posted February 25, 2011 This process should be fairly simple: $query = "SELECT * FROM tablename"; if (/* some value is present */) { $query .= " WHERE somecolumn = $somevar"; } // continue for as many values you need to check $result = mysql_query($query) or die(mysql_error()); // note, in a live environment NEVER use die() for error handling while($row = mysql_fetch_assoc($result)) { // do stuff } And, again, WHERE 1 is redundant, and not necessary at all. Quote Link to comment https://forums.phpfreaks.com/topic/228733-mysql_fetch_assoc-no-longer-works/#findComment-1179557 Share on other sites More sharing options...
justlukeyou Posted February 25, 2011 Author Share Posted February 25, 2011 Thanks, Apologies. I was using an example and your right the $ shouldn't be in links. I do have the links working on individual queries so I can say: .php?description=redwidget and .php?price=1-300 But I cant mix them together into one query to read: php?price=1-300&description=redwidget or php?description=redwidget&price=1-300 I picked up Where 1 from me here. Many thanks for help. I shall have another crack at putting this together. I have only been using PHP for around 4 weeks so I am still learning but this is a great forum and many thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/228733-mysql_fetch_assoc-no-longer-works/#findComment-1179564 Share on other sites More sharing options...
KevinM1 Posted February 25, 2011 Share Posted February 25, 2011 Well, again, a URL query string and a db query aren't the same thing. You need something like: $query = "SELECT * FROM productsfeed"; if(isset($_GET['description']) && !empty($_GET['description'])) { $description = $_GET['description']; $query .= " WHERE description = $description"; } if(isset($_GET['price']) && !empty($_GET['price'])) { $price = explode('-', $_GET['price']); $lowPrice = (int)$price[0]; $highPrice = (int)$price[1]; $query .= " AND price BETWEEN $lowPrice AND $highPrice"; } $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_assoc($result)) { // do stuff } Quote Link to comment https://forums.phpfreaks.com/topic/228733-mysql_fetch_assoc-no-longer-works/#findComment-1179571 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.