This question already has answers here:
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in
(3 answers)
Closed 7 years ago.
I'm selecting data from MySQL. My code looks like this:
$sql = "SELECT oznacenie_odpadu FROM zdroj_dat ORDER by ID ASC";
if ($result = $conn->query($sql)) {
// fetch associative array
while($row = $result->fetch_assoc()) {
echo '<option value="'.$row['oznacenie_odpadu'].'" >'.$row['oznacenie_odpadu'].'</option>';
}
}
This part of code is in my code multiple times. I'm calling it 5times only string "oznacenie_odpadu" is changing. Therefore I made function:
function select($data) {
$sql = "SELECT rozmer FROM zdroj_dat ORDER by id ASC";
if ($result = $conn->query($sql)) {
// fetch associative array
while($row = $result->fetch_assoc()) {
echo '<option value="'.$row[$data].'" >'.$row[$data].'</option>';
}
}
}
Calling it with select("somevalue");
Syntax is ok because I didn't change anything but when I load the page the data from database are not retrieved.
You have a variable scope issue. $conn is not available inside of your function. You need to pass it as a parameter so ic an be used inside of your function.
function select($conn, $data) {
$sql = "SELECT rozmer FROM zdroj_dat ORDER by id ASC";
if ($result = $conn->query($sql)) {
// fetch associative array
while($row = $result->fetch_assoc()) {
echo '<option value="'.$row[$data].'" >'.$row[$data].'</option>';
}
}
}
Call it:
select($conn, "somevalue");
Related
This question already has answers here:
How to get comma separated values from database [duplicate]
(3 answers)
Converting MySQL results into comma separated values
(4 answers)
Can I concatenate multiple MySQL rows into one field?
(16 answers)
Closed 3 years ago.
I'm creating a small website using PHP, which is generally a site for showcasing the hospital, and I modified the code given in this example:
https://www.w3schools.com/php/php_mysql_select.asp
<?php
$query = "SELECT * FROM emp WHERE type = 'woman' ";
$result = mysqli_query($db, $query);
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
$cat = $row["cat"] . ',';
echo $cat;
////<---- echo on while (Loop)
}
}
The expected output would be as follows:
Output: 35,36
But I changed the code with the link above and it is as follows:
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
$cat = $row["cat"] . ',';
}
echo $cat;
///// <---- echo Out of While (Loop)
}
Output: 35
My expecting output would be 35, 36 outside of "while" using "echo".
What code do you recommend to output "35,36" the same code above?
You can try the below code to achive your requirement
$data = array();
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result)) {
$data[] = $row["cat"];
}
}
echo implode(",",$data);
This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 5 years ago.
lets see code
public function chekfactor( $factor,$user) {
$arrfactor=preg_split('/<td>/',$factor);
$arrusers=preg_split('/<td>/',$user);
$tedstore=count($arrusers) ;
$tedkala=count($arrfactor) ;
$inttedstore=(int) $tedstore;
$inttedkala=(int)$tedkala;
$afa="0";
$a=(int)$afa;
for (;$a<$inttedstore;){
$storeusername=$arrusers[$a];
$faktorss=$arrfactor[$a];
$tablename='devlist'.$storeusername;
echo $tablename;
echo $faktorss;
$result = $this->conn->query("SELECT id FROM'".$tablename."' WHERE prcode='".$faktorss."' ");
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$user = array();
$user[$faktorss] = $row["id"];
return $user;
}
}else {
return "no";
}
$a++;
}
and give to me the errors
trying to get property of non-object on line 543 means " if
($result->num_rows > 0) { "
why happen and how fix it ?
change the line
if ($result->num_rows > 0)
for
if (!empty($result)) // make sure it is having value then use `num_rows` on it
The issue is that you are trying to access to num_rows property when $result is empty.
i guess your query is not working because of your quotes and space
"SELECT id FROM'".$tablename."' WHERE prcode='".$faktorss."'";
query will print like
SELECT id FROM'table_1' WHERE..
you can change like
"SELECT id FROM ".$tablename." WHERE prcode=".$faktorss."";
You can do
if($result && !empty($result)){
$num_rows = $result->num_rows;
if($num_rows > 0){
//the logic here
}
}
This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 6 years ago.
I cant retrieve value from database. Below is my query
<?php
$latestcover = queryTable("SELECT id, filename, title, thumbnail FROM borneo_ezone ORDER BY id DESC LIMIT 1");
foreach ($latestcover as $key){
echo '<p><img src="'.$key[3].'" alt="Borneo Ezone '.$key[0].'" /></p>';
echo '<p class="more">'.$key[0].'th issue of Borneo Ezone. Read more »</p>';
}
?>
Receive warning for PHP deprecated. How can I use mysqli to retrieve the query.
this my queryTable function
function queryTable($ask){
$query = mysqli_query($dbhandler,$ask); //query the db
$resArr = array(); //create the result array
while($row = mysqli_fetch_array($query)) { //loop the rows returned from db
$resArr[] = $row; //add row to array
}
return $resArr;
}
$latestcover = "SELECT id, filename, title, thumbnail FROM borneo_ezone ORDER BY id DESC LIMIT 1";
if ($result = $mysqli->query($latestcover)) {
while ($row = $result->fetch_row()) {
echo '<p><img src="'.$row[3].'" alt="Borneo Ezone '.$row[0].'" /></p>';
echo '<p class="more">'.$row[0].'th issue of Borneo Ezone. Read more »</p>';
}
$result->close();
}
try this code
This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 8 years ago.
ItemDb class
public function getRandomItem() {
$query = "SELECT * FROM `items` ORDER BY RAND() LIMIT 2";
return $this->query($query);
}
Index.php
$item = new Item();
$result = $item->getRandomItem();
while ($row = $result->fetch_assoc()) {
foreach ($row as $key => $value) {
//I want to put them in a array but the two items need to be separated
}
}
I get two different items out of the database how can I split them and put them in one array separated like:
$array[$key][$value]
Sorry for my English its my second language and I hope you guys understand me.
You need to declare $itemArray[$key] before you use it. So your code needs to look like
$itemArray = array();
while ($row = $result->fetch_assoc()) {
foreach ($row as $key => $value) {
if(!isset($itemArray[$key])) {
$itemArray[$key] = array(); //Declare it
}
$itemArray[$key][] = $value;
}
}
This question already has answers here:
Object of class mysqli_result could not be converted to string
(5 answers)
Closed 1 year ago.
I'm trying to query the DB with mysqli and then fetch the result, but it's throwing an Object of class could not be converted to a string error.
Here's what I am trying to accomplish:
<?php
include ('conn.php');
$query= "select value from the_table where item = 'url'";
$result = mysqli_query($conn, $query);
?>
And then I am trying to populate a link with the $result:
This is the Link to the URL
I saw this post: PHP and MySQL error: Object of class mysqli_result could not be converted to string
So, I tried to format the echo with echo $result->fetch_object()->url;, but that didn't work.
I'm not sure if I have to fetch the result and then throw it into a look with a mysqli_fetch_array() and if so, how do I get it to populate that the value outside of the loop?
Assuming that $db is your database connection link you can perform the query:
$result = mysqli_query($db, 'select name from fruits');
Then you can get name using procedural style:
echo mysqli_fetch_object($result)->name;
Or object style:
echo $result->fetch_object()->name;
In your code you're trying to output url property which is not defined - as we can see in your query.
Try some thing like this:-
$query= "select value from the_table where item = 'url'";
$result = mysqli_query($conn, $query) or die ("Couldn't execute query.");
// use returned data
while($row = mysqli_fetch_assoc($result))
{
echo $row['value'];
}
OR
while ( $row = mysqli_fetch_assoc( $result ) )
{
foreach ($row as $key => $value)
{
print ($row . " = " . $value . "\n");
}
print("================");
}