I've a little problem here..
I am using jquery easy UI to get data from mysql and then write a table in my php application..
nah.. the problem is.. my code or my query doesn't work..
When I am using my old mysql_connect, my code works well... but when I try to write it to mysql, I get nothing...
Here is my old code:
$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
$rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
$offset = ($page-1)*$rows;
$result = array();
$conn = mysql_connect('127.0.0.1','root','');
mysql_select_db('colosus',$conn);
$rs = mysql_query("select count(*) from user");
$row = mysql_fetch_row($rs);
$result["total"] = $row[0];
$rs = mysql_query("select * from user limit $offset,$rows");
$rows = array();
while($row = mysql_fetch_object($rs)){
array_push($rows, $row);
}
$result["rows"] = $rows;
echo json_encode($result);
and its the PDO query code...
$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
$rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
$offset = ($page-1)*$rows;
$result = array();
$rs = $db->query("select count(*) from user");
$row = $rs->fetch(PDO::FETCH_NUM);
$result["total"] = $row[0];
$rs = $db->query("select * from user as u, leveluser as l where u.idLevelUser = l.idLevelUser limit $offset,$rows");
$rows = array();
while($row = $rs->fetch(PDO::FETCH_OBJ)){
array_push($rows, $row);
}
$result["rows"] = $rows;
echo json_encode($result);
The last query (PDO) doesn't work.. I dont know why.. anybody can help me???
Looks like you forget to instanciate your $db object.
Try to put this before $result = array();
$db = new PDO("mysql:host=127.0.0.1;dbname=colosus", 'root', '');
create the pdo connection like
$db = new PDO('mysql:host=localhost;dbname=testdb;charset=UTF-8',
'username',
'password',
array(PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
);
try query like
$query= $db->prepare("select * from user as u,
leveluser as l
where u.idLevelUser = l.idLevelUser
limit $offset,$rows");
$query->execute();
$result=$articlequery->fetch(pdo::FETCH_ASSOC);
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 have a function from class that gets data from MySQL. All works OK but I also want to know how I can get the column names for MySQL data.
Here is my code :
public static function getTickets(){
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$sql = "select tickets.*,customers.* from tickets,customers where
(tickets.ticket_customer_id = customers.customer_id) order by tickets.ticket_open_date desc ";
$st = $conn->prepare($sql);
$st->execute();
$list = array();
while($row=$st->fetch()) {
$tickets = New Tickets($row);
$list[] = $tickets;
}
//total rows of customer
$sql = "select FOUND_ROWS() as totalRows";
$totalRows = $conn->query($sql)->fetch();
$conn=null;
$columnCount = $st->columnCount();
//pass the values to page
return (array("results"=>$list ,"totalRows"=>$totalRows,"columnCount"=>$columnCount));
}
It's hard to tell what you need here, but looking at your code I would say that everything you need you can get from the very data you are fetching
public static function getTickets($conn){
$sql = "select tickets.*,customers.* from tickets,customers where
(tickets.ticket_customer_id = customers.customer_id) order by tickets.ticket_open_date desc ";
$st = $conn->query($sql);
$list = array();
while($row=$st->fetch(PDO::FETCH_ASSOC)) {
$list[] = New Tickets($row);
$columnNames = array_keys($row);
}
//total rows of customer
$totalRows = count($list);
$columnCount = count($columnNames);
//pass the values to page
return (array("results"=>$list ,"totalRows"=>$totalRows,"columnCount"=>$columnCount));
}
I've done this:
$result = mysql_query("SELECT image, id FROM store WHERE username = '$username_show'");
$num_rows = mysql_num_rows($result);
$ids = mysql_fetch_assoc($result);
$ids = $ids['id'];
for ($i = 0; $i < $num_rows; $i++) {
echo "<img src='get.php?id=$ids[$i]' height='300'><p/>";
}
I want to show all of my photos that has that username. But the $ids array only gets one index, and that's the last ID. What am I doing wrong?
Like #Matthew said thet are deprecated use :
// mysqli
$mysqli = new mysqli("example.com", "user", "password", "database");
$result = $mysqli->query("SELECT image, id FROM store WHERE username = '$username_show'");
$row = $result->fetch_assoc();
echo htmlentities($row['row']);
// PDO
$pdo = new PDO('mysql:host=example.com;dbname=database', 'user', 'password');
$statement = $pdo->query("SELECT image, id FROM store WHERE username = '$username_show'");
$row = $statement->fetch(PDO::FETCH_ASSOC);
echo htmlentities($row['row']);
To answer your comment :
- use the array function
$result_array = array();
while($row = mysql_fetch_assoc($result))
{
$result_array[] = $row;
}
$result = $mysqli->query("SELECT id FROM store WHERE username = '$username_show'");
$result_array = array();
while($row = mysqli_fetch_assoc($result))
{
$result_array[] = $row;
}
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
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++;
}