FIND IN SET not working with PDO - mysql

I'm having issues with working with mysql's FIND_IN_SET and pdo. This is my code:
$statement = $conn->prepare("SELECT * FROM `artistInfo` WHERE FIND_IN_SET(':array', artistServices)");
$statement->execute(array(':array' => '2'));
while($row = $statement->fetch()){
echo $row['id'];
echo "<br />";
}
This doesn't produce any results. Am I doing something wrong? Thanks!

$statement = $conn->prepare("SELECT * FROM `artistInfo` WHERE FIND_IN_SET(':array', artistServices)");
$statement->execute(array(:array => '2'));
while($row = $statement->fetch()){
echo $row['id'];
echo "<br />";
}
The single quotes surrounding :array is why I wasn't getting any results

Related

fetchAll from database using PDO

So I've been working on this website a long time but the problem is most of it is MYSQL, which of course isnt very secure, so now I'm trying to update all the PHP to PDO, so far I've only managed to get the connect.php working (in the first code below). My main issue is wthin the 2nd code below, on my reviews page I'm having a hard time fetching ALL the reviews from database including (cid,uid,username,date,message & rating) and then ORDER BY DESC, I have used multiple guides but they all seem to show a different way of doing it...
<?php
$host = 'localhost';
$dbuser = 'B99';
$dbpwd = 'testpass';
$dbname = 'admin';
//set DSN//
$dsn = 'mysql:host=' . $host .';dbname=' . $dbname;
//Create PDO instance//
//Attempt MySQL server connection.//
$pdo = new PDO($dsn, $dbuser, $dbpwd);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
?>
and the FetchAll code here:
<?php
include("/var/www/vhosts/myweb.co.uk/httpdocs/PHP/connect.php");
$sql = $pdo->query("SELECT * FROM `reviews` ORDER BY `cid` DESC");
$stmt = $pdo->prepare($sql);
$stmt->execute(['cid']);
$reviews = $stmt->fetchAll();
foreach ($reviews as $fetch) {
?>
<div class="eachreview" style="background-color:royalblue;">
<?php echo $fetch['rating']; ?>
<?php echo $fetch['uid']; ?>
<?php echo $fetch['message']; ?>
<?php echo date("d/m/Y" ,strtotime($fetch['date'])); ?>
</div>
<?php
}
?>
UPDATED CODE Kinda working but displaying 1/1/1970 and no other data
<?php
include("/var/www/vhosts/myweb.co.uk/httpdocs/PHP/connect.php");
$stmt = $dbh->prepare("SELECT * FROM `reviews` WHERE cid = :cid");
$stmt->execute(array(':cid' => "cid"));
$fetch = $stmt->fetchAll();
{
?>
THIS CODE BELOW WORKS! :)
<?php
include("/var/www/vhosts/my-web.co.uk/httpdocs/PHP/connect.php");
$stmt = "SELECT * FROM `reviews`";
$result = $dbh->query($stmt);
foreach ($result as $fetch) {
?>
<?php echo $fetch['message']; ?>

I got empty result with MySQL

I have tried to show the whole table data in MySQL. Rows are (User_id, first_name, last_name, and dept). There is no result when I refresh the page.
<?php
$servername = "fdb19.awardspace.net";
$username = "2598428_db";
$password = "password";
$dbname = "2598428_db";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql="SELECT * From users";
$result = $conn->query($sql);
while($res = mysqli_fetch_array( $result )) {
echo $res['AverageSatisfactionScore'];
}
?>
Your echo is false.
You need to display a valid column name like :
echo $res['id'] or echo $res['dept'].
just try to print the total count of your result set by
echo mysqli_num_rows( $result );
it will print total number of records in the result
Also try to print whole row by
print_r($res);
insted of using
echo $res['AverageSatisfactionScore'];

my statements returns empty row, eventho there records with values

i have this function:
function get_comments($file_id){
echo $file_id.'<br /><br />';
include 'mysql_connect.php'; ($connection is the dblink variable)
$result = mysqli_query($connection, "SELECT * FROM gastenboek");
print $row_cnt = mysqli_num_rows($result);
echo '<br /><br />';
foreach($result as $item){
echo $item['comment'];
}
}
Ive made 2 records in mij db with values for testing (also in comment). print $row_cnt gives me 2 rows but the foreach statement returns nothing? as if im getting empty rows back from the statement?
thnx for the help
Query is executed but results are not fetched.
Update your code to this:
$result = mysqli_query($connection, "SELECT * FROM gastenboek");
print $row_cnt = mysqli_num_rows($result);
echo '<br /><br />';
while($item = mysqli_fetch_assoc($result)){
echo $item['comment'];
}
You are not fetching the result, see code below:
$result = mysqli_query($connection, "SELECT * FROM gastenboek");
$row = mysqli_fetch_array($result,MYSQLI_ASSOC);
foreach($row as $item){
echo $item['comment'];
}

SQL/HTML - Statements wont load on the HTML page

Me and a friend of mine are working on a school project. We only have a little problem with implementing the SQL into HTML. And as our informatics teacher is of no help, I hope you are.
Our problem is the following.
We have coded our SQL part into the HTML, also guided by online tutorials...
Now our teacher sorted it to this:
<?php
mysql_connect("Localhost", "root", "usbw") or die (mysql_error());
mysql_select_db('aanbod');
$query = "SELECT merk, model FROM Aanbod";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
echo "<tr><td>" . $row["merk"] . "</td>
<td>" . $row["model"] . "</td></tr>";
}
echo "</table>";
mysql_close();
?>
And still getting this error, now on the webpage:
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in D:\USBWebserver v8.67\root\merkmodelshow.php on line 141
Supposing the problem is in this line:
while($row = mysql_fetch_array($result)){
If anyone could help us with this project. It's due soon :C
Regards,
Demiƫn en Bo.
use missed closing ;
try
<?php
mysql_connect("Localhost", "root", "usbw") or die (mysql_error ());
mysql_select_db('aanbod');
$query = "SELECT 'a'=COUNT(nr) FROM Aanbod";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
echo "<tr><td>" . $row['name'] . "</td><td>" . $row['a'] . "</td></tr>";
}
echo "</table>";
mysql_close();
?>
But now the site displays this error:
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in D:\USBWebserver v8.67\root\merkmodelshow.php on line 141
This suggests the problem is in this line:"
while($row = mysql_fetch_array($result)){
Though we weren't able to resolve the problem [yet]
change your query to
$query =" SELECT *, ( select COUNT(nr) FROM Aanbod) as a from Aanbod";

no output in DISTINCT in mysql php

I cannot display in my distinct query in mysql please help.
<?php
mysql_connect("localhost","root","123");
mysql_select_db("sarangani");
$result = mysql_query("SELECT DISTINCT year(posted) AS year FROM news ORDER BY posted");
while($row = mysql_fetch_array($result)) {
echo "$row[year]";
}
?>
If you're sure the query should deliver a result, you need to use the proper way to echo out the result:
while ($row = mysql_fetch_array($result)) {
echo $row['year']; // So don't put it inside double quotes
}