I am trying to de-duplicate entries in one of my MySQL tables but I keep getting the same error.
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\Inetpub\vhosts\myexamplewebsite.com\httpdocs\duplicate\index.php on line 25
What am I doing wrong please?
<?php
$dedupe=mysqli_connect("localhost","user","pass","database");
$result = mysqli_query($dedupe,"SELECT * FROM `videos` WHERE `dupe`=0 order by RAND() ");
while($row = mysqli_fetch_array($result))
{
$vod_index = $row['index'];
$vod_video_name = $row['video_name'];
$vod_vidstart = $row['vidstart'];
$vod_providerid = $row['providerid'];
$vod_dupe = $row['dupe'];
$result2 = mysqli_query($dedupe,"SELECT * FROM `videos` WHERE `index` != '$vod_index';
AND `dupe`='0'
AND `video_name` = '$vod_video_name'
AND `vidstart` = '$vod_vidstart'
AND `providerid` = '$vod_providerid'
AND `index` != '$vod_index'
");
while($row2 = mysqli_fetch_array($result2))
{
$vod_index2 = $row2['index'];
$vod_video_name2 = $row2['video_name'];
$vod_vidstart2 = $row2['vidstart'];
$vod_providerid2 = $row2['providerid'];
$vod_dupe2 = $row2['dupe'];
mysqli_query($dedupe,"UPDATE `videos` set `dupe`=1
WHERE `index`='$vod_index2' ");
{
echo("Error description: " . mysqli_error($recordcon));
}
mysqli_query($dedupe,"INSERT INTO deduped (index,videos_index)
VALUES ('NULL','$vod_index2')");
}
}
mysqli_close($dedupe);
?>
Please note: index is the primary key for the table.
I would prefer to fix this method than start another if possible as I wrote this as a simpler one for me to understand than examples I was finding that other people have written.
Any help is appreciated.
You have an unexpected semicolon in your query, try it like this:
$result2 = mysqli_query($dedupe,"SELECT * FROM `videos` WHERE `index` != '$vod_index'
AND `dupe`='0'
AND `video_name` = '$vod_video_name'
AND `vidstart` = '$vod_vidstart'
AND `providerid` = '$vod_providerid'
AND `index` != '$vod_index'
");
Related
Following is my code,
$result1 = "SELECT emp_id FROM employee where manager_id=".$userID;
$array = mysql_query($result1);
$cnt = 0;
while ($row = mysql_fetch_array($array)) {
"emp_id: " . $row[0];
$myArrayOfemp_id[$cnt] = $row[0];
$cnt++;
}
var_dump($myArrayOfemp_id);
$sql = "SELECT emp_id FROM emp_leaves WHERE emp_id='$myArrayOfemp_id' ORDER BY apply_date DESC";
$result = mysql_query($sql);
$total_results = mysql_num_rows($result);
When I'am trying to use $myArrayOfemp_id variable in $sql query, It shows that error:
Array to string conversion in..
How can I fix it?
You are trying to convert an array into a string in the following line:
$sql = "SELECT emp_id FROM emp_leaves
WHERE emp_id='$myArrayOfemp_id' ORDER BY apply_date DESC";
$myArrayOfemp_id is an array. That previous line of code should be changed to:
$sql = "SELECT emp_id FROM emp_leaves
WHERE emp_id={$myArrayOfemp_id[0]} ORDER BY apply_date DESC";
I placed 0 inside {$myArrayOfemp_id[0]} because I'm not sure what value want to use that is inside the array.
Edited:
After discussing what the user wanted in the question, it seems the user wanted to use all the values inside the array in the sql statement, so here is a solution for that specific case:
$sql = "SELECT emp_id FROM emp_leaves
WHERE ";
foreach ($myArrayOfemp_id as $value)
{
$sql .= " emp_id={$value) || ";
}
$sql .= "1=2";
$result = mysql_query($sql);
$total_results = mysql_num_rows($result);
$sql = "SELECT emp_id FROM emp_leaves WHERE emp_id in
(SELECT GROUP_CONCAT(emp_id) FROM employee where manager_id=".$userID.")
ORDER BY apply_date DESC";
$result = mysql_query($sql);
$total_results = mysql_num_rows($result);
just change your query like above might solve your problem.
you can remove following code now. :)
$result1 = "SELECT emp_id FROM employee where manager_id=".$userID;
$array = mysql_query($result1);
$cnt = 0;
while ($row = mysql_fetch_array($array)) {
"emp_id: " . $row[0];
$myArrayOfemp_id[$cnt] = $row[0];
$cnt++;
}
var_dump($myArrayOfemp_id);
I have the following code
$sql = "SET #uid := (SELECT ID FROM channels WHERE Used = 0 ORDER BY RAND() LIMIT 1);";
$sql = "UPDATE channels SET Used = 1 WHERE ID = #uid;";
$sql = "SELECT * FROM channels WHERE ID IN = #uid;";
$result = mysqli_multi_query($conn, $sql)
or die( mysqli_error($sql) );
if (mysqli_num_rows($result) > 0) {
$text = '';
while($row = mysqli_fetch_assoc($result)) {
$Channel_Location = $row['Channel_Location'];
$text = $text . $Channel_Location;
}
}
Now the issue i'm having is the php isnt displaying the result returned by the MYSQL query which is stored in a session later on in the code to be displayed on a dummy page it comes up with the following error
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result
The my SQL query does exactly what I need it to I just need to, so I don't really want to change it. I just need some advice on how i'd get the PHP to echo the #uid
is there anyone willing to help me solve the issue? if so thankyou.
You have 3 queries in your $sql so you should use multi_query function
http://php.net/manual/en/mysqli.multi-query.php
And you can change your first query to:
SET #uid = 0;
SELECT #uid := ID FROM channels WHERE Used = 0 ORDER BY RAND() LIMIT 1);
Update You can try this fragment of your code modified with all commented improvements.
$sql = 'SET #uid = 0;';
$sql .= 'SELECT #uid:= ID FROM channels WHERE Used = 0 ORDER BY RAND() LIMIT 1);';
$sql .= 'UPDATE channels SET Used = 1 WHERE ID = #uid;';
$sql .= 'SELECT * FROM channels WHERE ID IN = #uid;';
if (mysqli_multi_query($conn, $sql)) {
do {
$result = mysqli_store_result($conn);
} while(mysqli_next_result($conn));
if (mysqli_num_rows($result) > 0) {
$text = '';
while($row = mysqli_fetch_assoc($result)) {
$Channel_Location = $row['Channel_Location'];
$text = $text . $Channel_Location;
}
}
} else {
die( mysqli_error($conn) );
}
I want to get an id from browser and display some pictures from the database.
If there is no "display2.php?productid=" found, then I want to display default image.
How can I do that?
Here is my code;
$sql = "SELECT * FROM productlist where productid=".$_GET['productid'];
$result = $mysqli->query($sql);
while($myRow = $result->fetch_array())
{
if(null !==($_GET['productid']==$myRow["productid"])){
echo "<img src=".$myRow["productid"].">";
}
else {
echo "<img src="SELECT productimage FROM productlist where productid = 1;">";
}
}
Now I will make it easier to explain for you...
Check this out;
//This part works without any problem
$sql = "SELECT * FROM productlista where productid=".$_GET['productid'];
$result = $mysqli->query($restwo);
while($myRow = $resulttwo->fetch_array())
{
if(null !==($_GET['productid']==$myRow["productid"])){
echo "<img src=".$myRow["productimage"].">";
}
//This part below (that should be default) does not work...
if (!$_GET){
echo "hello world"; }
Asaph pointed out SQL injection. You should bind the parameter (google it), or at the minimum do this:
$defaultImage = "SELECT * FROM productlist WHERE imageSrc != '' OR IS NOT NULL ORDER BY productid DESC LIMIT 1";
// run the query, get the result, create a variable with default image...
$defaultImageSrc = ''; // what you get from the query result
$_GET['productid'] = preg_replace('#[^0-9]#', '', $_GET['productid']);
$sql = "SELECT * FROM productlist where productid=".$_GET['productid'];
$result = $mysqli->query($sql);
while($myRow = $result->fetch_array()) {
if(!$myRow['imageSrc']) $myRow['imageSrc'] = $defaultImageSrc;
echo '<img src="'.$path.'">';
}
If you want either $_GET['productid'] or the max(productid) when $_GET['productid'] is not set, you can use a ternary to change your sql query
$productid = ! empty($_GET['productid']) ? " WHERE productid = ".(int)$_GET['productid'] : " ORDER BY productid DESC LIMIT 1";
$sql = "SELECT * FROM productlist".$productid
$result = $mysqli->query($sql);
while($myRow = $result->fetch_array())
{
echo "<img src=".$myRow["productimage"].">";
}
so if isset($_GET['productid']) your query would be
SELECT * FROM productlist WHERE productid = (int)$_GET['productid']
but if not the default would be
SELECT * FROM productlist ORDER BY productid DESC LIMIT 1
Hi guys.
I'm currently try to make an mysql query than take the results and use them in an another query. So I thought I'm calling my database and use mysql_fetch_array and than implode it do insert , so I can use it in an another query. I read here many questions about this and based on the questions i wrote my own piece of code but I'm getting this error:
Warning: array_values() expects parameter 1 to be array, string given in /var/www/html/lager_management/warenkorb.php on line 107
Warning: implode(): Invalid arguments passed in /var/www/html/lager_management/warenkorb.php on line 108
Here is the piece of code what is going wrong I can't explain myself and I know mysql is old and I should use myqli
$sql3 = "SELECT `Index` FROM lm_Warenkorb;";
$result3 = mysql_query($sql3);
while($resultarray3 = mysql_fetch_array($result3))
{
$anfrage = array();
$anfrage = $resultarray3['Index'];
$anfrage = implode(", ", $anfrage);
$sql2 = "SELECT `Index`, `Artikelbezeichnung`, `Status`, `Bestand`, `Lieferant`, `Datum-Einlagerung`, `Lagerort` FROM `lm_Artikel` WHERE `Index` IN (".$anfrage.");";
}
The table lm_Warenkorb looks like this:
Index:
10
2
6
I think you could do it using one query with nested SELECT:
$sql3 = "
SELECT `Index`, `Artikelbezeichnung`, `Status`, `Bestand`, `Lieferant`, `Datum-Einlagerung`, `Lagerort`
FROM `lm_Artikel`
WHERE `Index` IN (
SELECT `Index` FROM lm_Warenkorb
)";
$result3 = mysql_query($sql3);
while($resultarray3 = mysql_fetch_array($result3)) {
// handle the results
}
you use mysql_fetch_array($result) in a while loop, which is perfectly right.
But this obviously will only return one row of your table from database and not the whole column.
therefore $resultarray3['Index']; returns the value of Index column of your first table row, which is not an array.
Try this
$anfrage = array();
while($resultarray3 = mysql_fetch_array($result3))
{
$anfrage[] = $resultarray3['Index'];
}
if(count($anfrage) > 0) {
$anfrage = implode(",", $anfrage);
$sql2 = "SELECT `Index`, `Artikelbezeichnung`, `Status`, `Bestand`, `Lieferant`, `Datum-Einlagerung`, `Lagerort` FROM `lm_Artikel` WHERE `Index` IN (".$anfrage.");";
}
$sql3 = "SELECT `Index` FROM lm_Warenkorb;";
$result3 = mysql_query($sql3);
$data = array(0);
while($resultarray3 = mysql_fetch_assoc($result3))
{
$data[] = $resultarray3['Index'];
}
$sql2 = "SELECT `Index`, `Artikelbezeichnung`, `Status`, `Bestand`, `Lieferant`, `Datum-Einlagerung`, `Lagerort` FROM `lm_Artikel` WHERE `Index` IN (".implode(',', $data).");";
echo $sql2;
my PHP code looks like this right now:
$query = mysql_query("SELECT * FROM `questions` WHERE `id` = '$id' ORDER BY RAND()") or die(mysql_error());
$cmd = mysql_fetch_assoc($query);
$question = $cmd['question'];
Right now, the questions just get randomized - which is fine - but sometimes the same question appears again, and I don't want that. I assume you can fix this with a session. But how? If someone can fix the code, I'd really appreciate.
Perhaps something like this:
$query = "SELECT * FROM `questions` WHERE `id` = `$id` AND `id` NOT IN (";
$query .= implode(', ', array_keys($_SESSION['questions']));
$query .= ') ORDER BY RAND()';
mysql_query($query) or die(mysql_error());
// Here add the returned questions to the $_SESSION['questions'] array so they would not appear again.
I don't know how the rest of the program works, so the logic you need may be a little different, but I'm sure that sort of query is what you're looking for.
This should do what you need:
$query = mysql_query("SELECT * FROM `questions` WHERE `id` = '$id' ORDER BY RAND()") or die(mysql_error());
$question = null;
while ($cmd = mysql_fetch_assoc($query)) {
if (array_search($cmd['question'], $_SESSION['asked']))
continue;
$question = $cmd['question'];
$_SESSION['asked'][] = $question;
break;
}
if (!$question) {
// No unique questions found.
} else {
// $question will be unique here
}
You can store the ID of all previous asked questions inside your session:
if (!isset($_SESSION['QuestionAsked']))
{
$_SESSION['QuestionAsked'] = array();
}
You can then extend your query to exclude all asked questions:
$query = 'SELECT * FROM `questions`';
if ($_SESSION['QuestionAsked'])
{
$askedIds = implode(',', $_SESSION['QuestionAsked']);
$query .= sprintf(' WHERE `id` NOT IN (%s)', $askedIds);
}
$query .= ' ORDER BY RAND()';
And finally after querying a new question, add it to the session:
$_SESSION['QuestionAsked'][] = $currentQuestionId;