Retrieve MySQL multiple Query with PHP and Process - php

So far I have this code:
<?php
include('config.php');
$sql = "SELECT senderID as receiverID, msg, time
FROM msg
WHERE senderID = 1";
$result = mysql_query($sql);
if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}
if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
while ($row = mysql_fetch_assoc($result)) {
echo $row["receiverID"];
echo $row["msg"];
echo $row["time"];
}
mysql_free_result($result);
?>
Now this simply fetch's the info and stuff, it works the only problem is the processing part. See, I can probably do this so easily in Python, but I am new in PHP, so, if someone could help me, because this is a example of the thing I get returned:
1Hello! How are you?!?!?!876765981admin to user 3574769
Now, I actually was wondering how I would be able to serperate these into its own variable or just print seperately, I am not quiet sure, this is how its suppose to look for a python thing:
firstRow = ['1','Hello! How are you?!?!?!','876765981']
secondRow = ['1','admin to user 3','574769']
Now, I am not so sure about the php structure or code for this.

In php,what you got was:
[
1 => ["receiverId"=>'1',
"msg"=>'Hello! How are you?!?!?!',
"time"=>'876765981'],
2 => ["receivedId"=>'1',
"msg"=>'admin to user 3',
"time"=>'574769']
]
To get the first row
$firstrow = $result[1]
To print it
echo $firstrow["msg"];
The $result is a nested associative array

Related

While loop not working for every index

I am new to php, but I had used while loop and it worked. But in this case i don't know why it is not working.
I am using following query to fetch data from mysql data base.
$query="SELECT * FROM `groupMembers` WHERE group_id = '$group_id'";
if ($result = mysql_query($query)) {
# code...
echo mysql_num_rows($result);
}
It prints 2 as number of rows. But the problem is in following while loop:-
while ($data = mysql_fetch_array($result)) {
# code...
echo $data['member_id'];
}
I prints only one member's id. (the second one == member_2)
The above query returns 2 rows when run in mysql :-
member_id | group_id
----------|----------
member_1 | group_1
member_2 | group_1
i had same problem,i was calling following code two times
mysql_fetch_array($result)
Show more code - what happens before while
first try mysql_num_rows in how many rows is returns.
if "0" then can't fetch any record from your mysql and check your sql query. if return no of row then try below code:
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. "<br>";
}
} else {
echo "0 results";
}
Enjoy Coding.
That is because when it create array for your MySQL query it will change me with to same value "group_1"
Try to convert mysqli_fetch_array to mysqli_fetch_assoc instead
$query="SELECT * FROM `groupMembers` WHERE group_id = '$group_id'";
Here,$group_id can't be place between '';
use $results->fetch_assoc(); which get your data
<?php
// your database connection
if ($results->num_rows > 0) {
while ($row = $results->fetch_assoc()) {
echo 'Member Id:' . $row["member_id"] . '<br>';
}
} else {
echo " No Result";
}
?>

Not enter while loop after mysql_fetch_assoc

please take a look at this code :
$sql = "SELECT * FROM shop";
$result = mysql_query($sql);
echo $result;
echo "before lop";
while ($xxx = mysql_fetch_assoc($result)) {
echo "inside lop";
echo $xxx['column_name'];
}
echo "after lop";
When I run such code i receive :
Resource id #244
before lop
after lop
It did not enter while lop, and I really don't know why :(
I used before such code and there were no problems.
Can someone help me?
$sql = "SELECT * FROM shop";
$result = mysql_query($sql) or die(mysql_error());
echo mysql_num_rows($result);
Check how many records are present in your shop table. I think shop table is empty.That is why not entering in the while loop.
You can do like this
$count = mysql_num_rows($result);
if($count > 0) {
while ($xxx = mysql_fetch_assoc($result)) {
echo $xxx['column_name'];
}
}
I would guess that the call to mysql_fetch_assoc() has returned false, possibly due to no results being returned from the database, this would cause the while loop to not execute even once. I would check the output of var_dump(mysql_fetch_assoc($result)) to ensure that data has been returned.

PHP - Why is looping between WHILE and DO-WHILE give different results in mysql fetching?

