I have seen other people with that problem but the solutions I've seen aren't helping me, or I don't know how to use them :P
<?php
$ordre = "nom";
$croissance = "ASC";
if(isset($_GET["ordre"])){
$ordre = $_GET["ordre"];
};
if(isset($_GET["croissance"])){
$croissance = $_GET["croissance"];
};
$con = mysql_connect('localhost','root','');
mysql_select_db('sdj_jeux', $con);
$sql = "SELECT * FROM jeux ORDER BY $ordre $croissance";
$result = mysql_query($sql, $con);
$row = mysql_fetch_array($result);
$couleurcompteur = 0;
while ($row = mysql_fetch_array($result)){
$couleurcompteur += 1;
if($couleurcompteur % 2){
$classe = "pale";
} else {
$classe = "fonce";
};
?>
My code is skipping the first row of my database and I don't understand why.
Remove the line:
$row = mysql_fetch_array($result);
The while loop will grab the first row on the first iteration.
Resulting code:
<?php
$ordre = "nom";
$croissance = "ASC";
if(isset($_GET["ordre"])){
$ordre = $_GET["ordre"];
};
if(isset($_GET["croissance"])){
$croissance = $_GET["croissance"];
};
$con = mysql_connect('localhost','root','');
mysql_select_db('sdj_jeux', $con);
$sql = "SELECT * FROM jeux ORDER BY $ordre $croissance";
$result = mysql_query($sql, $con);
$couleurcompteur = 0;
while ($row = mysql_fetch_array($result)){
$couleurcompteur += 1;
if($couleurcompteur % 2){
$classe = "pale";
} else {
$classe = "fonce";
};
?>
Right here is your problem:
$row = mysql_fetch_array($result);
$couleurcompteur = 0;
while ($row = mysql_fetch_array($result)){
You call mysql_fetch_array() once before the while. This throws out the first row since you don't use it. Remove that un-needed call.
NOTICE: Do not use MySQL_* for it has been deprecated as of PHP 5.5. Use MySQLi_* or PDO instead
Related
Here is my code to encode data in JSON format, but it doesn't work. The result is []. Where is my mistake?
<?php
$conn = new mysqli('localhost','root','','project');
$data =array();
if(!empty($_GET['masp'])){
$masp =$_GET['masp'];
$sql ="SELECT *FROM sanpham WHERE masp='$masp'";
$result = mysqli_query($conn,$sql);
if($result){
while($r = mysqli_fetch_assoc($result)){
$r['masp'] =$data['masp'];
$r['loai'] =$data['loai'];
$r['hangsx']=$data['hangsx'];
$r['tensp']=$data['tensp'];
$r['img']=$data['img'];
$r['gia']=$data['gia'];
$r['nx']=$data['nx'];
}
}
}
print json_encode($data);
?>
You are setting your variables wrong.
In every while cycle you get a new $r variable that you want to add to your $data variable.
$conn = new mysqli('localhost', 'root', '', 'project');
$data = array();
if (!empty($_GET['masp'])) {
$masp = $_GET['masp'];
$sql = "SELECT *FROM sanpham WHERE masp='$masp'";
$result = mysqli_query($conn, $sql);
$i = 0;
if ($result) {
while ($r = mysqli_fetch_assoc($result)) {
$data[$i]['masp'] = $r['masp'];
$data[$i]['loai'] = $r['loai'];
$data[$i]['hangsx'] = $r['hangsx']];
$data[$i]['tensp'] = $r['tensp'];
$data[$i]['img'] = $r['img'];
$data[$i]['gia'] = $r['gia'];
$data[$i]['nx'] = $r['nx'];
$i += 1;
}
}
}
print json_encode($data);
You make mistake. You should swap variable data with r inner loop, but probably than also will works unpropely. write in while loop $data [] = $r;
I am trying to show the points outside whileloop. I am fetching result like this.
Facebook
Twitter,Facebook,Instagram,Youtube
Facebook
Facebook
It fetches Facebook from each row and count no of times in the end.
<?php
$q = "select * FROM users";
$r = mysql_query($q);
$total = mysql_num_rows($r);
while($row = mysql_fetch_assoc($r)) {
$fb= $row['social'];
$dbreq = implode(',',explode(',', $fb));
$fa=array("Twitter,",",Instagram,","Youtube");
$newstring = str_replace($fa, "", $dbreq);
echo $points= count(explode(',', $newstring));
}
?>
Please try like this,
<?php
$q = "select * FROM users";
$r = mysql_query($q);
$total = mysql_num_rows($r);
$cnt =0 ;
while($row = mysql_fetch_assoc($r)) {
$fb= $row['social'];
if (strpos($fb,'facebook') !== false) {
$cnt++;
}
}
echo "TOTAL:".$cnt;
?>
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'];
-------------------------------
----------------------
}
}
I have a table with two fields, named credithour and gradepoint.
I want to add the data from each row of credithour with gradepoint.
My code :
$totalcredithour = 0;
$GPA = "SELECT * FROM $dept_stu_id WHERE sessionyear = '$sessionyear'" or die (mysql_error());
$resultGPA = mysql_query($GPA);
while($data = mysql_fetch_array($resultGPA))
{
$credithour = "$data[credithour]";
$gradepoint = "$data[gradepoint]";
echo $totalcredithour += $credithour;
}
What am I doing wrong?
It added data, but showing the first credit hour also besides result.
Suppose, here i entered two data 3 and 5. When I run this code it echo 38. Which means "3+5=8" and 1st credithour = "3".
Actually other answer are wrong, you're doing or die(mysql_error()); next to a string, that's not right.
Your code should be like this..
$totalcredithour = 0;
$GPA = "SELECT * FROM `".$dept_stu_id."` WHERE sessionyear = `".$sessionyear."` ";
$resultGPA = mysql_query($GPA) or die(mysql_error());
while($data = mysql_fetch_array($resultGPA, MYSQL_ASSOC))
{
$credithour = $data['credithour'];
$gradepoint = $data['gradepoint'];
echo $totalcredithour += $credithour; //Make sure `$totalcredithot` isn't null/doesn't exist.
}
try something like this in your while loop:
$credithour = $data['credithour'];
$gradepoint = $data['gradepoint'];
Try it like this
I have edited the answer and added the floatval function
$totalcredithour = 0;
$GPA = "SELECT * FROM `".$dept_stu_id."` WHERE sessionyear = '".$sessionyear."'" or die (mysql_error());
$resultGPA = mysql_query($GPA);
while($data = mysql_fetch_array($resultGPA))
{
$credithour = $data['credithour'];
$gradepoint = $data['gradepoint'];
echo $totalcredithour += floatval($credithour);
}
Here's what I currently have to get the values from database
$unread_messages = "";
$query = mysql_query("SELECT * FROM pm WHERE msg_to='".$_SESSION['user_id']."' or
msg_from='".$_SESSION['user_id']."' LIMIT 10") or die(mysql_error());
while($row = mysql_fetch_array($query)){
$msg_id = $row["msg_id"];
$msg_to = $row["msg_to"];
$msg_from = $row["msg_from"];
$msg_title = $row["msg_title"];
$msg_content = $row["msg_content"];
$msg_date = $row["msg_date"];
$msg_read = $row["msg_read"];
}
now I need it so that everytime '$msg_read == 1' it adds +1 to '$unread_messages'. Can somebody help me ?
Just add a check in your loop
$unread_messages = 0;
while ( ... ) {
// do stuff
if ($msg_read == 1) {
$unread_messages++;
}
}
I highly recommend that you take a look at the mysqli or PDO extensions for database access. mysql_ is outdated and should not be used.
have you tried this one ?
$unread_messages = "";
$query = mysql_query("SELECT * FROM pm WHERE msg_to='".$_SESSION['user_id']."' or
msg_from='".$_SESSION['user_id']."' LIMIT 10") or die(mysql_error());
while($row = mysql_fetch_array($query)){
$msg_id = $row["msg_id"];
$msg_to = $row["msg_to"];
$msg_from = $row["msg_from"];
$msg_title = $row["msg_title"];
$msg_content = $row["msg_content"];
$msg_date = $row["msg_date"];
$unread_messages = ($row["msg_read"]==1 && $unread_messages=='') ? $unread_messages=1 : $unread_messages=$unread_messages+1;
}
ohh, im sorry, using PDO actualy really good practice, may be you can try this
$unread_messages = "";
try {
$dbh = new PDO("mysql:host=$hostname;dbname=dbname", $username, $password);
$sql = "SELECT * FROM pm WHERE msg_to='".$_SESSION['user_id']."' or msg_from='".$_SESSION['user_id']."' LIMIT 10";
foreach ($dbh->query($sql) as $row){
$unread_messages = ($row["msg_read"]==1 && $unread_messages=='') ? $unread_messages=1 : $unread_messages=$unread_messages+1;
}
$dbh = null;
}
catch(PDOException $e){
echo $e->getMessage();
}
$unread_message = 0;
while($row = mysql_fetch_array($query))
{
//Your code
if ($msg_read === 1)
$unread_message++;
}