I have some php that gets data from the database, and I just want to get it into my view. I had this code from earlier where I just wanted 1 row, however now I want to get all the data into the view.
Do I have to make a loop and get each row into a string and keep appending to it?
here's the php:
if ($result = $mysqli->query("SELECT * FROM myData")) {
$row_cnt = $result->num_rows;
if ($row_cnt > 0) {
$row = $result->fetch_assoc();
$data = $row["data"];
echo $data;
} else {
echo "no data";
}
/* close result set */
$result->close();
}
Something like this should do it, unless you need it in the code later. If that is the case store it to an array.
while($row = $result->fetch_assoc()) {
echo $row["data"];
//$array[] = $row["data"];
}
Try this way :
if ($result = $mysqli->query("SELECT * FROM myData")) {
$row_cnt = $result->num_rows;
if ($row_cnt > 0) {
While ($row = $result->fetch_assoc()) {
$data .= $row["data"]."<br>";
}
echo $data;
} else {
echo "no data";
}
/* close result set */
$result->close();
}
Related
I created the code using mysqli_fetch_assoc with 'while' as shown below.
But it does not work.
if ($ result = mysqli_query ($ dbconn, $ query)) {
It works by here.
while ($ row = mysqli_fetch_assoc ($ result)) {
It does not work from here.
I can not find the wrong part.
If I do not use 'while', it works as follows.
What is the problem?
// not works
$query = "select * from member where f_status='1'";
if ($result=mysqli_query($dbconn, $query)) {
while ($row = mysqli_fetch_assoc($result)) {
if ($row[f_status]==0) {
error("No data");
} else {
echo $row[f_user_id];
echo $row[f_user_name];
}
}
mysqli_free_result($result);
}
// works
$query = "select * from member where f_status='1'";
$result = mysqli_query($dbconn, $query);
$row = mysqli_fetch_assoc($result);
if ($row) {
echo $row[f_user_id];
echo $row[f_user_name];
} else {
error("No data");
}
I think the problem is the way you have iterated while loop and condition to show No data message,
You should try this way:
$query = "select * from member where f_status='1'";
if ($result = mysqli_query($dbconn, $query)) {
if ($result->num_rows) {
while ($row = mysqli_fetch_assoc($result)) {
echo $row[f_user_id];
echo $row[f_user_name];
}
}
else {
error("No data");
}
mysqli_free_result($result);
}
Hope this should solve your issue.
I'm running PHP and MySQL and have the following code:
$data = array();
$result = mysql_query($search_query);
if ($result){
while($row = mysql_fetch_assoc($result)) {
$data[] = $row;
}
if (sizeof($data) > 0) {
//var_dump($data);
echo json_encode($data);
} else {
echo 'empty';
}
}
If my query has no rows I do get empty returned.
But if there's any records I get a Resource has no content in Safari.
But if I uncomment my //var_dump($data); then I do get a nice array of values.
Try this:
// Database connection.
$mysqli = new mysqli('localhost', 'user', 'password', 'db_name');
// Your query.
$search_query = "SELECT * FROM yuor_table";
$data = array();
$result = $mysqli->query($search_query);
if ($result){
while($row = $result->fetch_assoc()) {
$data[] = $row;
}
if (sizeof($data) > 0) {
//var_dump($data);
echo json_encode($data);
} else {
echo 'empty';
}
}
This is very simple solution. I would suggest to use "mysqli".
Table: student
student.php
<?php
function findStudentRecord()
{
//Db connection
$q1 = "select * from student where gender = 'F'";
$r1 = mysqli_query($dbc, $q1);
$total_records = mysqli_num_rows($r1);
$record = array();
if($total_records > 0)
{
while($row1 = mysqli_fetch_array($r1, MYSQLI_ASSOC))
{
$record[] = $row1;
}
}
else
{
//[HERE]
}
return $record;
}
$record = findStudentRecord();
//[HERE]
?>
I want to find female student record but there is no record from my database. How do I return 0 value from function and display "No record found" on my web page in [HERE] section?
if($total_records > 0)
add no else block so it will return an empty array now you can do something like this
$records = findStudentRecord();
if(count($records) === 0) {
echo "No record found";
}
I would not change your function. It returns an array (may be empty) in any case wich is quite consistent.
Instead look at the number of array items returned:
$record = findStudentRecord();
//[HERE]
if(count($record) == 0) {
echo "No record found";
} else {
// what ever
}
I have read several of these posts on the site, but still can't find the answer to my problems. I have a while loop where for every entry in the database the table populates. Although if the table in the database is empty, I want it to display a messaged instead. Any ideas of what is wrong here? (aware of the deprecated tags)
$result = mysql_query("SELECT * FROM blog");
while($row = mysql_fetch_array($result))
{
if(count($row) === 0)
{
echo 'No Data';
}
<table code>
}
Use mysql_num_rows for counting rows from DB.
<?php
if (mysql_num_rows($result) > 0) {
echo '<table>';
while ($row = mysql_fetch_assoc($result)) {
echo '<tr>...</tr>';
}
echo '</table>';
} else {
echo 'No result found';
}
?>
EDIT: updated code for table.
$result = mysql_query("SELECT * FROM blog");
if(mysql_num_rows($result) === 0) {
echo 'No Data';
} else {
while($row = mysql_fetch_array($result)) {
// code
}
}
use mysql_num_rows() to get count of query result rows
$rows = mysql_num_rows($result);
if($rows > 0) {
// do your stuff
}
Use mysql_num_rows for counting number of rows are returned in query.
Try this. mysql_num_rows will check the records in database. On true condition it will allow to execute the while loop other wise else condition will execute.
$result = mysql_query("SELECT * FROM blog");
if(mysql_num_rows($result) > 0) {
echo 'No Result Found';
} else {
while($row = mysql_fetch_array($result)) {
// Here your Data
}
}
If you want to display when no record found then
$result = mysql_query("SELECT * FROM blog");
while($row = mysql_fetch_array($result))
{
if(count($row) == 0 || count($row) < 1)
{
echo 'No Data';
}
else
{
//print records
}
}
So I've connected to my mySQL database, and I am able to view all of the columns within the table that I'm looking to extract information from. Now I need to be able to read all of the values, from specific fields within "ds_users" which are "password" and "username". I'd like to store these into an array and print them out. Here's the code that I have so far:
$result = mysql_query("SHOW COLUMNS FROM ds_users");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
print_r($row);
}
}
also, is there a way to print the results in JSON format?
$result = mysql_query("SELECT username, password FROM ds_users");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$dataArray['user'] = $row->user;
$dataArray['password'] = $row->password;
}
print_r(json_encode($dataArray));
}
On side note: mysql functions are deprecated, you should choose between mysqli or PDO.
Use json_encode() in php
$arr = array();
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$arr[] = $row;
}
}
print_r(json_encode($arr));
To store in array
$dataArray = array();
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$dataArray[] = $row;
}
}
Convert into JSON format using json_encode function
$jsonString = json_encode($dataArray);