Jump to content

mysql_fetch_assoc No Longer Works??


justlukeyou

Recommended Posts

 

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>";
}

Link to comment
Share on other sites

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 :D.

 

 

$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

Link to comment
Share on other sites

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>&#163; $price</center>
</div>
<div class='productbuybutton'>
<center><a href='$link' target='_blank' ><img src=/images/buybutton.png /></a></center>
</div>
</div>
</div>";
} 




?>

Link to comment
Share on other sites

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
}

Link to comment
Share on other sites

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>&#163; $price</center>
</div>
<div class='productbuybutton'>
<center><a href='$link' target='_blank' ><img src=/images/buybutton.png /></a></center>
</div>
</div>
</div>";
} 

Link to comment
Share on other sites

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.
     }
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

$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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.