mysql insert turns ★ into ★ - mysql

When I do an insert from my application all ★ (stars) turns into "★"
How can I stop this from happening?
*It works if I directly insert it through phpmyadmin, but not when doing it using this php:
<?php
$servername = "";
$username = "";
$password = "";
$dbname = "";
$inputTime = mysql_escape_string($_POST['inputTime']);
$inputBotid = mysql_escape_string($_POST['inputBotid']);
$inputImage = mysql_escape_string($_POST['inputImage']);
$inputName = mysql_escape_string($_POST['inputSkin']);
$inputStatus = mysql_escape_string($_POST['inputStatus']);
$inputTradeid = mysql_escape_string($_POST['inputTradeid']);
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO items (trade, market_hash_name, status, img, botid, time)
VALUES ('$inputTime', '$inputName', '$inputStatus', '$inputImage', '$inputBotid','$inputTime')";
if ($conn->query($sql) === TRUE) {
echo "Success";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>

You have Mojibake. Search this Q&A for 'Mojibake' to see what causes and how to prevent it.
Since you are into symbols, you might want to use utf8mb4, not just utf8 in MySQL. The former includes various 4-byte Emoji.
Also, see PHP tips. The most important is $mysqli_obj->set_charset('utf8mb4'); right after connecting.

Related

Multiple WHERE find_in_set lookups

I'm wanting to pull steamid from multiple clubs. Website is based on an Invision Community site.
The output is a list of steamids which is imported into our game servers.
My simple pull from one club code below works perfectly:
<?php
$servername = "";
$username = "";
$password = "";
$dbname = "";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT
core_members.steamid,
core_members.member_id,
core_members.name,
core_clubs_memberships.member_id,
core_clubs_memberships.club_id
FROM core_members
INNER JOIN core_clubs_memberships
ON core_members.member_id = core_clubs_memberships.member_id
WHERE
(core_members.steamid IS NOT NULL AND TRIM(core_members.steamid) <> '') AND (find_in_set('1',core_clubs_memberships.club_id))
ORDER BY core_members.member_id ASC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $row["steamid"]. " ". $row["name"];
echo ("\r\n");
}
} else {
echo "0 results";
}
$conn->close();
?>
Whenever I try to add a second group, the code doesn't work, for example I tried:
<?php
$servername = "";
$username = "";
$password = "";
$dbname = "";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT
core_members.steamid,
core_members.member_id,
core_members.name,
core_clubs_memberships.member_id,
core_clubs_memberships.club_id
FROM core_members
INNER JOIN core_clubs_memberships
ON core_members.member_id = core_clubs_memberships.member_id
WHERE
(core_members.steamid IS NOT NULL AND TRIM(core_members.steamid) <> '') AND ((find_in_set('1',core_clubs_memberships.club_id) OR (find_in_set('6',core_clubs_memberships.club_id)))
ORDER BY core_members.member_id ASC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $row["steamid"]. " ". $row["name"];
echo ("\r\n");
}
} else {
echo "0 results";
}
$conn->close();
?>

Last Record from Mysqli Database not showing

Thank you in advance for your help.
For some reason I cannot get the last record from the Database to show when I retrieve information from it.
This was working just fine until I upgraded from PHP 5 to PHP 7.1
<?php
$servername = "xxxxxxxx";
$username = "xxxxxxxx";
$password = "xxxxxxxx";
$dbname = "xxxxxxxx";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//Second Half
$sql="SELECT * FROM Online_Customers order by id DESC LIMIT 100";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
?>
fetch_assoc() removed in PHP 7
change
$row = $result->fetch_assoc()
to
$row = $result->fetch_array()
I got this fixed. I removed the if statement and it's working just fine. If someone is having the same problem please let me know and I can guide you through.

i have issue with my database mySQL select from table

Dears
Please help me in this matter.
I have issue with my MySQL database.
It is working fine if I'm doing inserting records to the table. However, if I'm doing selecting and fetching, the result is 0 despite of the table actually have records.
$sql2 = 'SELECT * FROM `users`';
$result2 = $conn->query($sql2);
echo $result2->num_rows;
if ($result2->num_rows > 0) {
// output data of each row
while($row = $result2->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
please help me guys I tried every thing I almost give up.
thanks,
Maybe you can try this code
<?php
try {
$conn = new PDO('mysql:host=localhost;dbname=contoh', "root", "root");
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sql2 = 'SELECT * FROM `user_ranks`';
$result2 = $conn->query($sql2);
while($row = $result2->fetch()) {
$id = $row['id'];
$username = $row['username'];
$password = $row['password'];
$rank = $row['rank'];
}
}
catch (PDOException $e) {
print "connection or query have a problem: " . $e->getMessage() . "<br/>";
die();
}

Working script stop working (php,mysql)

I've got a weird problem.
I created a website on localhost. Everything was working fine. I could connect to my mysql db without any problems.
I decided to put it on external server to show it to my friends. I changed $username, $dbname etc.
Then I decided to work little with the website back on localhost. And then it started. When I run my script which adds new records to my db (name, message, email) this shows up.
No database selected
Everything is just the same it was before I decided to put it on the server. What can be the problem?
EDIT: Here's the code of connect.php
<?php
$username ="root";
$password = "";
$host = "localhost";
$table = "test";
$conn = new mysqli("$host", "$username", "$password", "$table");
if (!$conn) {
die("Nie udało się połączyć z bazą danych: ".mysqli_connect_error());
}
and here's a script implemented into index.php
<?php
include 'connect.php';
$sql = "SELECT s_id, name, s_like, s_dislike, message FROM suggestions ORDER BY rand()";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$s_id = $row["s_id"];
$s_like = $row["s_like"];
$s_dislike = $row["s_dislike"];
echo '<p>' . $row["message"] . '</p>';
echo '<h1 class="podpis">' . $row["name"] . '</h1>';
mysqli_close($conn);
?>
and here's the cde of comments.php where I see the message about No database selected.
<?php
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$message = $_REQUEST['message'];
$username ="root";
$password = "";
$host = "localhost";
$table = "test";
$conn = mysqli_connect($host, $username, $password, $table);
$query = "INSERT INTO suggestions (name, email, message) VALUES ('$name',
'$email', '$message')";
if (mysql_query($query)) {
echo 'Dodano sugestię!';
header('Location: index.php');
} else{ die(mysql_error());}
mysql_close();
?>

How to display a query from a phpmyadmin that uses MariaDB? My query returns empty

Can someone please explain to me how to display a query from a phpmyadmin that uses MariaDB? When I execute this script, the page returns empty. No error messages, nothing. All blank screenshot of the phpamydmin here.
It seems the commands I shall use in PHP must be different from the used in the conventional phpmyadmin.
I've searched in different sites, but it seems MariaDB is not much used.
<?php
$dbhost = 'localhost:3306';
$dbuser = 'santos1q_test';
$dbpass = 'xxxxxx';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT * FROM test';
mysql_select_db('santos1q_events');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo {$row['event_title']};
echo {$row['event_address']};
}
echo "Fetched data successfully";
mysql_close($conn);
?>