I have one record in a table in my mysql database. I use the following PHP code to retrieve data:
$result = mysql_query($query = "SELECT realname FROM t_user");
if($result)
{
while($data=mysql_fetch_assoc($result)){
echo $data['realname'];
}
}
the results do not appear, but when I use a do-while loop like below:
if($result)
{
$data=mysql_fetch_assoc($result);
do{
echo $data['realname'];
} while($data=mysql_fetch_assoc($result));
}
the results appear, then I tried to add one more record to the table, in the while loop, only shows one data record (the first record), and the do-while loop displays all the data. Why is that? Is it because there is my code wrong?
The code you have shown can't possibly exhibit this problem, so the logical explanation is this:
$result = mysql_query($query = "SELECT realname FROM t_user");
if($result)
{
// Something fetched a row from $result before this statement is run
while($data=mysql_fetch_assoc($result)){
echo $data['realname'];
}
}
Seeing how you're comparing two similar codes, you may have accidentally written something in between the two:
if($result)
{
mysql_fetch_assoc($result);
while($data=mysql_fetch_assoc($result)){
echo $data['realname'];
}
}
$result = mysql_query($query = "SELECT realname FROM t_user");
if($result) { while($data=mysql_fetch_assoc($result)){
echo $data["realname"]; } }
$result = mysql_query($query = "SELECT realname FROM t_user");
var_dump($data);die;
see the result array in your browser. the above while loop should work. check your result.

How to get query result in table format using php-postgresql?

I am stuck with displaying result into table format. I tried with all options like "pg_fetch_row, pg_fetch_array, pg_fetch_all_columns", but I am not getting results in table format.
The query part is working fine, also I am getting results in XML format, but I need to display it in table format, also it should contain a column heading.
Please can anyone suggest me how to get the results in table format.
My PHP_PostgreSQL code shown in below:
<?
$conn = pg_connect("user=postgres password=nkr#123 host=localhost dbname=test");
if (!$conn) {
echo "An error occured.\n";
exit;
}
$cat=$_POST['cat'];
$subcat=$_POST['subcat'];
echo "Value of \$cat = $cat <br>Value of \$subcat = $subcat \n";
$result = pg_query($conn, "SELECT * FROM $cat JOIN $subcat ON $cat.districtco=$subcat.districtcode");
if (!$result) {
echo "An error occured in result.\n";
exit;
}
echo $result;
if (!$result) {
echo "An error occured in xml_result.\n";
exit;
}
?>
I am using this code to get results in XML:
$xml_result="<resultxml>";
$num = pg_num_fields($result);
while($row = pg_fetch_array($result)){
$xml_result.="<record>";
for ($i=0; $i < $num; $i++)
{
$xml_result .= "<".pg_field_name($result, $i).">".$row[$i]."</".pg_field_name($result, $i).">";
}
$xml_result.="</record>";
}
$xml_result.="</resultxml>";
echo $xml_result;
You do know you should process the result set with a for() loop, don't you?
Although psql will spit the result set from a query optionally in HTML, this only happens because psql was specifically written to do this.

Display mysql data in php function

Ok i got a problem now i want to display a data from the database and display it through a function now how do i do that??
like i have fetched a row from the database and its name is $row_field['data']; and it is correct now i have assigned a variable to it like this $data = $row_field['data']; now if i call it in a function it shows undefined variable even after i assigned it global in the function like this
function fun(){
global $data;
echo $data;
}
but if i assign it a value like 1 or 2 or anything it gets displayed without any error why is that so??
If it displays if you assign it a value like 1 or 2 while still in the global scope, then I can only assume that your database did not return the result you thought it did. Does the database value display if you echo it out outside of the function?
Global is evil. I dont know what you are trying to do, but why dont you just do the query in the function itself?
If you have a column named data and your php call was something like
$result = mysql_query("SELECT data FROM mytable");
while ($row_field = mysql_fetch_assoc($result, MYSQL_NUM)) {
...
}
Then you could replace ... with print $row_field['data'].
Else please provide a snippet of your code where you query the db and retrieve the result.
When learning php try to start with simple things. For example in order to get some data from a database follow the examples from php website.
<?php
$conn = mysql_connect("localhost", "mysql_user", "mysql_password");
if (!$conn) {
echo "Unable to connect to DB: " . mysql_error();
exit;
}
if (!mysql_select_db("mydbname")) {
echo "Unable to select mydbname: " . mysql_error();
exit;
}
$sql = "SELECT id as userid, fullname, userstatus
FROM sometable
WHERE userstatus = 1";
$result = mysql_query($sql);
if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}
if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
// While a row of data exists, put that row in $row as an associative array
// Note: If you're expecting just one row, no need to use a loop
// Note: If you put extract($row); inside the following loop, you'll
// then create $userid, $fullname, and $userstatus
while ($row = mysql_fetch_assoc($result)) {
echo $row["userid"];
echo $row["fullname"];
echo $row["userstatus"];
}
mysql_free_result($result);
If all this goes well go a little further change a little the while loop.
$myArray = array();
while ($row = mysql_fetch_assoc($result)) {
$myArray[] = $row;
}
mysql_free_result($result);
// now you can start playing with your data
echo $myArray[0];
Small steps...

Categories