PHP Mysql table fetch only gives first row - php

$query = "SELECT * FROM pass";
$result = mysql_query($query,$conn);
echo mysql_num_rows($result);
while($row = mysql_fetch_array($result))
{
$username = $row['user'];
$password = $row['pass'];
}
Num rows = 12 but while loops only the first one if I use in while $row = mysql_fetch_array(mysql_query("SELECT * FROM pass",$conn))
If i use the first code it gives error after the first row,
Warning: mysql_fetch_array() expects parameter 1 to be resource, string given in

you define the variables wrong if you don't give it a number it will always only have 1 result and do some reading about newer ways to connect to mysql database
$i = 0;
while($row = mysql_fetch_assoc($result))
{
$username[$i] = $row['user'];
$password[$i] = $row['pass'];
$i++;
}
//you can test like this
$r = 0;
while($r < $i)
{
echo $username[$r];
echo $password[$r];
$r++;
}

Related

'mysqli_fetch_array' echoes SQL entries back twice [duplicate]

I managed to create a query that fit my need. But now I am running into issues where given the current way of displaying results the query repeats each and every result twice. How can i make it work correctly where it shows it once.
Code:
$sql = "SELECT DISTINCT contacts.contact_id, user_accounts.full_name,
contact_notes.note_name, contact_notes.type, contact_notes.note_id,
contact_posts.why_post, contact_posts.type, contact_posts.post_id
FROM contacts, user_accounts, contact_notes, contact_posts
WHERE (contacts.system_id = '$sid' AND contacts.contact_id =
user_accounts.system_id AND contact_notes.user_allowed = '$everybody' AND
contact_posts.user_allowed = '$everybody') OR (contacts.contact_id = '$sid'
AND contacts.system_id = user_accounts.system_id AND contact_notes.user_allowed
= '$everybody' AND contact_posts.user_allowed = '$everybody')
LIMIT $startrow, 20";
$query = mysql_query($sql) or die ("Error: ".mysql_error());
$result = mysql_query($sql);
if ($result == "")
{
echo "";
}
echo "";
$rows = mysql_num_rows($result);
if($rows == 0)
{
print("");
}
elseif($rows > 0)
{
while($row = mysql_fetch_array($query))
{
$noteid = htmlspecialchars($row['note_id']);
$note_title = htmlspecialchars($row['note_name']);
$postid = htmlspecialchars($row['post_id']);
$postreason = htmlspecialchars($row['why_post']);
$datetimeadded = htmlspecialchars($row['just_date']);
print("<br /> <br />$note_title - $noteid <br /><br />$postreason - $postid");
}
}
Instead of using mysql_fetch_array() use mysql_fetch_assoc(). By default, mysql_fetch_array() will return an array with both associative and number indices which will result in you seeing the results twice.

how to use two mysql_fetch_array() arrays in one while loop

Hi i have two arrays built by using mysql_fetch_array() i want to use them both in one while loop :
$username =$_SESSION["username"];
$password =$_SESSION["password"];
$query1= "SELECT * FROM subs WHERE username='$username' AND password='$password' ";
$res1 = mysql_query($query1);
$row_cnt1 = mysql_num_rows($res1);
if($res1 === FALSE) {
die(mysql_error()); // TODO: better error handling
}
$row1 = mysql_fetch_array($res1);
if($row_cnt1 == 1){
$sid = $row1["id"];
}else{
die("there is more than one user with the same username and password");
}
$query2= "SELECT * FROM bookreservations ";
$res2 = mysql_query($query2);
$row_cnt2 = mysql_num_rows($res2);
if($res2 === FALSE) {
die(mysql_error()); // TODO: better error handling
}
$row2 = mysql_fetch_array($res2) ;
$query= "SELECT * FROM books";
$res = mysql_query($query);
$row_cnt = mysql_num_rows($res);
if($res === FALSE) {
die(mysql_error()); // TODO: better error handling
}
while ($row = mysql_fetch_array($res)){
if($row2["sid"] == $sid && $row2["bid"] == $row["id"]){
continue;
}
$temp = $row["id"];
$temp1 = $row["title"];
echo "Title : ".$temp1."<br><br>";
$temp1 = $row["authors"];
echo "Authors : ".$temp1."<br><br>";
$temp1 = $row["copies"];
echo "Copies : ".$temp1."<br><br><br><br>";
echo "<div class=\"re\"><input type=\"radio\" value=\"reserve\" name=\"".$temp ."\">reserve</div> <br><br><br><br><br><br>" ;
}
I want to extract things from row array but i don't want them to have some values from row2 array so I have to use them both in one while loop ,THE SUMMARY row array is ok it jumps to the next row in each loop but row2 array is stuck it doesn't move to the next row what to do ?
Although I don't really understand your task well, I however, would recommend the following edits
My assumption: You are trying to fetch all rows from $res for each $res2.
Although inefficient technique, but I'll edit your code as below.
while($row2 = mysql_fetch_array($res2) ){
while ( $row = mysql_fetch_array($res) ){ //Both $row & $row2 will have equal incremented rows.
}
}//Outer loop

