-2

I am trying to fetch the values of a table which consists of three records or both x and y and the error message displayed is : mysql_query() expects parameter 1 to be string, resource given in C:\Program Files (x86)\EasyPHP-5.3.9\www\ag_fetching.php on line 17 Invalid query: please help me to figure out the problem. My code goes like this

<?php

    $servername = "localhost";
    $username = "root";
    $password = "";

    $conn =mysql_connect($servername, $username, $password);

    if (! $conn) {
        die("Connection failed: " . mysql_error());
    }

    mysql_select_db("PRJ") or die(mysql_error());

    $result =mysql_query("SELECT * FROM CF");

    if (!$result) { 
        die('Invalid query: ' . mysql_error());
    }

    $retval = mysql_query($result,$conn);
    if (!$retval) { 
        die('Invalid query: ' . mysql_error());
    }

    $count=0;

    while($row = mysql_fetch_array($retval)) {

        $X[]=$row['x'];
        $Y[]=$row['y'];
        $count++;
    }
    mysql_close($conn);
?>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Anvesh
  • 39
  • 7
  • 1
    Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[a Kitten is strangled somewhere in the world](http://2.bp.blogspot.com/-zCT6jizimfI/UjJ5UTb_BeI/AAAAAAAACgg/AS6XCd6aNdg/s1600/luna_getting_strangled.jpg)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Nov 22 '16 at 10:14
  • You have obviously coded something incorrectly, but you dont show us any code! So how can we possibly help – RiggsFolly Nov 22 '16 at 10:15
  • Welcome to SO. Please read [What topics can I ask about](http://stackoverflow.com/help/on-topic) and [How to ask a good question](http://stackoverflow.com/help/how-to-ask) And [the perfect question](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) And how to create a [Minimal, Complete and Verifiable example](http://stackoverflow.com/help/mcve) – RiggsFolly Nov 22 '16 at 10:15
  • You already ran the query and then you try and run `$retval = mysql_query($result,$conn);` – RiggsFolly Nov 22 '16 at 10:17
  • Apologies. I hadnt posted my code .Now i joined my code along with my problem. – Anvesh Nov 22 '16 at 10:18
  • Some sensible code indentation would be a good idea. It helps us read the code and more importantly it will help **you debug your code** [Take a quick look at a coding standard](http://www.php-fig.org/psr/psr-2/) for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. – RiggsFolly Nov 22 '16 at 10:19
  • Still icant figure ot the problem whats gone wrong? – Anvesh Nov 22 '16 at 10:21

1 Answers1

0

You seem to have added a section of code that is completely unnecessary and incorrectly coded. If you just remove it as below things will probably work

<?php

    $servername = "localhost";
    $username = "root";
    $password = "";

    $conn =mysql_connect($servername, $username, $password);

    if (! $conn) {
        die("Connection failed: " . mysql_error());
    }

    mysql_select_db("PRJ") or die(mysql_error());

    $result = mysql_query("SELECT * FROM CF");

    if (!$result) { 
        die('Invalid query: ' . mysql_error());
    }

    // unnecessary and incorrectly coded  
    //$retval = mysql_query($result,$conn);
    //if (!$retval) { 
    //    die('Invalid query: ' . mysql_error());
    //}

    $count=0;

    //while($row = mysql_fetch_array($retval)) {
    while($row = mysql_fetch_array($result)) {

        $X[]=$row['x'];
        $Y[]=$row['y'];
        $count++;
    }
    mysql_close($conn);
?>

Every time you use the mysql_ database extension in new code a Kitten is strangled somewhere in the world it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the PDO or mysqli database extensions. Start here

Community
  • 1
  • 1
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • It worked for me.Thanks for your suggestions.I will surely begin with mysqli and PDO extensions. – Anvesh Nov 22 '16 at 10:25