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?

7
  • 3
    please make sure those are not your real credentials Commented Sep 23, 2013 at 4: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 Commented Sep 23, 2013 at 4: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 ? Commented Sep 23, 2013 at 4: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. Commented Sep 23, 2013 at 4:26
  • 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. bobby-tables.com/php has examples to get you started, and this question has many examples in detail. Commented Sep 23, 2013 at 4:42

4 Answers 4

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Good call, mysql_select_db() is a pretty good guess as to what he's expecting from that 4th parameter.
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( ... ) { ... }
}

1 Comment

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

Comments

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

1 Comment

Maybe remove the credentials the OP mistakenly provided?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.