Does not fetch all products from db - php

I currently downloaded MAMP on my mac and imported an SQL file containing 589 products with their own product_id.
That worked fine and I can see all the products in "Browse".
I then tested the connection to this db. That worked aswell.
DEFINE('DB_USERNAME', 'root');
DEFINE('DB_PASSWORD', 'root');
DEFINE('DB_HOST', 'localhost');
DEFINE('DB_DATABASE', 'testproducts');
$mysqli = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
if (mysqli_connect_error()) {
die('Connect Error ('.mysqli_connect_errno().')'.mysqli_connect_error());
}
echo 'Connected successfully.';
I then tried to fetch all the products to see if that worked by creting a variable $num to see how many got fetched, but then I only get 295 products with this code:
$res = $mysqli->query("SELECT * FROM ac_product");
$num = 0;
foreach ($res as $r) {
$row = mysqli_fetch_array($res);
$pid = $row['product_id'];
echo "</br>". $pid;
$num++;
}
echo "total: ".$num;
$mysqli->close();
Am I missing something from this? Thought this would just fetch all and not have a limit.
Anyone who have some knowledge of this? Thanks

Related

How to convert timestamp to normal time in php

I'm trying to convert timestamp from my mysql table in to normal time.
I've tried looking for information on the internet but there is only solutions for timestamps that you entered by hand
<?php
//setting header to
header('Content-Type: application/json');
//database
define('DB_HOST', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_NAME', 'bitcoin');
//get connection
$mysqli = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);
if(!$mysqli){
die("Connection failed: " . $mysqli->error);
}
//query to get data from the table
$query = sprintf("SELECT Kaina, Laiko_Kodas FROM localbtc LIMIT 200");
//execute query
$result = $mysqli->query($query);
//loop through the returned data
$data = array();
foreach ($result as $row) {
$data[] = $row /*("SELECT date_format(Laiko_Kodas) FROM localbtc")*/;
}
//free memory associated with result
$result->close();
//close connection
$mysqli->close();
//now print the data
print json_encode($data);
while($row = $result->fetch_assoc()) {
$data = array();
$data2[]= $row['Kaina'];
$datetime=$row['Laiko_Kodas'];
$data2[] = date('M j Y g:i A', strtotime($datetime));
$dtata[]=$data;
}
Try the following code:
<?php
//setting header to
header('Content-Type: application/json');
//database
define('DB_HOST', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_NAME', 'bitcoin');
//get connection
$mysqli = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);
if(!$mysqli){
die("Connection failed: " . $mysqli->error);
}
//query to get data from the table
$query = "SELECT Kaina, Laiko_Kodas FROM localbtc LIMIT 200";
//execute query
$result = $mysqli->query($query);
//loop through the returned data
$data = array();
foreach ($result as $row) {
$row['Laiko_Kodas'] = date('H:i:s', strtotime($row['Laiko_Kodas']));
$data[] = $row;
}
//free memory associated with result
$result->close();
//close connection
$mysqli->close();
//now print the data
print json_encode($data);

How to create a "Record Not Found" error message