database query, compare each two rows but getting only first two rows

Hi i am querying my db so that i can compare every two rows. ex 1 and 2, then 2 and 3, then 3 and 4. and so on and so forth. but it only compares the first two rows. any ideas? here is my code:
$result = mysql_query("SELECT *FROM attendance ORDER BY pid ASC") or die(mysql_error());
// check for empty result
if (mysql_num_rows($result) > 0) {
// $response["nominees"] = array();
while ($row = mysql_fetch_array($result)) {
// temp user array
$prev_sid = $row["sid"];
$prev_pid = $row["pid"];
$row2 = mysql_fetch_array($result);
if($row2["pid"] == $prev_pid){
if(($row2["sid"] - $prev_sid) == 1){
$attended_elec = mysql_query("SELECT pid FROM election_attendance where election_id = '$election_id'");
if(mysql_num_rows($attended_elec) > 0){
$not_officer = mysql_query("SELECT usertype FROM users WHERE pid = '$prev_pid'");
if(mysql_result($not_officer, 0) == "member"){
// echo "PID" . $prev_pid;
// $nominee["pid"] = $row2["pid"];
$user_details = mysql_query("SELECT *FROM users WHERE pid = '$prev_pid'");
if(mysql_num_rows($user_details) > 0){
$response["nominees"] = array();
while ($row3 = mysql_fetch_array($user_details)) {
$nominee = array();
$nominee["pid"] = $row3["pid"];
$nominee["firstname"] = $row3["firstname"];
$nominee["lastname"] = $row3["lastname"];
$nominee["gender"] = $row3["gender"];
$nominee["contact"] = $row3["contact"];
$nominee["email"] = $row3["email"];
$nominee["address"] = $row3["address"];
$nominee["institution"] = $row3["institution"];
$nominee["points"] = $row3["points"];
$nominee["usertype"] = $row3["usertype"];
// push single product into final response array
array_push($response["nominees"], $nominee);
}
}
}
}
}
}
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
} else {
// no products found
$response["success"] = 0;
$response["message"] = "There is no candidate yet from the records.";
// echo no users JSON
echo json_encode($response);
}
thanks in advance, have a nice day
Theres a problem right at the while(), you're fetching the 1st row, and is correct.
Then, inside it you fetch the 2nd, which is what you intend, but when the iteration is repeating, the while will fetch the 3rd, and the inner the 4th, so you lost there the comparisson between 2nd and 3rd.
// fetch data from DB
$results = "...";
if (mysql_num_rows($result) < 1)
{
// no products found tell your users
return;
}
// lets make some variables to walk through the list
$previous = mysql_fetch_array($results);
$actual = null;
// and now lets walk through the list
while($actual = mysql_fetch_array($results))
{
// execute your logic here
// than at the end move the actual row to become the previous
$previous = $actual;
}
NOTICE you shouldn't use mysql_ * methods they're are deprecated. you can use the brother mysqli_ * or PDO
I would do something like this? But there is almost certainly a less resource intensive way to do it.
$x = 0;
$y = 1;
$total = mysql_num_rows(mysql_query("SELECT * FROM `the_place`");
while ($x < $total AND $y < total){
$query1 = "SELECT * FROM `the_place` LIMIT $x,1";
$query2 = "SELECT * FROM `the_place` LIMIT $y,1";
$row1 = mysql_fetch_array(mysql_query($query1);
$row2 = mysql_fetch_array(mysql_query($query2);
// Do comparison here
if ($row1 == $row2){
// etc
}
$x = $x++;
$y = $y++;
}

Return multiple rows with mysqli

I am in the process of learning mysqli and I am trying to query a databases and return multiple rows. I understand that a loop has to be used to return multiple rows but I have no idea how I would implement this into my code. Some help in doing this would be greatly appreciated.
$query2 = mysqli_query($con,"SELECT * FROM journeys WHERE id = $id");
$count = mysqli_num_rows($query2);
if ($count == 0) {
$journeys = 'You have no future journeys.';
} else {
while ( $row = mysqli_fetch_assoc($query2) ) {
$from = $row ['origin'];
$to = $row ['destination'];
$date = $row ['date'];
$hour = $row ['hour'];
$minute = $row ['minute'];
$journeyid = $row ['journeyid'];
try this code
<?php
$con= mysqli_connect('localhost', 'root', '', 'your data base name');
$query2 = mysqli_query($con,"SELECT * FROM journeys where id=$id");
$count = mysqli_num_rows($query2);
if ($count == 0) {
$journeys = 'You have no future journeys.';
echo $journeys;
} else {
while ( $row = mysqli_fetch_assoc($query2) ) {
$from = $row ['origin'];
$to = $row ['destination'];
$date = $row ['date'];
$hour = $row ['our'];
$minute = $row ['minute'];
$journeyid =$row ['journeyid'];
echo $from.'<br>';
}
}
have a look at this line echo $from; this will display what ever you have in the origin column in your database, if you want to display destination, use this as well echo $to;
means what ever you want to display do it like this echo $variableName; it will display the result;
before you display the result it will only store it, but will not display it,
have a look i change your this line of code as well
its your code
if ($count == 0) {
$journeys = 'You have no future journeys.';
echo $journeys;// i added this line
i added one line, because what you are saying here that if $count is equal to zero, than $journeys='you have no future journeys'; but you are not using echo you have to use echo to display the result.
if you still have some confusion ask again, and try to google as well
$query2 = mysqli_query($con,"SELECT * FROM journeys WHERE id = $id");
$row = mysql_fetch_array($query2);
$count = count($row);
if(intval($count)>0)
{
for($i = 0; $i<$count; $i++)
{
$from[$i] = $row[$i]['origin'];
-------------------------------
----------------------
}
}

How to prevent this query from printing same results twice?

I managed to create a query that fit my need. But now I am running into issues where given the current way of displaying results the query repeats each and every result twice. How can i make it work correctly where it shows it once.
Code:
$sql = "SELECT DISTINCT contacts.contact_id, user_accounts.full_name,
contact_notes.note_name, contact_notes.type, contact_notes.note_id,
contact_posts.why_post, contact_posts.type, contact_posts.post_id
FROM contacts, user_accounts, contact_notes, contact_posts
WHERE (contacts.system_id = '$sid' AND contacts.contact_id =
user_accounts.system_id AND contact_notes.user_allowed = '$everybody' AND
contact_posts.user_allowed = '$everybody') OR (contacts.contact_id = '$sid'
AND contacts.system_id = user_accounts.system_id AND contact_notes.user_allowed
= '$everybody' AND contact_posts.user_allowed = '$everybody')
LIMIT $startrow, 20";
$query = mysql_query($sql) or die ("Error: ".mysql_error());
$result = mysql_query($sql);
if ($result == "")
{
echo "";
}
echo "";
$rows = mysql_num_rows($result);
if($rows == 0)
{
print("");
}
elseif($rows > 0)
{
while($row = mysql_fetch_array($query))
{
$noteid = htmlspecialchars($row['note_id']);
$note_title = htmlspecialchars($row['note_name']);
$postid = htmlspecialchars($row['post_id']);
$postreason = htmlspecialchars($row['why_post']);
$datetimeadded = htmlspecialchars($row['just_date']);
print("<br /> <br />$note_title - $noteid <br /><br />$postreason - $postid");
}
}
Instead of using mysql_fetch_array() use mysql_fetch_assoc(). By default, mysql_fetch_array() will return an array with both associative and number indices which will result in you seeing the results twice.

Categories