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
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";

}

?>

Link to comment
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.

 

 

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??

Link to comment
Share on other sites

The original code and the "fixed" code are both mixing a mysqli query with a mysql_fetch_xxxxx, which is what is causing the error.

 

Pick mysqli or mysql. Don't mix them.

 

Sorry if I sound stupid but what's the difference? between mysqli and mysql

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.