Jump to content

[SOLVED] mysql_result not working


lokie538

Recommended Posts

Hi,

 

With this code im trying to get the output in a table. But I keep getting this error:

 

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in C:\wamp\www\4\review.php on line 19

 

 

<?php
include("checkloginstatus.php");
$host = $_SESSION['host'];
$user = $_SESSION['user'];
$passwd = $_SESSION['passwd'];
$dbname = "ppss";
$cxn = mysqli_connect($host,$user,$passwd,$dbname) 
            or die ("User name or password is wrong.");

$query = "SELECT Given_Names, Family_Name, Roll_class, Date_Enrolled, T.ContactDate From studePE1 S, TIEnrolmentReview T WHERE S.Student_ID=T.StudentID ORDER BY 4 ASC";
$result = mysqli_query($cxn,$query) or die ("Couldnt execute query.");
$num = mysqli_num_rows($result);

echo "<b><center>Database Output</center></b><br><br>";

$i=0;
while ($i < $num) {

$Given_Names=mysql_result($result,$i,'Given_Names');
$Family_Name=mysql_result($result,$i,"Family_Name");
$Roll_class=mysql_result($result,$i,"Roll_class");
$Date_Enrolled=mysql_result($result,$i,"Date_Enrolled");
$ContactDate=mysql_result($result,$i,"ContactDate");

echo "<b> $Given_Names $Family_Name</b><br>$Roll_class<br>$Date_Enrolled<br>$ContactDate<hr><br>";

$i++;
}

mysql_error()
?>

 

Do you know whats wrong as Ive never used the mysql_result function before?

Thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/101002-solved-mysql_result-not-working/
Share on other sites

Using a counter to retrieve records is a poor choice anyway. Use a while loop with one of the mysql_fetch_ functions. Also, no need to copy all the results into variables just to use those variables once.

 

<?php
include("checkloginstatus.php");
$host = $_SESSION['host'];
$user = $_SESSION['user'];
$passwd = $_SESSION['passwd'];
$dbname = "ppss";
$cxn = mysqli_connect($host,$user,$passwd,$dbname) 
           or die ("User name or password is wrong.");

$query = "SELECT Given_Names, Family_Name, Roll_class, Date_Enrolled, T.ContactDate From studePE1 S, TIEnrolmentReview T WHERE S.Student_ID=T.StudentID ORDER BY 4 ASC";
$result = mysqli_query($cxn,$query) or die (mysql_error());

echo "<b><center>Database Output</center></b><br><br>";

while ($row = mysql_fetch_assoc($result)){

 echo "<b> " . $row['Given_Names'] . " " . $row['Family_Name'] . " </b><br>" . $row['Roll_class'] .
      "<br>" . $row['Date_Enrolled'] . "<br>" . $row['ContactDate'] . "<hr><br>\n";

}

?>

Using a counter to retrieve records is a poor choice anyway. Use a while loop with one of the mysql_fetch_ functions. Also, no need to copy all the results into variables just to use those variables once.

 

 

Now im getting a Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource error??

 

Is there something wrong with my query as it works in phpmyadmin??

Yup its working now with this code:

 

<?php
include("checkloginstatus.php");
$host = $_SESSION['host'];
$user = $_SESSION['user'];
$passwd = $_SESSION['passwd'];
$dbname = "ppss";
$cxn = mysqli_connect($host,$user,$passwd,$dbname) 
            or die ("User name or password is wrong.");

$query = "SELECT Given_Names, Family_Name, Roll_class, Date_Enrolled, T.ContactDate From studePE1 S, TIEnrolmentReview T WHERE S.Student_ID=T.StudentID ORDER BY 4 ASC";
$result = mysqli_query($cxn,$query) or die ("Couldnt execute query.");
$num = mysqli_num_rows($result);

echo "<b><center>Database Output</center></b><br><br>";

while ($row = mysqli_fetch_assoc($result)){

  echo "<b> " . $row['Given_Names'] . " " . $row['Family_Name'] . " </b><br>" . $row['Roll_class'] .
       "<br>" . $row['Date_Enrolled'] . "<br>" . $row['ContactDate'] . "<hr><br>\n";
}
?>

 

Thanks everyone :D

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.