have created a mobile site for my client www.-leeds-united-fans.co.uk and the problem is when you view the the latest news is not pulling through for the latest news properly on the home page.
It does not show the latest news item, however when you go to the news section it pulls through all together.
I am using the same coding for both just with a limiter on the latest:
$query = "SELECT * from newsfeed_items order by id desc limit 0,6";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
while ($row = mysql_fetch_assoc($result)) {
echo "<div id=\"featureone\">";
echo "<div class=\"comtitle\"><a href=\"news.php?id=".$row['id']."\" >".$row['title']."</a></div>";
echo "</div>";
}
can anyone help please?
Never assume a MySQL query goes through without an error. always do some kind of error handling like this:
if (!($result=#mysql_query($query, $connection)))
die('Error ' . mysql_errno() . ": " . mysql_error());
This will also give you (and us) some information on what the nature of your error is, and you can google the error number or ask us here.
Related
Noob alert here. I'm just (trying to) teaching myself PHP because I need it on my internship job. Now I am creating a survey and I am trying to use while loops to show the questions and their respective answers onto my XAMPP. I am using this code:
<?php
mysqli_select_db($conn, "surveyordina");
//code hier schrijven
$sql = "SELECT questions_body FROM survey_questions where subthema_id = 1";
$sql2 = "SELECT answer_body FROM survey_answers where answer_id = 1 or answer_id = 2 or answer_id = 3";
$result = mysqli_query($conn, $sql);
$result2 = mysqli_query($conn, $sql2);
if(mysqli_num_rows($result2) > 0){
while($row = mysqli_fetch_assoc($result)) {
echo "<br>" ."Vraag: <br>" . $row["questions_body"]. "<br>";
while($row_answer = mysqli_fetch_assoc($result2)) {
echo $row_answer["answer_body"]. "<br>";
}
}
}
else{
echo "No results";
}
Now the returning part of this code looks like this:
Vraag:
Is the organization aware of where all the personal data is stored? Be it on-site or in the cloud, hosted by the company or by a third party?
Yes
No
I don't know
Vraag:
Is the organization able to locate and find personal data of a particular data subject?
Vraag:
Does the organization have technology in place to return all personal data on a given data subject, given a single search from personnel?
Vraag:
testerino kekkerino
I am trying to get the Yes, No, and I don't know parts under each and every question but only seem to get it under one of the questions.
Is there a method to return the whole answer part under each question? If so, am I on the right path or am I doing something completely wrong?
Thanks in advance.
Once you hit the end of a result-set, mysqli_fetch_assoc() doesn't just keep cycling through. So you're hitting the end of the $result2 set within the first outer loop, and then that's it, there are no more results to fetch.
You should fetch all of the $result2 results outside of the $result loop, assign the results to a variable, and then loop through that array within the $result loop.
Something like:
if(mysqli_num_rows($result2) > 0){
$result2Set = mysqli_fetch_all($result2, MYSQLI_ASSOC);
while($row = mysqli_fetch_assoc($result)) {
echo "<br>" ."Vraag: <br>" . $row["questions_body"]. "<br>";
foreach($result2Set as $row_answer) {
echo $row_answer["answer_body"]. "<br>";
}
}
}
Hi I am trying to fetch data from a particular coloumn from all rows.
Eg Situation:
DB Data: id, fbid, name
$sql = 'SELECT id FROM table WHERE table.fbid IN (1234,5678,4321)';
$sql_run = mysql_query($sql);
$sql_fetch = mysql_fetch_assoc($sql_run);
print_r($sql_fetch);
This returns the data when I test it using Sequel PRO or PHPmyAdmin.
But when I print the array it only displays one value.
Can you help me with a solution or tell me where I'm going wrong?
<?php
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Persons");
while($row = mysqli_fetch_array($result))
{
echo $row['FirstName'] . " " . $row['LastName'];
echo "<br>";
}
mysqli_close($con);
?>
Before using a function, or - at least - when it does not what you expect - it's always a good idea to read the function description in the manual page.
PHP provides extremely easy access to its manual pages. All you need to type in the address bar is php.net/function name. It takes less time than typing whole question on Stack Overflow, yet you will get exactly the same answer. Think of efficiency.
You need to loop through each row
$sql_run = mysql_query($sql) or die(mysql_error());
while ($sql_fetch = mysql_fetch_assoc($sql_run)) {
print_r($sql_fetch);
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in select
Okay, so I have my page "List Customers" which then links to "List Jobs" adding on ?custID= into the URL, then on List jobs I have used $_GET["custID"] so my query uses the custID from the URL. that works fine, lists out my jobs for said customer using their ID.
My problem comes up now, I need the links on this page to give my third page the techID as well. But the techID is in jobDetails, so my query cannot give them the techID as it is querying my job section.
This is my "ListJobs.php" page, which upon choosing a customer in the page before it, loads this page with the url ListJobs.php?custID=001 (001 being an example, it will give the number depending on your choice of customer).
<?php
// Get data from the database depending on the value of the id in the URL
mysql_select_db($database_con_sim5, $con_sim5);
$query_Recordset1 = "SELECT * FROM job WHERE custID=" . $_GET["custID"] ;
$Rs1 = mysql_query($query_Recordset1);
// Loop the recordset $rs
while($row = mysql_fetch_array($Rs1)) {
$strName1 = $row['jobID'] . " " . $row['jobDesc'] . " " . $row['computerName'];
$strLink = "<a href = 'jobDetails.php?jobID=".$row['jobID']."'>".$strName1."</a>";
// Write the data of the person
echo "<li>" . $strLink . " </li>";
}
// Close the database connection
mysql_close();
?>
Then on pressing one of the links, link to jobDetails.php?jobID= with the job number.
I am able to show all the details in Job Detail with this, but I also need my Tech Name to appear, Tech Name is not in Job Details, but Tech ID is.
Here is my Job Details page coding :
<?php
// Get data from the database depending on the value of the id in the URL
mysql_select_db($database_con_sim5, $con_sim5);
$query_Recordset1 = "SELECT * FROM jobDetail WHERE jobID=" . $_GET["jobID"] ;
$query_Recordset2 = "SELECT technician.techName FROM technician
WHERE techID=" . $query_Recordset1["techID"] ;
$Rs1 = mysql_query($query_Recordset1);
$Rs2 = mysql_query($query_Recordset2);
while($row1 = mysql_fetch_array($Rs1)) {
while($row2 = mysql_fetch_array($Rs2)) {
echo "<dt><strong>Job Note ID:</strong></dt><dd>".$row1["jobNoteID"]."</dd>";
echo "<dt><strong>Job Notes:</strong></dt><dd>".$row1["jobNotes"]."</dd>";
echo "<dt><strong>Date Completed:</strong></dt><dd>".$row1["dateCompleted"]."</dd>";
echo "<dt><strong>Time Spent:</strong></dt><dd>".$row1["timeSpent"]."</dd>";
echo "<dt><strong>Job ID:</strong></dt><dd>".$row1["jobID"]."</dd>";
echo "<dt><strong>Technician ID:</strong></dt><dd>".$row1["techID"]."</dd>";
echo "<dt><strong>Technician Name:</strong></dt><dd>".$row2["techName"]."</dd>";
}
}
// Close the database connection
mysql_close();
?>
The error I am getting is:
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in D:\xampp\htdocs\Sim5Server\Pages\jobDetails.php on line 129
That line is:
while($row2 = mysql_fetch_array($Rs2)) {
I hope I am making any sense AT ALL.
To sum up, I need my final page to show data from mysql technician using the Primary/index key techID.
Some way to add techID onto either listJobs' links to job details or in job details' second Recordset.
EDIT:
I should probably state this will never be used on the net, I only need it to work for an assignment. in future, thanks to a comment, I will no longer be using mysql_* I am jsut using them as my entire workbook tells us to use it.
Try making this change in your jobDetails.php page
$query_Recordset1 = "SELECT * FROM jobDetail WHERE jobID=". (int) $_GET["jobID"];
$Rs1 = mysql_query($query_Recordset1) or die(mysql_error());
while($row1 = mysql_fetch_array($Rs1)) {
$query_Recordset2 = "SELECT technician.techName FROM technician
WHERE techID=" . $row1["techID"] ;
$Rs2 = mysql_query($query_Recordset2) or die(mysql_error());
while($row2 = mysql_fetch_array($Rs2)) {
For the second query you were using the result resource from executing the first query as techID. Also you will have to query for techName from inside the while loop fetching job details, since only then will you have the techID.
I'm trying to code a basic messaging system for my website. I have the sending and reciving set up, but for some reason on the inbox the html just stops displaying. It displays halfway through the page and then just stops for some reason. Won't even display basic html like if I typed Hello it wouldn't show up. I'm confused as this has never happened before.
</table>
<p>Hello</p><!--THIS WILL DISPLAY-->
<?php
///////////End take away///////////////////////
// SQL to gather their entire PM list
include_once ('../../mysql_server/connect_to_mysql.php');
$sql = mysql_query("SELECT * FROM messaging WHERE to_id='$my_id' AND recipientDelete='0' ORDER BY id DESC LIMIT 100");
while($row = mysql_fetch_array($sql)){
$date = strftime("%b %d, %Y",strtotime($row['time_sent']));
if($row['opened'] == "0"){
$textWeight = 'msgDefault';
} else {
$textWeight = 'msgRead';
}
$fr_id = $row['from_id'];
// SQL - Collect username for sender inside loop
$ret = mysql_query("SELECT * FROM myMembers WHERE id='$fr_id' LIMIT 1");
while($raw = mysql_fetch_array($ret)){ $Sid = $raw['id']; $Sname = $raw['firstname']; }
?>
<p>Hello</p><!--THIS WON'T DISPLAY-->
<table width="96%" border="0" align="center" cellpadding="4">
Any help is appreciated..
EDIT:
The first while loop does close, just after the table. Everything outside the first while loop displays, however, everything inside the while loop doesn't.
Don't know if this is just a cut and paste error, but your first while loop doesn't look closed. Try closing it and see where it goes from there.
while($row = mysql_fetch_array($sql)){ //needs closing
EDIT:
Have you tried to see if your sql is throwing any errors:
// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$sql) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
This PHP link might be useful.
$sql = "SELECT messages.text, users.name FROM messages INNER JOIN users ON messages.user_id=users.id ORDER BY messages.ms_id DESC LIMIT 10";
$result = mysql_query($sql);
$rows = array();
while($row = mysql_fetch_array($result))
{
$rows[]=$row;
}
echo $rows[0][1].$rows[0][0];
/* for($i=0;$i<=10;$i++)
{
echo $rows[i][1].$rows[i][0];
}
*/
This script is supposed to show the last 10 messages in a chat.What I'm doing is getting the Name of the user from the users table and the text from the message table and I want to display them in my chat window.Right now I have only 4 messages recorded and don't know how this affects the whole script I should implement a check for this too, but the bigger problem is that when i use echo $rows[0][1].$rows[0][0]; the info is displayed correctly, but when I try to make a loop so I can show tha last 10 (I tried the commented one) then nothing is displayed.I thought at least when I use this loop I'll see the 4 recorded messages but what really happen is a blank window.Obvously I have the info recorded in $rows[] and can echo it, but don't understand why this loop don't work at all.I'll appreciate if someone can help me with this and with the check if the messages are less then 10.
Thanks.
Leron
P.S
Here is the edited script, thanks to all of you, I need the array because otherwise the most recent message is shown at the top which is not an opiton when I use it for diplaying chat masseges.
for($i=10;$i>=0;$i--)
{
if($rows[$i][1]!="" || $rows[$i][0]!="")
{
echo $rows[$i][1].' : '.$rows[$i][0];
echo "</br>";
}
}
Your FOR loop was running 11 times even if only 10 records. The second clause should be a < instead of <=. Plus the $ was missing on the i variable.
For example sake, you don't really need to make an array from the rows, and you can refer to the fields by name:
while($row = mysql_fetch_array($result))
{
echo $row['name'] . ' says: ' . $row['message'] . '<BR>';
}
why not just do
while($row = mysql_fetch_array($result))
{
echo $row[1]." ".$row[0];
}
Your query, auto limits it to the last 10, this will then show anything from 0 to 10 which get returned.
PS I added a space between username and message for readability
You need $ symbols on your i variable:
for($i=0;$i<10;$i++)
{
echo $rows[$i][1].$rows[$i][0];
}
A more robust solution would be like this:
$sql = "SELECT messages.text, users.name FROM messages INNER JOIN users ON messages.user_id=users.id ORDER BY messages.ms_id DESC LIMIT 10";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
echo $row[1].$row[0];
}