This question already has answers here:
How can I combine two strings together in PHP?
(19 answers)
Closed 7 years ago.
I have following sql query in PHP and output is as JSON:
<?php
$user_id = "1";
$sql = 'SELECT *,DATE_FORMAT(c.`date`, "%d.%m.%Y") AS date
FROM
conversation c,
users u
WHERE
c.user_two = u.uid AND c.user_one = '$user_id'
ORDER By c.precious_time DESC';
$result = mysqli_query($con, $sql);
$rows = array();
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$rows [] = $row;
}
mysqli_close($con);
echo json_encode($rows);
?>
When I execute the the php file it does not echo anything, but if I set c.user_one = "1" instead of c.user_one = '$user_id' then it works and it gives me the results. Is there a formatting error ?
If you use doublequotes it works:
$sql = "SELECT *,DATE_FORMAT(c.`date`, '%d.%m.%Y') AS date
FROM conversation c, users u
WHERE c.user_two = u.uid AND c.user_one = '$user_id'
ORDER By c.precious_time DESC";
FYI: http://php.net/manual/en/language.types.string.php#language.types.string.parsing
Related
I'm having an issue of retrieving values from two different tables. Here's the code so far:
$result = mysqli_query($conn,"SELECT * FROM articles");
$num = mysqli_num_rows($result);
while ($row = mysqli_fetch_array($result)) {
$uid=$row['_uid'];
$result2 = mysqli_query($conn, "SELECT _username FROM users WHERE _id = '$uid' ";
$num2 = mysqli_num_rows($result2);
while ($row2 = mysqli_fetch_array($result2)) {
$username = $row2['_username'];
}
$divtext='<h3>'.$row['_posttype'].'</h3> <h2>'.$username.' </h2>';
}
I've been reading that I should preform this while inside a while with multiple query, also found on w3 that you could directly assign a value to a variable directly using:
"SELECT _username INTO $username FROM users WHERE _id = '$uid' LIMIT 1";
But this works in SQL inside myadmin, in a php I can't find how to cast it.
I have also replace the fetch_row for fetch_assoc and still nothing, im struggling for two days already with this.
you could select al the value using a single query
SELECT a._uid , a._posttype, u._username
FROM articles a
INNER JOIN users u on a._uid = u._id
..
$result = mysqli_query($conn,
"SELECT a._uid , a._posttype, u._username
FROM articles a
INNER JOIN users u on a._uid = u._id");
$num = mysqli_num_rows($result);
while ($row = mysqli_fetch_array($result)) {
$divtext='<h3>'.$row['_posttype'].'</h3> <h2>'.$username.' </h2>';
}
$echo $divtext;
This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 5 years ago.
I wanna ask how to display database usernames from mysql in php echo
$send = $database->query('SELECT * FROM `usernames` ORDER BY RAND() LIMIT 1');
while ($show = mysql_fetch_array($send)) {
echo ''.$show['usernames'];
}
This fixes the php error:
$ts = $database->query('SELECT * FROM `usernames` ORDER BY RAND() LIMIT 1');
while ($show = $ts->fetch(PDO::FETCH_ASSOC)) {
echo ''.$show['usernames'];
}
For sake of completeness you expect only 1 row of results so while-loop could be redundant:
$ts = $database->query('SELECT * FROM `usernames` ORDER BY RAND() LIMIT 1');
$show = $ts->fetch(PDO::FETCH_ASSOC);
echo $show['usernames'];
$sql = "SELECT * FROM 'usernames` ORDER BY RAND() LIMIT 1' ";
$result = $conn->query($sql);
while($record = mysqli_fetch_array($result)) {
foreach ($record as $d){
echo $d;
}
}
Try this
$query = "SELECT * FROM `usernames` ORDER BY RAND() LIMIT 1"
$send = mysql_query($query);
while ($show = mysql_fetch_array($send)) {
echo ''.$show['usernames'];
}
I have two MySQL output which I need to encode in a single JSON output.
Output 1:
$sql = "select * from t1 ORDER BY id DESC LIMIT 25";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
$output[] = array_map("nl2br", $row);
}
Output 2:
$sql2 = "select * from t2 ORDER BY id DESC LIMIT 25";
$result2 = $conn->query($sql2);
while($row2 = $result2->fetch_assoc()) {
$output2[] = array_map("nl2br", $row2);
}
This is what I am doing to get them in single JSON_encode:
echo json_encode($output.$output2);
still not getting both the outputs. I came to know of another solutions i.e. to merge both the queries but I am not able to do that as well. I referred this question also but no luck :(
How about using UNION in your query? Please check it out here: https://dev.mysql.com/doc/refman/5.0/en/union.html
What about
$fullOutput = array_merge($output1, $output2);
echo json_encode($fullOutput);
This question already has answers here:
Get the single value of a sql result set
(3 answers)
Closed 9 years ago.
I have this query:
$result = "SELECT MAX(N) as maxid FROM (
SELECT SUBSTRING(id_f, 2,len(id_f) -1) as N
From formas WHERE id_f LIKE '%D%'
) t" ;
$res = odbc_exec($connection, $result) or die(odbc_error());
Now, if i put the query in SQL SERVER i get the correct result.
What i need is to save the maxid as a variable.. how to do it?
Thanks
What you need is the function odbc_fetch_array.
$result = "SELECT MAX(N) as maxid FROM (
SELECT SUBSTRING(id_f, 2,len(id_f) -1) as N
From formas WHERE id_f LIKE '%D%'
) t" ;
$res = odbc_exec($connection, $result) or die(odbc_error());
$info = odbc_fetch_array($res);
$content[] = $info;
$maxN = $content[0]
For a multiple rows query, you need to encapsulate the function in a while loop :
while($row = odbc_fetch_array($res))
{
print_r($row);
}
This question already has answers here:
SELECT COUNT(*) AS count - How to use this count
(5 answers)
Closed 1 year ago.
$query = "select count(*)
from relationships
where leader = 'user_id'";
$result = mysql_query($query);
how can i display the count? thanks
$count = mysql_fetch_array($result);
echo $count[0];
$query = "SELECT COUNT(*) AS total FROM table";
$result = mysql_query($query);
$values = mysql_fetch_assoc($result);
$num_rows = $values['total'];
echo $num_rows;
use $row = mysql_fetch_array($result) and access it by index 0: $row[0]
use an alias in your query ("select count(*) as cnt from ...") and $row = mysql_fetch_assoc($result) and access it by name: $row['cnt']
$abc="SELECT count(*) as c FROM output WHERE question1=4";
$result=mysqli_query($conn,$abc);
if($result)
{
while($row=mysqli_fetch_assoc($result))
{
echo $row['c'];
}
}
Its work completely in this it count the number of occurrences of 4 in the column