no output in DISTINCT in mysql php - mysql

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
}

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']; ?>

SQL to JSON converts my int to string

I have this in my PHP.
$sql="select * from defaulttime";
$result = mysqli_query($db, $sql);
while ($row = mysqli_fetch_array($result)){
$row_set[] = $row;
}
echo trim(json_encode($row_set));
In my SQL some columns are Integer. but in JSON it shows as String.
can someone please tell me what I am doing wrong?
when I run the PHP, I get the below results. (Note Hour and Minute should be Integers)
[{"name":"Test","Hour": "6" ,"Minute":"45"}],
I want it to show string as string and Int as Int.
{"name":"Test","Hour": 6 ,"Minute":45}
You have to use JSON_NUMERIC_CHECK option in order to get numerics value,
look here: https://lornajane.net/posts/2011/php-returning-numeric-values-in-json
echo trim(json_encode($row_set,JSON_NUMERIC_CHECK));
Thanks all,
I got it working by using this code:
$result = mysqli_query($db, $sql);
while ($row = mysqli_fetch_array($result)){
$row_set[] = $row;
}
$json=json_encode($row_set,JSON_NUMERIC_CHECK);
echo $json;

FIND IN SET not working with PDO

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

using mysqli instead of mysql accessing the DB

please find below a script in php that that fetch data from my database company, the database is in phpMyadmin
I understand that there are secure ways to fetch data. My question is
Can I improve the code below using mysqli to fetch instead of mysql_query.
<?php
get results from database. Can I improve the code below using mysqli to fetch instead of mysql_query in the following line?.
$result = mysql_query("SELECT * FROM customer")
or die(mysql_error());
tables begins here with the customer id and customer name
echo "<table >";
echo "<tr>
<th>ID</th> the customer id
<th>Name</th> the customer name
</tr>";
// loop through results of database query, displaying them in the table
the table below display the columns in html using mysql fetch array. all data is displayed.
while($row = mysql_fetch_array( $result )) {
// echo out the contents of each row into a table
my purpose is embed the php in html in another way using msqli
echo "<tr>";
echo '<td>' . $row['CustomerID'].'</td>';
echo '<td>' . $row['Name']. '</td>';
echo "</tr>";
}
echo "</table>";
?>
my purpose is embed the php in html in another way using msqli
Just replace all the mysql with mysqli
$result = mysqli_query("SELECT * FROM customer");
while($row = mysqli_fetch_array( $result )) {
If you wanna keep it simple
Or do it like this...
/*Your connection info goes here...*/
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT * FROM customer";
if ($result = $mysqli->query($query)) {
echo "<table ><tr><th>ID</th><th>Name</th></tr>";
/* fetch results by row */
while ($row = $result->fetch_row()) {
echo "<tr><td>' . $row['CustomerID'].'</td><td>' . $row['Name']. '</td></tr>';
}
/* free the result set */
$result->close();
}
/* close the connection */
$mysqli->close();

search mysql column with similar_text() matching percentage

i want to echo one value from mysql column which is 90% similar to php variable.
echo only the value that is 90% similar to php value.
i think like clause wont help in this case.
<?php
$x = $_GET[x];
$con=mysqli_connect("example.com","peter","abc123","my_db");
$res = mysqli_query($con,"SELECT * FROM text");
while ($row = mysql_fetch_assoc($res))
{
$string[] = $row['text'];
}
foreach ($string as $y)
{
similar_text($x, $y, $percent);
}
if ($percent>"90")
{
echo $string ;
?>
i think the above code has many mistakes. i welcome some new solution for my task.