this is my code when i run it
$name=$_GET['name'];
$strSQL = "SELECT * FROM category WHERE name LIKE =" .$name;
$rs = mysql_query($strSQL);
// Loop the recordset $rs
$response = array();
while($row = mysql_fetch_array($rs)) {
$product = array();
$product["id"] = $row["id"];
$product["name"] = $row["name"];
array_push($response, $product);
}
echo json_encode($response);
}
error:mysql_fetch_array() expects parameter 1 to be resource, boolean given in while($row = mysql_fetch_array($rs))
it shows error on this line help me to fix this
you have inserted wrong syntax ....use this
$strSQL = "SELECT * FROM category WHERE name LIKE '%$name'";
You need to open a connection to your database. Something like:
<?php
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$name=$_GET['name'];
$strSQL = "SELECT * FROM category WHERE name LIKE '%" .$name . "%'";
// $strSQL = "SELECT * FROM category WHERE name = '" .$name . "'";
$rs = mysql_query($strSQL, $conn);
// Loop the recordset $rs
$response = array();
while($row = mysql_fetch_array($rs)) {
$product = array();
$product["id"] = $row["id"];
$product["name"] = $row["name"];
array_push($response, $product);
}
echo json_encode($response);
?>
Also if you are going to use "LIKE" in your sql, you don't use "=".
It is because You are not sending Query matched with your datatype of the name field.
Try below syntax with ''
$strSQL = "SELECT * FROM category WHERE name LIKE '" .$name . "'";
IF you still getting the same error then try this code
$name=$_GET['name'];
$strSQL = "SELECT * FROM category WHERE name LIKE '" .$name . "'";
$rs = mysql_query($strSQL);
if(mysql_num_rows($rs) > 0){
// Loop the recordset $rs
$response = array();
while($row = mysql_fetch_array($rs)) {
$product = array();
$product["id"] = $row["id"];
$product["name"] = $row["name"];
array_push($response, $product);
}
echo json_encode($response);
}
}
else{ echo json_encode(array('msg'=>"No records found")); }
Related
I am trying to create a single JSON file displaying data from two different tables. I can manage to fetch them, however I don't understand how to concatenate the two arrays. I am new to JSON. Any suggestions/tutorials would be very helpful. Thanks in advance.
Here the expected JSON:
{
"array1":[
{"days":"1","id":"1","image":"image1.jpg, image2.jpg, image3.jpg, image4.jpg"},{"days":"2","id":"1","image":"elephanta.jpg,image2.jpg,image1.jpg,imagica.jpg"},
{"days":"3","id":"1","image":"image3"},{"days":"4","id":"2","image":"image4"}
],
"array2":[
{"id":"1","image_1":"image1.jpg"},
{"id":"2","image_1":"image2.jpg"},
{"id":"3","image_1":"image3.jpg"}
]
}
but the JSON that I get is this:
{
"array1":[
{"days":"1","id":"1","image":"image1.jpg, image2.jpg, image3.jpg, image4.jpg"},{"days":"2","id":"1","image":"elephanta.jpg,image2.jpg,image1.jpg,imagica.jpg"},
{"days":"3","id":"1","image":"image3"},{"days":"4","id":"2","image":"image4"}
]},
{"array2":[
{"id":"1","image_1":"image1.jpg"},
{"id":"2","image_1":"image2.jpg"},{"id":"3","image_1":"image3.jpg"
}
]}
PHP code:
<?php
//open connection to mysql db
$connection = mysqli_connect("localhost","root","","test") or die("Error " . mysqli_error($connection));
//fetch table rows from mysql db
$sql = "select * from image_data";
$sql1 = "select * from one";
$result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));
$result1 = mysqli_query($connection, $sql1) or die("Error in Selecting " . mysqli_error($connection));
//create an array
$imageArray = array();
$imageArray1 = array();
$imageArray["array1"] = array();
$imageArray1["array2"] = array();
while($row =mysqli_fetch_array($result))
{
$tmp = array();
$tmp["days"] = $row["id"];
$tmp["id"] = $row["place_id"];
$tmp["image"] = $row["image"];
array_push($imageArray["array1"], $tmp);
// $imageArray[] = $row;
}
while($row =mysqli_fetch_array($result1))
{
$tmp = array();
$tmp["id"] = $row["id"];
$tmp["image_1"] = $row["image"];
array_push($imageArray1["array2"], $tmp);
// $imageArray[] = $row;
}
echo json_encode($imageArray);
echo ",";
echo json_encode($imageArray1);
//close the db connection
mysqli_close($connection);
?>
If you want the 2 sub arrays to belong to the same array then use only one array and address the 2 sub arrays like this when loading them
$imageArray["array1"][] = $tmp;
and
$imageArray["array2"][] = $tmp;
Amended code
<?php
//open connection to mysql db
$connection = mysqli_connect("localhost","root","","test") or die("Error " . mysqli_error($connection));
//fetch table rows from mysql db
$sql = "select * from image_data";
$sql1 = "select * from one";
$result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));
$result1 = mysqli_query($connection, $sql1) or die("Error in Selecting " . mysqli_error($connection));
//create an array
$imageArray = array(); // <- removed other arrays
while($row =mysqli_fetch_array($result))
{
$tmp = array();
$tmp["days"] = $row["id"];
$tmp["id"] = $row["place_id"];
$tmp["image"] = $row["image"];
$imageArray["array1"][] = $tmp; // <- address array like this
}
while($row =mysqli_fetch_array($result1))
{
$tmp = array();
$tmp["id"] = $row["id"];
$tmp["image_1"] = $row["image"];
$imageArray["array2"][] = $tmp; // <- address array like this
}
echo json_encode($imageArray); // <- only one json_encode
//close the db connection
mysqli_close($connection);
?>
I would have done it this way using stdObject and selecting the specific data from the tables, and using mysqli_fetch_obect() so there is a lot less you have to do woman-draulically
<?php
//open connection to mysql db
$connection = mysqli_connect("localhost","root","","test") or die("Error " . mysqli_error($connection));
//fetch table rows from mysql db
// only select what you want
$sql = "select id,place_id,image from image_data";
$sql1 = "select id,image from one";
$result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));
$result1 = mysqli_query($connection, $sql1) or die("Error in Selecting " . mysqli_error($connection));
//create an array
$imageObject = new stdObject();
while($row =mysqli_fetch_object($result))
{
$imageObject->array1[] = $row;
}
while($row =mysqli_fetch_object($result1))
{
$imageObject->array2[] = $row;
}
echo json_encode($imageObject);
?>
I don't know why but it disregards the first row of the table.
Please help:
$row = 0;
$con = mysqli_connect("localhost", "root", "root", "db");
$query = "select * from user_accounts";
$result = mysqli_query($con, $query);
$row = mysqli_fetch_assoc($result);
while($row = $result->fetch_array())
{
echo $row['ID'] . " " . $row['Username'];
echo "<br />";
}
Two things, you're declaring an un-needed variable ($row = 0;)and you're fetching twice, which you shouldn't do:
$con = mysqli_connect("localhost", "root", "root", "db");
$query = "select * from user_accounts";
$result = mysqli_query($con, $query);
while($row = mysqli_fetch_assoc($result))
{
echo $row['ID'] . " " . $row['Username'];
echo "<br />";
}
$row = mysqli_fetch_assoc($result);
while($row = $result->fetch_array())
So, you fetch one, ignore it and then continue fetching
<?php
include"../database_conn.php";
$con=mysqli_connect("localhost","admin","123456","ayurveadic");
$query_pag_data = "SELECT id from diseases";
$result_pag_data = mysql_query($query_pag_data) or die('MySql Error' . mysql_error());
while ($row = mysql_fetch_array($result_pag_data)) {
$diseases_id = $row['id'];
$result = mysqli_query($con,"SELECT $diseases_id FROM treatment WHERE gender_id = '10' AND diseases_id = '$diseases_id'");
$row_gid = mysqli_fetch_array($result);
if ($row_gid == TRUE){
$sl_dise = mysqli_query($con,"SELECT Diseases_type, id FROM diseases WHERE id = '$diseases_id'");
$rowss = array();
while($r = mysqli_fetch_assoc($sl_dise)) {
$rowss[] = $r;
}
print json_encode($rowss);
}
}
Output is:
[{"Diseases_type":"fever","id":"114"}][{"Diseases_type":"rhrh","id":"123"}]
How can i get this output:
[{"Diseases_type":"fever","id":"114"},{"Diseases_type":"rhrh","id":"123"}]
You need to initialize the JSON array before the outer loop, and print it at the very end, not each time through.
<?php
include"../database_conn.php";
$con=mysqli_connect("localhost","admin","123456","ayurveadic");
$query_pag_data = "SELECT id from diseases";
$result_pag_data = mysql_query($query_pag_data) or die('MySql Error' . mysql_error());
$rowss = array();
while ($row = mysql_fetch_array($result_pag_data)) {
$diseases_id = $row['id'];
$result = mysqli_query($con,"SELECT $diseases_id FROM treatment WHERE gender_id = '10' AND diseases_id = '$diseases_id'");
$row_gid = mysqli_fetch_array($result);
if ($row_gid == TRUE){
$sl_dise = mysqli_query($con,"SELECT Diseases_type, id FROM diseases WHERE id = '$diseases_id'");
while($r = mysqli_fetch_assoc($sl_dise)) {
$rowss[] = $r;
}
}
}
print json_encode($rowss);
Here is you optimised code
<?php
include"../database_conn.php";
$con=mysqli_connect("localhost","admin","123456","ayurveadic");
$query_pag_data = "select diseases.Diseases_type,diseases.id from diseases inner join treatment on treatment.diseases_id = diseases.id where treatment.gender_id = '10' ";
$result_pag_data = mysql_query($query_pag_data) or die('MySql Error' . mysql_error());
$rowss = array();
while ($row = mysqli_fetch_assoc($result_pag_data)) {
$rowss[] = $row;
}
print json_encode($rowss);
?>
Put your
print json_encode($rowss);
after end of thiswhile ($row = mysql_fetch_array($result_pag_data)) {
Also remove this $rowss = array(); array initialization
The following query is returning the result I expected:
$link=mysqli_connect('localhost','user','pass');
if(!$link){
echo "No connection!";
exit();
}
if (!mysqli_set_charset($link, 'utf8'))
{
echo 'Unable to set database connection encoding.';
exit();
}
if(!mysqli_select_db($link, 'database')){
echo "No database";
exit();
};
$res = $link->query("SELECT rules FROM xmb9d_viewlevels WHERE id=10");
while ($row = $res->fetch_array()) {
echo " cenas = " . $row['rules'] . "\n";
};
But, since I'm using Joomla 2.5.16 and I'm trying to keep its syntax, I tried:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$res = $db->query("SELECT rules FROM #__viewlevels WHERE id=10");
while ($row = $res->fetch_assoc()) {
echo " cenas = " . $row['rules'] . "\n";
};
This isn't working. It is only displaying the text " cenas =".
What is wrong with this code?
Try the following:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName('rules'))
->from($db->quoteName('#__viewlevels'))
->where($db->quoteName('id')." = ".$db->quote('10'));
$db->setQuery($query);
$results = $db->loadObjectList();
foreach ( $results as $result) {
echo " cenas = " . $result->rules;
}
I am having an issue with a while statement only returning one result. this is my first time trying to display product information from several tables and I don't know what I'm doing wrong. here is the code:
<?php
require('./includes/config.inc.php');
require(MYSQL);
$sql = sprintf("SELECT * FROM tea");
$res = mysqli_query($dbc, $sql);
if(!$res){
die('Could not complete query: '.mysqli_error($dbc));
} else {
echo 'Success!<br />';
while($row = mysqli_fetch_array($res)){
$table = $row['category'];
$inum = $row['item_number'];
echo $table.' '.$inum.'<br />';
$fetch = sprintf("SELECT sub_category, item_name, description FROM $table WHERE item_number ='$inum'");
$fetchRes = mysqli_query($dbc, $fetch);
if(!$fetchRes){
die('Could not fetch: '.mysqli_error($dbc));
} else {
while($fetchRow = mysqli_fetch_array($fetchRes)){
$subCat = $fetchRes['sub_category'];
$iNumber = $inum;
$iname = $fetchRes['item_name'];
$desc = $fetchRes['description'];
echo $id.' '.$subCat.' '.$iNumber.' '.$iname.' '.$desc.'<br />';
}
}
}
}
Inside your while loop, you should be using the $fetchRow variable as the array from which to grab columns, such as 'sub_category'.
IS
$subCat = $fetchRes['sub_category'];
$iNumber = $inum;
$iname = $fetchRes['item_name'];
$desc = $fetchRes['description'];
Needs to be: Res to Row
$subCat = $fetchRow['sub_category'];
$iNumber = $inum;
$iname = $fetchRow['item_name'];
$desc = $fetchRow['description'];