PHP Error - MYSQL Function

6 replies
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource

Having some trouble... can't figure it out what is wrong.

<?php
if(isset($_POST['post'])){
if($_POST['title'] || $_POST['text'] || $_POST['text'] > 24){
$title = clean($_POST['title']);
$text = clean($_POST['text']);
$showselect = $_POST['show'];
$sql = "SELECT `id` FROM `show` WHERE `show_title` = '$showselect'";
$query = mysql_query($sql);
while($row = mysql_fetch_assoc($query)){
$sql = "INSERT INTO `blog` (show_id, title, content) VALUES('".$row['id']."', '$title', '$text')";
$query = mysql_query($sql);
if($query == TRUE)
echo "
Your blog post has been successfully added to the database.
";
else
echo "ERROR";
}
}else
echo "
All fields are required and the text must be <b>over</b> 24.
";
}
?>
#error #function #mysql #php
  • Profile picture of the author wayfarer
    You'll get better error reporting if you do this:
    PHP Code:
    $query mysql_query($sql) or die(mysql_error()); 
    instead of your current mysql_query. This will allow MySQL to report what is wrong with your SQL query instead of relying on PHP's error reporting.
    Signature
    I build web things, server things. I help build the startup Veenome. | Remote Programming Jobs
    {{ DiscussionBoard.errors[4411443].message }}
  • Profile picture of the author wayfarer
    As an additional note (this may even be the source of your problem), you should be escaping your $_POST['show'] since it comes from an unreliable source (a form). It could have double quotes in it, which would mess up your SQL, or worse have double quotes followed by more SQL code, which could cause SQL injection (a form of hacking). You should always escape user input used in SQL queries like so:

    PHP Code:
    $showselect mysql_real_escape($_POST['show']); 
    This will escape this data based on the exact current encoding of the currently connected database.
    Signature
    I build web things, server things. I help build the startup Veenome. | Remote Programming Jobs
    {{ DiscussionBoard.errors[4411469].message }}
  • Profile picture of the author KirkMcD
    You're redefining $query inside the loop. You might want to rename it.
    {{ DiscussionBoard.errors[4413095].message }}
  • Profile picture of the author NerdGary
    $sql = "SELECT `id` FROM `show` WHERE `show_title` = '$showselect'";

    Should be:

    $sql = "SELECT * FROM show WHERE show_title='$showselect'";

    ... I think you want ALL the results where show_title = your variable..

    Also.. change this part...

    VALUES('".$row['id']."', '$title', '$text')";

    to

    VALUES('$row[id]', '$title', '$text')";

    ... you dont need to use the . to join... just remove the quotes...
    Signature



    <><>-----------------------------------------<><>
    {{ DiscussionBoard.errors[4497875].message }}
  • Profile picture of the author lknielsen
    KirkMcD is correct, there are other causes for this error as well:

    Sometimes you will get this error if your query returns zero rows. You could use a statement like this to determine if you have a result:

    if (mysql_num_rows($query) > 0)
    {
    //Put the code here
    }
    {{ DiscussionBoard.errors[4502500].message }}
  • Profile picture of the author leppozdrav
    Issue resolved.. Thanks for your help NerdGary. And other too!!!
    {{ DiscussionBoard.errors[4505263].message }}

Trending Topics