0

Sorry to waste your time but I'm trying to store data from DB table into arrays and display in a table. I keep getting the same error. I've changed the "'s and removed variables. Still I get

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/a3656574/public_html/Home.php on line 41

<?php
if ($_POST['search_projects']){

    $con= mysql_connect("host","username","password","a3656574_opacmin") or die ('Error: ' . mysql_error()); 
    $sql= "SELECT * FROM searchedWords WHERE accessDate LIKE '%" . $_POST['search'] . "%' ORDER BY accessDate DESC";
    $result= mysql_query($sql); 

    while($row= mysql_fetch_array($result))
    { 
        $Date =$row['AccessDate']; 
        $Key=$row['keyWord']; 
        $Count=$row['count']; 

        echo "<tr>"; 
        echo "<td>" .$Date ."</td> ". " <td>" . $Key.  " </td>" . " <td>" . $Count.  " </td>"; 
        echo "</tr>";  

    }
}
?>

I don't know how to fix this. Can someone please help?

Passerby
  • 9,715
  • 2
  • 33
  • 50
  • 3
    please make sure those are not your real credentials – Serguei Fedorov Sep 23 '13 at 04:20
  • make sure your query is correct , $sql= "SELECT * FROM searchedWords WHERE accessDate LIKE '%" . $_POST['search'] . "%' ORDER BY accessDate DESC"; just print it and run it directly on console – Manish Goyal Sep 23 '13 at 04:23
  • 3
    You should be using `mysqli_*` functions, `mysql_*` functins are deprecated from MySQL 5.5 IF I'm not mistaken. Remove actual credentials from your mysql con when posting in in any public forum. You have to select a db; did you select one before `mysql_query` ? – Maz Sep 23 '13 at 04:24
  • 1
    After `$result= mysql_query($sql);` add `if(!$result) die(mysql_error())`. What's the error message? Also, as Maz says, you should use mysqli or PDO instead of the mysql_ functions. – ChicagoRedSox Sep 23 '13 at 04:26
  • You need to first check if really `$result` is filled with actual `recoreds` by putting `if condition` like @ChicagoRedSox said. – Smile Sep 23 '13 at 04:34
  • @Maz +1 for using mysqli* – srain Sep 23 '13 at 04:40
  • 3
    **By building SQL statements with outside variables, you are leaving yourself wide open to SQL injection attacks.** Also, any input data with single quotes in it, like a name of "O'Malley", will blow up your SQL query. Please learn about using parametrized queries, preferably with the PDO module, to protect your web app. http://bobby-tables.com/php has examples to get you started, and [this question](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) has many examples in detail. – Andy Lester Sep 23 '13 at 04:42

4 Answers4

3

Mysql connection function receive 3 arguments (mysql_server, mysql_user, mysql_password)

and you should select database using mysql_select_db(database, connection_resource_id);

Also make sure your credential

Try:

$con= mysql_connect("host","username","password");
mysql_select_db("a3656574_opacmin",$con);
MD SHAHIDUL ISLAM
  • 14,325
  • 6
  • 82
  • 89
  • Good call, mysql_select_db() is a pretty good guess as to what he's expecting from that 4th parameter. – TML Sep 23 '13 at 04:29
0

Please check your database credentials and then try with:

<?php
  if ($_POST['search_projects'] && !empty($_POST['search']))
  {
    $con= mysql_connect("host.com","opacmin","password","opacmin") or die ('Error: ' . mysql_error()); 

    $sql= "SELECT * FROM searchedWords WHERE accessDate LIKE '%" . $_POST['search'] . "%' ORDER BY accessDate DESC";
    $result= mysql_query($sql); 

    while($row= mysql_fetch_array($result))
    { 
      $Date =$row['AccessDate']; 
      $Key=$row['keyWord']; 
      $Count=$row['count']; 

      echo "<tr>"; 
      echo "<td>" .$Date ."</td> ". " <td>" . $Key.  " </td>" . " <td>" . $Count.  " </td>"; 
      echo "</tr>";  
    }
  }
?>
Rajeev Ranjan
  • 4,152
  • 3
  • 28
  • 41
Sukh
  • 11
  • 2
0

0) To begin, I would urge you to start using PDO instead of mysql_connect (and friends), as the latter is being deprecated. There's tutorial to help you start in this migration here.

1) I'm not sure what it is you're expecting the 4th argument to mysql_connect() to do for you. Per the PHP documentation, this should be a boolean value:

mysql_connect ([ string $server = ini_get("mysql.default_host") [, string $username = ini_get("mysql.default_user") [, string $password = ini_get("mysql.default_password") [, bool $new_link = false...

2) Check for error conditions before moving on to mysql_fetch_array():

$sql= "SELECT * FROM searchedWords WHERE accessDate LIKE '%" . $_POST['search'] . "%' ORDER BY accessDate DESC";
$result= mysql_query($sql); 

if (! $result) {
    // use something like mysql_error() to find out why it failed, log it, etc.
} else {
    while( ... ) { ... }
}
TML
  • 12,813
  • 3
  • 38
  • 45
  • Thank you for that quick response. I finally got it right though. Found someone to help me in person. I read about PDO too and that mysql_fetch_array() and similar functions will be removed soon. I'll start adjusting to that now. Anyway, thank you again. –  Sep 23 '13 at 07:03
0

Firstly I would like to recommend you to start using "mysqli". You can look for this here

and then you should first check if your credentials are right. If that is right got to phpmyadmin and try your query out there and see if its working fine. Maybe you are missing something. Good luck.

theAndDev
  • 662
  • 2
  • 11
  • 33