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?
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);
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( ... ) { ... }
}
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.
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>";
}
}
?>
Related
i'm trying to find a solution to make my php script show only the last 3 entries in a row but all i can find is how to show the first entries not the last 3. any ideas?
this is the script that shows the entries:
<?php
// Grab the data from our people table
$sql = "select * from people";
$result = mysql_query($sql) or die ("Could not access DB: " . mysql_error());
while ($row = mysql_fetch_assoc($result)) {
echo "<div class=\"picture\">";
echo "<p>";
// Note that we are building our src string using the filename from the database
echo "<img src=\"content/uploads/" . $row['filename'] . "\" alt=\"\" /><br />";
echo $row['fname'] . " " . "<br />" . "<br />" . $row['lname'] . "<br />";
echo "</p>";
echo "</div>";
}
?>
try this
$sql = "SELECT * FROM people ORDER BY id DESC LIMIT 3";
id=>is your column name you you might have to change this
and dont use mysql function as they are depricated and soon going to be dropped. Learn mysqli or PDO
Just do reverse ORDER BY, ie. if your table is ordered by column id, then do ORDER BY id DESC and then use LIMIT 3:
$sql = "select * from people ORDER BY id DESC LIMIT 3";
And PS, mysql_* are deprecated...
I have following code. With a single select query and an update query.It is working fine when i remove the update query. When i run following complete code then nothing happens.
Please help me I want to update table with every cycle of select query. Is there any way to execute following code.
$query = "SELECT * FROM ab_rec WHERE username='$userid'" or die(mysql_error());
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){ //Creates a loop to loop through results
$t_name=$row['testname'];
$first_url=$row['first_url'];
$thanks_url=$row['thanks_url'];
$start_date=$row['start_date'];
$parse_first_url = parse_url($first_url); //parsing URL of first page for removing main domain name from it.
$parse_thanks_url = parse_url($thanks_url);
$final_first_url = $parse_first_url['path'] ; //Finally parsed URLs are stored into new variables
$final_thanks_url = $parse_thanks_url['path'] ;
$row['unique_visits'] = calculate_visitors($final_first_url, $start_date);
$row['conversions']= calculate_visitors($final_thanks_url, $start_date);
$row['conversion_percent'] = ($conversions/$unique_visits_first)*100;
$query1="UPDATE `ab`.`ab_rec` SET unique_visits=$row['unique_visits'], conversions=$row['conversions'] , conversion_percent=$row['conversion_percent'], WHERE testname=$row['testname'] " or die(mysql_error());
$result2=mysql_query($query1, $connection);
echo "<tr><td>" . $checkbox . "</td><td>" ."<a href='my_test.php?test_name=$t_name'>".$row['testname'] . "</a></td><td>" . $row['date_of_creation'] . "</td><td>" . $row['unique_visits'] . "</td><td>" . $row['conversions'] . "</td><td>" . $row['conversion_percent'] ."%". "</td></tr>"; //$row['index'] the index here is a field name
}
This:
$query1="UPDATE `ab`.`ab_rec` SET unique_visits=$row['unique_visits'],
conversions=$row['conversions'] , conversion_percent=$row['conversion_percent'],
WHERE testname=$row['testname'] " or die(mysql_error());
$result2=mysql_query($query1, $connection);
Should be:
$query1="UPDATE `ab`.`ab_rec` SET unique_visits=$row['unique_visits'],
conversions=$row['conversions'] , conversion_percent=$row['conversion_percent']
WHERE testname='{$row['testname']}'";
echo $query1; //POST THIS RESULT
$result2=mysql_query($query1, $connection) or die(mysql_error());
I'm trying to display data starting at a certain time and day and ending and another time and day. here is my code:
<?php
include 'includes/connect3.php';
$query = "SELECT * FROM u_visits WHERE date >= '2013-08-31 22:56:20' AND date <= '2013-
08- 31 23:59:59'";
$result = mysqli_query($con,$query);
echo "<table><tr>
<th>USER ID</th>
<th>TIMES VISITED</th>
</tr>";
while($row = mysqli_fetch_assoc($result))
{
echo "<tr><td>" . $row['id'] . "</td><td>" . $row['visits'] . "</td></tr>";
}
echo "</table>";
?>
When I go to the page, only the table header is displayed with no data showing.
you can also use BETWEEN for range comparisons.
$query = "SELECT * FROM u_visits WHERE `date` BETWEEN '2013-08-31 22:56:20' AND '2013-08-31 23:59:59'";
The reason you didn't see any result is because your query has some errors. The error in your query is that you have a space after 2013-08- in your where clause (date <= '2013-08- 31 23:59:59'";)
Had you used proper error handling, you would have noticed this yourself. One common way to do this is:
$result = mysqli_query($con,$query) or die ('Query execution failed: ' . mysqli_error($con));
Hi I have trying to learn php by writing little web app for showing me sales data. I have got a query which i now works as i have tested it but i want it to echo the datematched and the number of rows/results found with that date. This is what I have so far
<?php
$con=mysqli_connect("host","user","password","database");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM matched WHERE datematched IN (
SELECT datematched FROM matched GROUP BY datematched HAVING count(*) > 1");
while($row = mysqli_fetch_array($result))
{
echo "['";
echo "" . $date['datematched'] . "', ";
echo "" . $num_rows . "],";
}
mysqli_close($con);
?>
I know i am doing something wrong here. ryan
EDIT:
<?php
$con=mysqli_connect("host","user","password","database");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM matched WHERE datematched IN (
SELECT datematched FROM matched GROUP BY datematched HAVING count(*) > 1");
echo "['";
echo " 16/08/2013 ', ";
echo "12345}],";
mysqli_close($con);
?>
Okay i have just checked my echo and they work i put in some data so all i need is to find a way of getting the information of the datematched that has been found and then the number of rows that has been found with that. Thanks Ryan
first of all you need to make an adjustment to your query, so that it has the number of rows your expecting.
$result = mysqli_query($con,"SELECT datematched, COUNT(*) as num_rows "
. "FROM matched GROUP BY datematched HAVING num_rows > 0");
then you can display the data as follows
while($row = mysqli_fetch_array($result))
{
echo $row['datematched'] . ",";
echo $row['num_rows'];
}
if your sql query is perfect then you should write like this wayt
while($row = mysqli_fetch_array($result))
{
echo "['";
echo "" . $row['datematched'] . "', ";
echo "" . $row['num_rows'] . "', ";
}
please set your column as you got in your mysql query.
<?php
$query=mysqli_query($con,"SELECT datematched FROM matched GROUP BY datematched");
$num=mysqli_num_rows($query);
if($num>1)
{
$result = mysqli_query($con,"SELECT * FROM matched");
$num_rows=mysqli_num_rows($result);
while($row = mysqli_fetch_array($result))
{
echo '['; echo $row['datematched']; echo $num_rows; echo ']';
}
}
I am trying to perform an additional query as you can see from the below piece of code.
I am having issues performing the second query. It seems like the code is not recognizing the second query. Can someone please advice?
Thanks in advance.
$query = "SELECT * FROM tst WHERE status ='active' order by created DESC LIMIT 9";
$result = mysql_query($query);
if (!$result) die ("database access failed : " . mysql_error());
while($row = mysql_fetch_array($result))
{
echo"<ul class='gallery'><li>";
echo "<td>" . $row['adid'] . "</td>";
$subquery = ("SELECT * FROM match_item_image where adid = '.$row['adid'].' LIMIT 1 ");
$results = mysql_query($subquery) or die ("Error in query: $subquery " . mysql_error());
$rows = mysql_fetch_array($results);
$rows = mysql_num_rows($results);
echo ' <img src= "upload/'.$rows['image'].'"height="175" width="200"border="0" /> ';
echo "<h2><a href=''>".$row['state'] . "</a></h2>";
echo "<h2><a href=''>".$row['loc'] . "</a></h2>";
echo"
</li>
</ul>";
}
the query is not working because you have included the $row['adid'] as a string and not as a value in the query, concactenate it like this
"SELECT * FROM match_item_image where adid = '" . $row['adid'] . "' LIMIT 1 "
and your query is now vulnerable with SQL Injection, please read the article below
Best way to prevent SQL injection in PHP