Some table values returning null? - php

I have a database that stores a website's news articles. Each row contains a time, title, author, and a content field. Javascript handles the information passed from php and displays it via togglable buttons. For some reason even though I see the data in Mysql Workbench some content fields are returning null. Why would this be?
Here is my code..
var json = <?php
$servername = "sfxworks.net"; //Currently pulls from my webserver. Pass sql credentials or write a php file that I can include that wont show on github.
$username = "foo";
$password = "bar";
$dbname = "foodbarmmmmmmmmm";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$rows = array();
$sql = "SELECT title, author, date, content FROM News";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$rows[] = $row;
}
} else {
echo "0 results";
}
$conn->close();
print json_encode($rows);
?>;
For some reason, after php processes this with error reporting on, I just get null values at random. In the same places, but random.

There are some values in the table with interesting apostrophes..
After removing [ ’ ] from the string and replacing it with [ ' ] the value is no longer null.

Related

copy data from 1 table to another table with checking for equal data

I have been working on migrating a webshop. The company that did this did something wrong and I want to fix it myself.
The problem is:
I have 2 tables 1 from the old webshop (let's call this one A) and one from the new one (and we call this one B).
Now the short description needs to be copied from table A to table B. The column name in table A is meta_description and in table B it is description_short. but I do not want to just copy and be done. Because when I do that everything will not be in order. Cause both tables are sorted differently.
I got this far:
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO pskd_product_lang (description_short)
VALUES ($desc)";
// use exec() because no results are returned
$conn->exec($sql);
echo "New record created successfully";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
where $desc is made here:
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM oc_product_description";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr><th>ID</th><th>Name</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
$name = $row["name"];
$desc = $row["meta_description"];
echo "<tr><td>".$name."</td><td>".$desc."</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
as you can see the target table (B) is called pskd_product_lang
The source table (A) is called oc_product_description I tried were statements and a lot of other things I found on the internet but nothing worked...
If someone could help me out or point me in the right direction I would be grateful!!
Thanks in advance

How to output php search result to html table?

Want to output search result from database into an html table, but all I'm getting is a printed array. Fairly certain that's due to "print_r", but how do I output to html table instead and getting shown more than just the first result?
My html table using data from my database works fine - but there I'm just using a "SELECT *" with no user input.
I've tried playing around with my php-code that works with just the "SELECT *", but no luck.
<?php
$servername = "test";
$username = "test";
$password = "test";
$dbname = "test";
$journal = (isset($_SESSION["journal"])) ? $_SESSION["journal"]
: null;
$conn = mysqli_connect($servername, $username, $password, $dbname) or die("connection failed: " . mysqli_connect_error());
mysqli_select_db($conn, $dbname) or die("something went wrong");
if(isset($_POST["form_submit"]))
{
$journal =$_POST["journal"];
$stmt = $conn->prepare("SELECT * FROM straffesager WHERE journalnummer=?");
$stmt->bind_param("s",$journal);
$stmt->execute();
$val = $stmt->get_result();
$row_count= $val->num_rows;
if($row_count >0)
{
$result =$val->fetch_assoc();
print_r($result);
}
else{
echo "<br><br>Den indtastede information matcher ikke sager i databasen.";
}
$stmt->close();
$conn->close();
}
?>
This is my result: "Array ( [idnumber] => 4 [journalnummer] => 17-1717171 [status] => lukket".
The result above is just text. I'd like it to be in the html table I'm using somewhere else as well.
How do I output the result from my code to an HTML table and have more than one search result being shown?
Thanks for helping out a rookie.
instead of
$result =$val->fetch_assoc();
print_r($result);
you should print your table with help of your $result variable
like this
echo "<table><tr><th>HEAD1</th><th>HEAD2</th><th>HEAD3</th></tr>";
while ($row = $val->fetch_assoc()) {
printf("<tr><td>%s</td><td>%s</td><td>%s</td></tr>",
$row['idnumber'],$row['journalnummer'],$row['status']);
//it will put $row instead of %s
}
echo "</table>";

PhP MySQL get ceratin items from database and echo them

I need some help and I can't figure it out,i tried searching the internet but I found nothing
So I'm using this code from w3schools
<?php
$servername = "localhost"; //Obviously this are set to my parameters
$username = "username"; //Obviously this are set to my parameters
$password = "password"; //Obviously this are set to my parameters
$dbname = "myDB"; //Obviously this are set to my parameters
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM People";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
//Some code goes here
}
} else {
echo "0 results";
}
$conn->close();
?>
How can I modify this code to echo certain items only?
EXAMPLE
I have 10 items in talbe People (Table people contains id,age,name,gender)
I want to echo out 3 out of 10 People by using their ID which are 1 trough 10
I know i can use this AND id IN(1, 5, 9)"; This just echoes them out 1 after another
Is there a way to echo out id 1 goes here , id 5 goes here and than id 9 goes there like in 3 different places with 3 different codes or something? is this even possible?
You could use:
class people
{
public function people()
{
$sql = $this->conn->prepare("SELECT * FROM `people`");
$sql->execute();
$result = $sql->fetch(PDO::FETCH_OBJ);
return $result;
}
}
then when you want certain information you could simply use in say index.php:
$fetch = new people();
$info = $fetch->people();
you can then run simple echo's such as $info->name or $info->id etc etc..
Depending on how you're searching for the user you could just add
WHERE `userid` = :id
$sql->bindParam(':id', $userid);
and add $userid into the people($userid)
But this will vary depending on how you're pulling out specific users.
use this code
<?php
//using PDO
try
{
$conn = new PDO('mysql:dbhost=myhost;dbname=mydbname;', 'user','pass');
}
catch (PDOException $e)
{
echo $e->getMessage();
}
$getId = $conn->query("
SELECT * FROM users
WHERE id = 1
AND id = 5
AND id = 9
");
while ($ids = $getId->fetch(PDO::FETCH_OBJ)
{
echo $getId->id . '<br>';
}

how to get diffrent data from single column in mysql and php

i want to store multiple data with comma separated in single column and get back data with the help of php in different different line
like in column view it will looks like this-- toothbrush-1234,glass-1234,
in up there 1234 is product id
i want to get this data in php with separated values each line with its all details.
like
<?php $sql="select product_name form products where product_id=1234" ?>
So is it possible to get data and separat it by - and , from single column.
You want to display your comma separated data into separate rows. You can try this.
<?
//Here is the complete example of your code by using MYSQLi Object Oriented:
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "select product_id,product_name form products where product_id=1234";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
$yourData = array();
while($row = $result->fetch_assoc()) {
$yourData[$row['product_id']] = explode(",",$row['product_name']);
}
if(count($yourData) > 0){
foreach ($yourData as $key => $value) {
foreach ($value as $productname) {
echo "Product ID >> ".$key. " -- Product Name >> ".$productname;
}
}
}
}
else
{
echo "0 results";
}
$conn->close();
?>
Side Note: If you are using mysql_* in your application than i suggest you to use mysqli_* or PDO because mysql_* is deprecated and not available in PHP 7.

Value returned from mysqli db call in php doesn't return values with newline chara

Ok so for some reason when I try and get values from a cell in my table with type TEXT it returns nothing, even thought there is text in the cell. It might be because there are new line characters in the text. In fact I'm certain that's the reason cause the only time things aren't return from the table is when there are newlines in it.
Here is the code where I get the data from my db:
function getRecord($cmo_name,$username,$password){
$i = 0;
$servername = "localhost";
$db = "my_db";
$return_array[] = "";
$conn = new mysqli($servername, $username, $password, $db);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM `my_table` WHERE `Name` = '".$name."'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()){
foreach($row as $value)
array_push($return_array, $value);}
}
$conn->close();
return $return_array;
}
The above code returns nothing for the TEXT column that includes the newlines.
Help please!!!
Wrap your query result in PHP's TRIM function to strip the newline and returns out:
http://php.net/manual/en/function.trim.php

Categories