I am trying to add some code to the sample below, that will allow a "Record Not Found" error to be generated - if a record is not found:
<?php
header('Content-type=application/json; charset=utf-8');
//database constants
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', '');
define('DB_NAME', 'test');
//connecting to database and getting the connection object
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
//Checking if any error occured while connecting
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
die();
}
$stmt = $conn->prepare('SELECT * FROM donor WHERE city =? AND gender=? AND
bloodgroup=? AND age=?;');
$stmt->bind_param('ssss',$_GET['city'],$_GET['gender'],$_GET['bloodgroup'],$_GET['age']);
$stmt->execute();
//binding results to the query
$stmt->bind_result($id, $name, $gender, $city, $contact, $bloodgroup,$age);
$donors = array();
//traversing through all the result
while($stmt->fetch()){
$temp = array();
$temp['id'] = $id;
$temp['name'] = $name;
$temp['gender'] = $gender;
$temp['city'] = $city;
$temp['contact'] = $contact;
$temp['bloodgroup'] = $bloodgroup;
$temp['age'] = $age;
array_push($donors, $temp);
}
//displaying the result in json format
echo json_encode($donors);
?>
Where would you suggest I put the code to enable the "error not found" error?
You can use $stmt->num_rows or you can check it with below code
if (empty($donors)) {
echo json_encode(array('msg' => 'Record Not Found'));
}else{
echo json_encode($donors);
}

Php code not giving response

The following code does show anything in the browser . All the database name and table name are correct . While adding a line echo json_encode($temp) in the while loop displays result as objects.
<?php
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', '');
define('DB_NAME', 'sample_userdata');
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
die();
}
$stmt = $conn->prepare("SELECT questions FROM sample_questions;");
$stmt->execute();
$stmt->bind_result($post);
$userdata = array();
while($stmt->fetch()){
$temp = array();
$temp['questions'] =$post;
array_push($userdata,$temp);
}
echo json_encode($userdata);
?>
When you use json_decode() function it creates object for array with keys
$x['test1'] = 1;
$x['test2'] = 2;
echo json_encode($x);
// Result: {"test1":1,"test2":2}
You can remove keys using array_values()
$x['test1'] = 1;
$x['test2'] = 2;
echo json_encode(array_values($x));
// Result: [1,2]

Null return from localhost php coding

I set up my localhost, I have all my relevant files in place. I run the php where I run the sql query, it returns NULL values. But it should not. I visit to the directory of the link via browser.
Can someone point me in the return direction, why it is returning NULL? I ran the SQL query, and works fine on my phpmyAdmin
<?php
header('Content-Type: application/json');
define('DB_HOST', '127.0.0.1');
define('DB_USERNAME', 'myusername');
define('DB_PASSWORD', 'Ihavenopassword');
define('DB_NAME', 'mytablename');
$mysqli = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);
//$conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
if(!$mysqli){
die("Connection failed: " . $mysqli->error);
}
//query to get data from the table
$query = sprintf("SELECT playerid, score FROM score ORDER BY playerid");
//execute query
$result = $mysqli->query($query);
//loop through the returned data
$data = array();
foreach ($result as $row) {
$data[] = $row;
}
//free memory associated with result
$result->close();
//close connection
$mysqli->close();
//now print the data
print json_encode($data);
$result is not what you expect. You need to fetch every item, with fetch_assoc for example:
//loop through the returned data
$data = array();
while ($row = $result->fetch_assoc()) {
$data[] = $row;
}

Global variable in query php/mysql

I have a website with 3 dropdown menus that loop through a database in mysql to get 50 bands in a list you can chose. I have made it so if you vote for band number 1, he gets 10 points, number 2, 6 points etc.. Now i have the problem that you can vote for 3 the same bands bcs those dropdown are each looping trough the database. I want to make a global variable so i can put in the query from dropdown 1 of 2 that it doesnt show the chose in dropdown 1.. so like "WHERE NOT LIKE 'variable'"
Here is my code for dropdown 1:
<?php echo '<select name="bands1">';
echo '<option>Kies een band</option>';
$sql="SELECT * FROM bands ORDER BY band";
$result = mysqli_query($db_link, $sql);
if (!$result){
die ("Database connection failed!");
}
while($row = mysqli_fetch_assoc($result)){
echo '<option value="' . $row['BandID'] . '">' . $row['band'] . '</option>';
}
echo '</select>';
?>
this is for the 2 other dropdown menus the same only the select name is different
this is my connection file that i included at the top of the html file:
<?php
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', '');
define('DB_NAME', 'mysql_enquete');
$db_link = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME) or die ("Verbindingsfout");
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
if ($result = $mysqli->query("SELECT DATABASE()")) {
$row = $result->fetch_row();
printf("", $row[0]);
$result->close();
}
?>
Somebody on stackoverflow that can help me?

Categories