Reputation:
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?
Upvotes: 0
Views: 806
Reputation: 11
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>";
}
}
?>
Upvotes: 0
Reputation: 672
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.
Upvotes: 0
Reputation: 14523
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);
Upvotes: 3
Reputation: 12966
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( ... ) { ... }
}
Upvotes: 0