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
Related
My problem is this:
<?php
// Connect to database server
mysql_connect("localhost", "root", "abcabc") or die (mysql_error ());
// Select database
mysql_select_db('iite') or die(mysql_error());
// Get data from the database depending on the value of the id in the URL
$strSQL = "SELECT * FROM table1 WHERE id=" . $_GET["id"];
$rs = mysql_query($strSQL);
// Loop the recordset $rs
while($row = mysql_fetch_array($rs)) {
// Write the data of the person
echo'<h1>'. $row['title'].'</h1>';
echo'<h1>'. $row['price'].'</h1>';
}
// Close the database connection
mysql_close();
?>
I want to show related posts, for this I need to insert this:
$sql = "SELECT * FROM table1 where title like '%keyword%' limit 5";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["price"]. " " . $row["title"]. "<br>";
}
} else {
echo "0 results";
}
so how can I insert the related post part near
$strSQL = "SELECT * FROM table1 WHERE id=" . $_GET["id"];
and how to echo?
Thanks
You can use OR or AND for combining different where conditions(according to requirement) in query :
Ex;
$strSQL = "SELECT * FROM table1 WHERE id=" . $_GET["id"] ." OR title like '%keyword%' limit 5";
$strSQL = mysql_query(SELECT * FROM table1 WHERE id=" . $_GET["id"] ." OR title like '%keyword%' limit 5) or die(mysql_error());
$row_strSQL = mysql_fetch_assoc($strSQL );
$totalRows_strSQL = mysql_num_rows($strSQL );
if ($result->totalRows_strSQL > 0) {
// output data of each row
while($row_strSQL= $result->fetch_assoc()) {
echo "id: " . $row_strSQL["id"]. " - Name: " . $row_strSQL["price"]. " " . $row_strSQL["title"]. "<br>";
}
} else {
echo "0 results";
}
You don't need two seperate queries for that at all. Just use the OR operator in ur where clause like Where id=" . $_GET['id']." or title like '%keyword%'
I am obtaining some values from an array and making a match against these values in an SQL Query.
The code for this is as follows:
foreach($files as $ex){
$search = substr($ex,3,4);
echo $search . '<br>';
echo '<br>';
$sql = 'SELECT DISTINCT `pdb_code` FROM pdb WHERE `pdb_code` <> "' . $search . '" LIMIT 4';
}
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo 'SQL' . $row['pdb_code'] .'<br>';
$pdb[] = $row['pdb_code'];
}
The issue that I am having is that the <> seems not to be working.. I have even tried using the != operator, but still having the same issue.
The output of $search from the array are :
101m
102l
102m
103l
The output of SQL from the query is still:
101m
102l
102m
103l
Your code doesn't seem that logical, as you generate numerous SQL statements and then just execute the last one.
However I assume what you want to do is take a list of files, extract a string from each file name and then list all the pdb_code values from the table which are not already in the string.
If so something like this would do it. It takes each file name, extracts the sub string and escapes it, putting the result into an array. Then it builds one query, imploding the array to use in a NOT IN clause:-
<?php
$search_array = array();
foreach($files as $ex)
{
$search = substr($ex,3,4);
echo $search . '<br>';
echo '<br>';
$search_array[] = mysql_real_escape_string($search);
}
if (count($search_array) > 0)
{
$sql = "SELECT DISTINCT `pdb_code` FROM pdb WHERE `pdb_code` NOT IN ('" . implode("','", $search_array) . "') LIMIT 4";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
echo 'SQL' . $row['pdb_code'] .'<br>';
$pdb[] = $row['pdb_code'];
}
}
You have to use not in:
SELECT * FROM table_name WHERE column_name NOT IN(value1, value2...)
Try this:
$searchIds = implode(',',$search);
$sql = "SELECT DISTINCT `pdb_code` FROM pdb WHERE `pdb_code` NOT IN ('$searchIds') LIMIT 4";
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...
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>";
}
}
?>
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 ']';
}
}